Hack: WordPress function ‘wp_list_pages’ to output page description (or other custom fields) in your page lists
----
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
.
----
If you liked this post, then you can Subscribe to my feed










This post has 9 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. "";
Trackbacks
Add a comment