最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

directory - Post Ancestor and Child Post in Custom Post Type

programmeradmin1浏览0评论

Hierarchical structure of custom post type "Book" (for example).

When we are on Post 2-95, I want to know:

  • Does the post has this post ancestor(Post 1-31)?
  • Does it have child posts(Post 3-19, Post 3-10)?

Then, if it has:

  • an ancestor post: retrieve (object) of this post.
  • a child posts: retrieve (objects) of these posts.

Hierarchical structure of custom post type "Book" (for example).

When we are on Post 2-95, I want to know:

  • Does the post has this post ancestor(Post 1-31)?
  • Does it have child posts(Post 3-19, Post 3-10)?

Then, if it has:

  • an ancestor post: retrieve (object) of this post.
  • a child posts: retrieve (objects) of these posts.
Share Improve this question edited May 16, 2012 at 10:16 Vitalij asked May 16, 2012 at 9:40 VitalijVitalij 2381 gold badge2 silver badges15 bronze badges 4
  • 2 Compensation lack of english with image = kool +1 – kaiser Commented May 16, 2012 at 10:08
  • I hope that will help ;) – Vitalij Commented May 16, 2012 at 10:10
  • fixed english :) – kaiser Commented May 16, 2012 at 10:11
  • N/P. We all appreciate if the OP invests the same effort into asking, as others need for answering. – kaiser Commented May 16, 2012 at 10:42
Add a comment  | 

2 Answers 2

Reset to default 1

Given a post represented by a post object $p, you can find out if post 31 is the parent via:

if($p->post_parent == 31){
    // it is!
} else {
    // it isn't
}

To figure out the children, something like:

$posts = get_posts(array(
    'post_parent' => $p->ID,
    'post_type'   => $p->post_type
));
// if there are children, they will be contained in `$posts`

Finally, to determine how many levels deep down the hierarchy you are, you will need to recurse up the hierarchy $p->parent_post == 0, and then count how many times you needed to do that.

e.g.

$level = 1;
while($parent->parent_post != 0){
    $level++;
    $parent = get_post($parent->parent_post);
}

Check if current post is in range

We check with the function, if we are...

  • in the Loop?
  • in the set range?

Stick all the functions in your functions.php file.

function wpse52285_is_post_in_range( $post, int $range_from, int $range_to )
{
    // If we're IN the LOOP @link http://codex.wordpress/Function_Reference/in_the_loop
    if ( ! in_the_loop() )
        return false;

    // Abort if not in the allowed range
    if ( ! in_array( $post->ID, range( $range_from, $range_to ) ) )
        return false;

    return true;
}

Check if we got children in range

We check, if we...

  • are inside the loop?
  • got children of the needed post type (can be any custom post type, post, page, attachment, link, etc.)
  • the children are in range?

In case nothing was found, we return false, so we can make our check easier.

function wpse52285_get_children_in_range( $post, int $range_from, int $range_to, $post_type = 'post' )
{
    if ( ! in_the_loop() )
        return false;

    // get_children() @link http://codex.wordpress/Function_Reference/get_children
    $children = get_children( "post_parent={$post->ID}&post_type={$post_type}" );
    if ( 0 < count( $children ) )
    {
        foreach ( $children as $child )
        {
            in_array( $id, range( $range_from, $range_to ) ) AND $in_range[] = $child;
        }
        if ( 0 < count( $in_range ) )
            return $in_range;
    }

    return false;
}

Check if we got ancestors

We check, if...

  • we are in the loop?
  • we got ancestors?
  • ancestors are in range?

In case nothing meets, we again return false.

function wpse52285_get_ancestors_in_range( $post, int $range_from, int $range_to )
{
    if ( ! in_the_loop() )
        return false;

    // get_post_ancestors @link http://codex.wordpress/Function_Reference/get_post_ancestors
    $ancestors = get_post_ancestors( $post->ID );
    foreach ( $ancestors as $ancestor )
    {
        in_array( $ancestor->ID, range( $range_from, $range_to ) ) AND $in_range[] = $ancestor;
    }
    if ( 0 < count( $in_range ) )
        return $in_range;

    return false;
}

Template

Now we can use it in any template like this:

// The loop
if have_posts() : while( have_posts() ): the_post();
    global $post;

    // Is our current post in range?
    if ( wpse52285_is_post_in_range( $post, 2, 95 ) )
    {
        // Are any child posts in range?
        $children = wpse52285_get_children_in_range( $post, 3, 19 );
        if ( $children )
        {
            // Do stuff with the children
        }

        // Are any ancestors in range?
        $ancestors = wpse52285_get_ancestors_in_range( $post, 1, 31 );
        if ( $ancestors )
        {
            // Do stuff with the ancestors 
        }
    }
endwhile;
endif;
发布评论

评论列表(0)

  1. 暂无评论