WordPress.org

Ready to get started?Download WordPress

Forums

making child_of work with page slugs? (6 posts)

  1. abeolson
    Member
    Posted 5 years ago #

    Using WP 2.7, I'd like to generate a list of the children of the page with the slug='home'. I'm currently using:

    <ul class="legal-links">
    <?php wp_list_pages('title_li=&child_of=2'); ?>
     </ul>

    However, that only lets me get a list of the page with the ID=2, or whatever else the page_id may be. I'd rather generate the children based off the page slug, as that's a more flexible way to make a theme.

    Is this possible?

  2. MichaelH
    Member
    Posted 5 years ago #

    Don't think you are going to get that with just the template tag, wp_list_pages(). May need to get the page ID with something like get_page_by_title then call wp_list_pages with that ID.

  3. kdnewman
    Member
    Posted 5 years ago #

    This will work for the page title not the page slug.
    As far as I know there isn't an existing WP function to un-sanitize a slug although a simple str_replace would get you some way there:

    $titleslug = 'the-page-title';
    $unsanitized_titleslug = str_replace('-',' ',$titleslug);
    $pageinfo = get_page_by_title($unsanitized_titleslug);
    <ul>
    <?php wp_list_pages('title_li=&child_of='.$pageinfo->ID;); ?>
    </ul>
  4. kdnewman
    Member
    Posted 5 years ago #

    I've been working on a parallel problem and came up with a custom wpdb query to get the page ID from the page slug:

    $page_slug = 'the-page-slug';
    $sql = "SELECT * FROM " . $wpdb->posts . " WHERE post_name = '".$page_slug."' AND post_type = 'page' AND post_status='publish'";
    $row = $wpdb->get_row($sql);
    $page_id = $row->ID;

    and for the sake of completeness

    <ul>
    <?php wp_list_pages('title_li=&child_of='.$page_id); ?>
    </ul>
  5. Binh
    Member
    Posted 5 years ago #

    kdnewman,

    IT works. Sadly that's another SQL query to be ran...

    Did you find a better solution now?

  6. Binh
    Member
    Posted 5 years ago #

    Another method is to get page by path.

    $about = get_page_by_path('/about/');
    $aboutid = $about->ID;
    wp_list_pages("title_li=&amp;depth=1&amp;child_of=$aboutid");

    But it will create another query too, and much more complicated than what kdnewman gave. Complicated in term of the function get-page_by_path have a lot of code.

Topic Closed

This topic has been closed to new replies.

About this Topic