Skip to main content

This is rather usefull if you want to build a theme where you want to build a function for a specific page and the slug name will be the same ( although the ID wont be ).

Normally in wordpress when creating a page for the first time the slug will be the same for example if you create the pagename services, then the slug will be services. Lets say you want to list all sub pages of that page you will do a custom loop with query_posts.

Query_posts only have a parameter to get child pages of a id which is post_parent. so in order to use that with a page slug you will have to get the parent page id by using the slug. So add this function in your theme functions.php

<pre>function getIdFromSlug($pageSlug) {
    $page = get_page_by_path($pageSlug);
    if ($page) {
        return $page->ID;
    } else {
        return null;
    }
}</pre>

Now you can do a custom query like this:


$pageID = get_ID_by_slug('services');

query_posts( 'post_type=page&post_parent='.$pageID ); ?>

<?php if (have_posts()) : $count = 0; ?>

<?php while (have_posts()) : the_post(); $count++; ?>

// rest of loop goes here

Easy.. Hope this helped someone.

Janes Oosthuizen

Author Janes Oosthuizen

Programmer and Tech Junky from Cape Town, South-Africa. I have been programming for more than 15 years in various languages including ( CSS, HTML, javascript, PHP, MySQL, Wordpress and many other ).

More posts by Janes Oosthuizen

Leave a Reply