When wp_list_pages() can’t do it…
I just fixed up my projects page to use a custom template rather than free text editing. To do this, I wanted to use wp_list_pages(), however this does not give you the ability to easily add text inside the <li>, but outside the <a>.
My solution was to use get_posts(), and a custom loop instead. The code below is the main customisation in my new “List Child Pages” template.
<?php
global $post;
$myposts = get_posts('post_parent='.$post->ID.'&orderby=menu_order&post_type=page');
foreach($myposts as $post) : ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>: <?php echo get_post_meta($post->ID, descript, true) ?></li>
<?php endforeach; ?>
In this example, get_posts() is returning all posts (or confusingly, pages) that have a post parent of the current page (ie they are sub pages), ordered by the wordpress menu order, and of type “page”, as opposed to post. You can get your own attributes from the codex page.
Once get_posts() is set up, each post is echoed in a list. This is where the custom field gets added, using get_post_meta().
This solution is far simpler and neater then any method I saw of modifying wp_list_pages() to include a custom field. I don’t have much experience with custom loops, but I can’t see why there would be too many restrictions on where they can be used.
- Online
2 Responses
Thanks so much for posting this code. I’ve been looking for something like this for days. One question, though. One the blog I’m working on, it only pulls 5 subpages (there are about 15 total and if I unpublish one page, a new one fills it’s spot). Do you know why that would happen? Here’s the direct link to an example: http://www.tonybradley.com/security-basics/security-101-getting-started/ Any help or ideas greatly appreciated.
scratch that! Figured it out, thanks!