A web developer's blog. PHP, MySQL, CakePHP, Zend Framework, Wordpress, Code Igniter, Django, Python, CSS, Javascript, jQuery, Knockout.js, and other web development topics.

WordPress: Get sub-pages / children of Page

I have been busy lately making some extra money doing side-jobs with WordPress. In WordPress, you will be able to create a “sub-page” by selecting a “Parent”. This tip is useful when you want to retrieve all the sub-pages. You should be able to use this code anywhere in your template files.

<?php 
global $post;
$args = array(
    'title_li'     => 'More Under This Page',
    'child_of'     => $post->ID
);
$children =  get_pages($args);
if($children):
?>
<div class="subpages">
	<ul>
	 <?php
	 wp_list_pages($args);
	 ?> 
	</ul>
</div>
<?php
endif;
?>

If your page has sub-pages / children under it, then it should display this HTML:

<div class="subpages">
<ul>
    <li class="pagenav">
    More Under This Page
        <ul>
            <li class="page_item page-item-46">
            Subpage 1
            </li>
            <li class="page_item page-item-49">
            Subpage 2
            </li>
        </ul>
    </li>
</ul>
</div>
This entry was posted in General and tagged , , , , . Bookmark the permalink.

4 Responses to WordPress: Get sub-pages / children of Page

  1. Alex says:

    thanks dude… helped me a lot!

  2. Maggie Brown says:

    Thanks for the tip on sub pages. I need to create one and will give it a try. I really struggle to find good info. THANKS! Mags @ Child Safety

  3. Pingback: What Is The Best Seo Tactics For The Subpages Of The Site? | freearticlesdbonline.com

  4. Hi Wenbert,
    thank you very much for this code, I was actualy looking for one for a while. It really is helpfull in creation of a right side category menu. You are the man!!

Leave a Reply to Nathan @ Website Designers Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>