Udders
Udders

Reputation: 6976

wp_list_pages include pages and show children as a dropdown

I am using wordpress for a project, and I am struggling to get the nav to show the pages I only request in the wp_list_pages function, I am wanting to only show 5 pages in my main mav, and then if that page has any children then show those in a dropdown, below is the code that I am currently using.

<?php wp_list_pages('title_li=&sort_column=post_date&include=138,110,135,101,167'); ?>

How do I show the children of the included pages?

Upvotes: 1

Views: 3115

Answers (1)

Mark Nguyen
Mark Nguyen

Reputation: 7448

I find what works best for me in these situations is to forget about using wp_list pages. Instead, make a query and then iterate through the results to get page children.

Example:

<ul>
<?php 
    $args = array(
        'include'  => array(138, 110, 135, 101, 167),
        'orderby' => 'post_date',
        'post_type'=> 'page',
    );

    /* Get posts according to arguments defined above */
    $pages = get_posts($args);

    echo "<ul>";

    /* Loop through the array returned by get_posts() */
    foreach ($pages as $page) {

        /* Grab the page id */
        $pageId = $page->ID;

        /* Get page title */
        $title = $page->post_title;
        echo "<li>$title</li>";         

        /* Use page id to list child pages */
        wp_list_pages("title_li=&child_of=$pageId" );

        /* Hint: get_posts() returns a lot more that just title and page id. Uncomment following 3 lines to see what else is returned: */
        //echo "<pre>";
        //print_r($page);
        //echo "</pre>";
    } 
    echo "</ul>";
?>
</ul>

And your output should look something like:

<ul>
    <li>Parent Page1<li>

    <ul>
        <li>Child page1</li>
        <li>Child page2</li>
        <li>Child page etc</li>
    </ul>

    <li>Parent Page2</li>

    <ul>
        <li>Child page1</li>
        <li>Child page2</li>
        <li>Child page etc</li>
    </ul>

    ...and so forth
</ul>

Upvotes: 1

Related Questions