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 /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:

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.
  1. 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:

And, modify the previously listed code as well to look like:

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 .

See also