1

I am looking to use the wp_list_pages function to show children pages of a specific page ID. I was able to do this easily, but now I found an issue with it. I am running a Wordpress network of sites (total of 8). I am trying to set this up where I will only need 2 theme folders because there are 2 main blogs with 3 sub sites that fall under each. So, I am trying to make as many dynamic calls in my theme files as possible to prevent duplicating themes for each site.

In my sidebar I am using the following code:

<ul>
<?
    wp_list_pages('orderby=name&depth=1&order=DESC&show_count=0&child_of=10&title_li=');
?>
</ul>

The issue I am running in to is, on each of the sub sites the page ID's have changed but my sitemap for the most part has not. So on my main site child_of=10 displays the proper sub pages in my sidebar, but in my sub-sites the child_of=10 is a different page so I am not getting the proper child pages to display. I had a similar issue with pulling in posts but I was able to do the following:

<?
    $category = get_category_by_slug('news');
    wp_list_categories('orderby=name&order=DESC&show_count=0&exclude=53&title_li=&child_of='.$category->term_id);
?>

I am trying to achieve a similar solution for wp_list_pages where I use a page slug to identify the child pages instead of the page ID. Been digging around and trying a few posts I have found on Google / here but no luck. Hoping someone can help me out.

1 Answer 1

1

Why don't you use get_page_by_title() to get the page object, and then pass its ID as the child_of parameter?

If you would rather use the actual slug, then get_page_by_path( $slug ) should do the trick.

So:

if ( $page = get_page_by_path( 'your-page-slug' ) ){
  wp_list_pages( 'orderby=name&depth=1&order=DESC&show_count=0&child_of=' .$page->ID . '&title_li=' );
}
6
  • Awesome that was exactly what I was looking for. Now if you don't mind me asking you 1 more question related to this that just came up. I am running this code on a Contact page, and I am telling it to pull the child pages of another section. It is doing that now with the snippet you provided but its also pulling the sub pages of the Contact page and I don't want that. Is their a more dynamic way to use the exclude function within wp_list_pages to not show children pages of current page that you are on?
    – Danny
    Aug 15, 2012 at 18:26
  • If I understand your question, you only want direct children of the parent page shown, right? So instead of child_of, which lists all descendants, use parent=, which only gets immediate children. codex.wordpress.org/Function_Reference/get_pages#Parameters
    – Tom Auger
    Aug 15, 2012 at 19:32
  • What I am looking to do is show specific child pages of another page. In this example I want to display children pages of the main parent "Services" page on my "Contact Us" page. I also in the back-end of Wordpress have child pages listed under "Contact Us". When you view the front-end of the Contact Us page I only want the child pages of "Services" to show up and not display the child pages at all of the "Contact Us" page. Right now in my sidebar file I have the following:
    – Danny
    Aug 16, 2012 at 14:28
  • Right now I have: ` <?php } elseif (is_page('58')) { ?> <!--Contact Us Page--> <h4><span>Practice Areas</span></h4> <ul> <? if ($page = get_page_by_path('practice-areas')){ wp_list_pages('orderby=name&depth=1&order=DESC&show_count=0&child_of='.$page->ID.'&title_li='); } ?> </ul> <?php } else { ?> `
    – Danny
    Aug 16, 2012 at 14:28
  • Hey @Danny, do you mind updating your original question with the code you pasted into this comment? And of course, make sure you put it inside a SE code block, so it's nice and easy to read.
    – Tom Auger
    Aug 16, 2012 at 18:08

Your Answer

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

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