Forums

[resolved] List several pages from name, not id? (5 posts)

  1. yurini
    Member
    Posted 4 months ago #

    Hi,

    I need to list several specific pages from their names but dont know how to do it properly.

    The wp_list_pages command seems to only be accepting id´s when using the "include" parameter like:

    <?php wp_list_pages('sort_column=menu_order&title_li=&include=172,174,176,345' ); ?>

    And the get_page_by_title seems to only allow one page name when including, for example:

    <?php
    $page = get_page_by_title( 'About' );
    wp_list_pages( 'include=' . $page->ID );
    ?>

    How can I include several pages from their names? Something like this:

    <?php
    $page = get_page_by_title( 'About, Home, News' );
    wp_list_pages( 'include=' . $page->ID );
    ?>

    I want to have an answear how to do this. Even the reason doesn´t matter I can tell you why I need this and its because the page-id´s sometimes changes when you are exporting or moving the wordpress site. I know there are some ways to change the page id´s in the database, but I don´t wanna do that. There must be another way to do it simple and clean.

    Many thanks
    /Yurini

  2. alchymyth
    The Sweeper
    Posted 4 months ago #

    not very flexible, but should work:

    <?php
    $page1 = get_page_by_title( 'About' );
    $page2 = get_page_by_title( 'Home' );
    $page3 = get_page_by_title( 'News' );
    
    wp_list_pages( 'include=' . $page1->ID .','. $page2->ID .','. $page3->ID );
    ?>

    or slightly more automated:

    <?php $include_pages = array('About', 'Home', 'News' );
    $inc_str = array();
    foreach( $include_pages as $title ) {
    $page = get_page_by_title( $title );
    $inc_str[] = $page->ID; }
    $incl = implode(',', $inc_str);
    wp_list_pages( 'include=' . $incl ); ?>

    (untested)

  3. yurini
    Member
    Posted 4 months ago #

    Thank you, both methods worked!

    Alltho I get the message "Pages" getting echo:ed out on the row above the list. It did not show up before. Any idea why and how to remove it?

    My ul code now looks like:

    <ul id="nav3">
    <li <?php if(is_front_page()) { ?>class="current_page_item"<?php } ?>>
    </li>
    <?php
    $page1 = get_page_by_title( 'About' );
    $page2 = get_page_by_title( 'Home' );
    $page3 = get_page_by_title( 'News' );
    wp_list_pages( 'include=' . $page1->ID .','. $page2->ID .','. $page3->ID );
    ?>
    </ul>

    Thank you again
    /Yurini

  4. alchymyth
    The Sweeper
    Posted 4 months ago #

    Alltho I get the message "Pages" getting echo:ed

    read about 'title_li' in http://codex.wordpress.org/Function_Reference/wp_list_pages

  5. yurini
    Member
    Posted 4 months ago #

    Thanks again, problem solved!

    Added "title_li=" to the wp_list_pages parameter (set the value to empty or null) and the Pages list heading disapeard. The code now looks like:

    <ul id="nav3">
    <li <?php if(is_front_page()) { ?>class="current_page_item"<?php } ?>>
    </li>
    <?php
    $page1 = get_page_by_title( 'About' );
    $page2 = get_page_by_title( 'Home' );
    $page3 = get_page_by_title( 'News' );
    wp_list_pages( 'title_li=&include=' . $page1->ID .','. $page2->ID .','. $page3->ID );
    ?>
    </ul>

    Problem solved!

    This community is so friendly, its almost unbelivable! This was my first own question and post on this support forum and it was solved within an hour or two.

    Regards
    Yurini

Reply

You must log in to post.

About this Topic