Support » Fixing WordPress » making child_of work with page slugs?

  • 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'); ?>

    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?

Viewing 5 replies - 1 through 5 (of 5 total)
  • 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.

    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);
    <?php wp_list_pages('title_li=&child_of='.$pageinfo->ID;); ?>

    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

    <?php wp_list_pages('title_li=&child_of='.$page_id); ?>


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

    Did you find a better solution now?

    Another method is to get page by path.

    $about = get_page_by_path('/about/');
    $aboutid = $about->ID;

    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.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘making child_of work with page slugs?’ is closed to new replies.