0

please help me: I need to get the parent of the current page, in other words: to get the parent of $direct_parent.Thanks!

<?php 
    global $post;
    $direct_parent = $post->post_parent;
    $parent = $direct_parent->post_parent;
wp_list_pages( array(
    'child_of' => $parent,
    'title_li'     => false,
    'depth' => 1
) );
?>

This code doesn't work(

2 Answers 2

3

Try using get_post_ancestors. Here is how you can apply this in your case:

<?php
    global $wp_query;
    $post = $wp_query->post;
    $ancestors = get_post_ancestors($post);
    if( empty($post->post_parent) ) {
        $parent = $post->ID;
    } else {
        $parent = end($ancestors);
    } 
    if(wp_list_pages("title_li=&child_of=$parent&echo=0" )) {                
        wp_list_pages("title_li=&child_of=$parent&depth=1" ); 
    } 
?>

You'll probably need to remove the depth parameters to show you're 3rd level pages.

Let me know if this helps!

0

Replace $parent in child_of argument of wp_list_pages by parent->ID. wp_list_pages needs the post id instead of post object.

<?php 
  global $post;
  $direct_parent = $post->post_parent;
  $parent = $direct_parent->post_parent;
  wp_list_pages( array(
   'child_of' => $parent,
   'title_li'     => false,
   'depth' => 1
 ) );
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.