1

I have a much larger problem I posted earlier (still unanswered) which I might be able to solve if I can find a way to target the grandchild (or third level post) of a parent post.

Right now, this code:

$Pages = wp_list_pages('child_of='.($post->post_parent != false ? $post->post_parent : $post->ID).'&title_li=&echo=0')

echo $Pages;

Will show 2nd and 3rd level pages (bold=visible in submenu).

  • Home
  • Parent (active page)
    • Child
      • Grandchild

I need to find a way to preform a str_replaceon the grandchild.

I know how to target the child, but basically need to do the exact same thing for the grandchildren.

$Pages = wp_list_pages('child_of='.($post->post_parent != false ? $post->post_parent : $post->ID).'&title_li=&echo=0')

$Pages = str_replace($Pages,
                     '<ul id="test">'.$Pages.'</ul>',
                     $Pages);
echo $Pages;
unset($Pages);
0

The correct answer is:

$Pages      = wp_list_pages('child_of='.($post->post_parent != false ? $post->post_parent : $post->ID).'&title_li=&echo=0&depth=1');
            $InnerPages = wp_list_pages('child_of='.($post->post_child != false ? $post->post_child : $post->ID).'&title_li=&echo=0');
            $Title      = ($post->post_child != false) ? trim(get_the_title($post->post_child)) : trim(wp_title('', false));
            if($Title != '')
              $Pages      = str_replace($Title.'</a></li>',
                                        $Title.'</a>'.
                                        '<ul id="test"><li>Overview</li>'.$InnerPages.'</ul></li>',
                                        $Pages);
            echo $Pages;
            unset($Pages, $InnerPages);

HOWEVER, I have found a much better solution to accomplish what I was trying to do: How to add active class to custom menu using while loop and wp_list_pages

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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