Hack: WordPress function ‘wp_list_pages’ to output page description (or other custom fields) in your page lists
Blog | Tech Blog | Secure Coding | Twitter | RSS Feed | Get Email Updates
Disclaimer: I’m neither a WordPress guru nor a PHP expert. This is what it is, a dirtly little hack, otherwise I’d have called it an “enhancement” 
Now onto the main things.
Aim: You would have used wp_list_pages (or plugins like dd-list-subpages, that use it) to display a list of your pages/subpages on a particular page. e.g., I use it on my “Projects” pages hierarchy to list all the relevant projects under a particular heading. Now, the thing is that wp_list_pages gives you a lot of options to display things like dates, page title, page link, etc, but that’s not enough. I need to provide a small description for each page as well, to put things into perspective. Obviously, I don’t expect the visitors to go inside each link to see what it holds in store for them.
The Hack: What you need to do is locate a file called “classes.php” in your wordpress installation. It is usually located at <WordPress Base>/wp-includes.
Now, open the file and find a function called “start_el”. This is a function in the “Walker” class, which is used in WordPress to parse tree-like structures. Here, it used by wp_list_pages to list out all the pages. Now, at the very end of this function (just after the closing brace of if ( !empty($show_date) ) { ), place the following piece of code:
-
$description = get_post_meta($page->ID, "description", true);
-
if ($page->ID != $current_page)
-
{
-
$output .= " ";
-
if (!$description)
-
{
-
$description = strip_tags(substr($page->post_content, 0, 250));
-
}
-
$output .= $description;
-
}!
What this code does is:
1) Look for a custom field called “description” in your pages. If present, it will show what you wrote there after each of your page in the list generated by wp_list_pages. (To add a custom field to your page, look at the very bottom of your “write” page or “manage” page in the WordPress dashboard.
2) If the “description” field is not present (obviously you might not like to go back and add a custom field to all your pages), it takes the first 250 characters of your page’s content and displays that.
The Hack Is Not Finished Yet: There is just one little thing left to do. As I mentioned, this function is used other times as well (e.g. making your navigation menu). So, you don’t want the description to be appearing always, otherwise it will wreak havoc on your site’s layout. So, there is again a dirty little trick to prevent this.
At the place where you are calling wp_list_pages, modify the call to include the following code before and after the call:
-
update_option('my_wp_list_pages_option', 1);
-
$content .= wp_list_pages($your_wp_list_pages_options);
-
update_option('my_wp_list_pages_option', 0);
And, modify the previously listed code as well to look like:
-
$my_wp_list_pages_option = get_option('my_wp_list_pages_option');
-
-
if ($my_wp_list_pages_option == 1)
-
{
-
$description = get_post_meta($page->ID, "description", true);
-
if ($page->ID != $current_page)
-
{
-
$output .= " ";
-
if (!$description)
-
{
-
$description = strip_tags(substr($page->post_content, 0, 250));
-
}
-
$output .= $description;
-
}
-
}
What this does now is that it adds an option/variable to WordPress database, which you set to 1 before calling wp_list_pages to tell your code that now its time to display the description, and then restore its state to 0, to prevent the description from being displayed for any other wp_list_pages call.
For your reference, I'm attaching my copy of classes.php here.File Attachment: classes.php.zip (6 KB).
For an example of how it finally looks like, take a look at my “Project” page.
Now It Is Finished: Yes, am not lying. It’s done. All your “Thank You’s” are accepted, and so are your flames, if any
.
© Shantanu Goel | Hack: WordPress function ‘wp_list_pages’ to output page description (or other custom fields) in your page lists
|
Liked this post? Get FREE Updates Subscribe to RSS feed |






This post has 14 comments
April 17th, 2008
This is just great - love it!
May 13th, 2008
I have been looking for this function for 1 week and finaly found it here. Thanks a lot
May 13th, 2008
Glad it helped you, benjamin..
June 23rd, 2008
If I'm trying to add a page description and keywords to a page to improve the site, is this what I should do?
June 24th, 2008
@Tom: The method described here is for the benefit of the "human" visitors wanting to see a comprehensive list. I guess what you want is from a search engine perspective. For that you need to look at "meta" tags. You can install the "All in one SEO" plugin (from wordpress extend repository), it'll allow you to do that automatically. Let me know if you need more help regarding that.
June 27th, 2008
This is perfect. Thank you very much.
July 16th, 2008
Good work, i hope someone makes this into a plugin though.
August 26th, 2008
Hi,
Nice hack, would it be possible to customise this in such a way to get the description to output as you have indicated but to output like this ..
Page Linkdescrition
... and so on
Thanks
August 26th, 2008
Sorted..
Just changed
$output .= "" .$description. "";
February 17th, 2009
great stuff.
I just modified it a bit, so that you can control which meta var to pull in, directly from your post/page.
instead of passing a boolean to start_el to tell it whether to look for "description", you can simply pass it the name of the meta var:
update_option('get_meta', "Location");
Then in the start_el you could do something like this:
$get_meta = get_option("get_meta");
if($get_meta!=""){
$meta_var = get_post_meta($page->ID, "$get_meta", true);
if ($page->ID != $current_page){
$output .= " ";
if (!$meta_var){
$meta_var = strip_tags(substr($page->post_content, 0, 250));
}
$output .= $meta_var;
}
}
it's all I need at the moment (more than 1 set of meta vars) but this could be extended to accept an array of elements... Then it would be ready to be made into a plugin!
March 25th, 2009
Hi,
Can I use the field I grab from META to sort the pages? In my case the meta tag is price and I would like to sort my pages by the price.
Thanks
June 28th, 2009
Для Вас сайт музыкальных альбомов,музыкальные сайты ucoz,шаблон +ради музыкального сайта,радио хит.
June 29th, 2009
Hi, just exactly what i was looking for. this works great. The only thing i have changed so far is amend the output with span's with a class name for full CSS control. Could anyone please provide me with an example of how i can extend this in case i wanted to have more than one description ? some kind of loop ? my PHP is not very good
any help would be great.
June 30th, 2009
@Shane: Search for "custom fields". You can use custom fields for what you want..
Trackbacks