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 .

[tags]WordPress, hack, description, wp_list_pages, start_el, PHP, Walker, custom fields, pages, subpages, list of pages[/tags]

23 comments to Hack: WordPress function ‘wp_list_pages’ to output page description (or other custom fields) in your page lists

  • This is just great – love it!

  • Benjamin

    I have been looking for this function for 1 week and finaly found it here. Thanks a lot ;-)

  • Glad it helped you, benjamin..

  • If I’m trying to add a page description and keywords to a page to improve the site, is this what I should do?

  • @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.

  • This is perfect. Thank you very much.

  • Good work, i hope someone makes this into a plugin though. :)

  • 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

  • Sorted..
    Just changed
    $output .= “” .$description. “”;

  • 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! :)

  • 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

  • Для Вас сайт музыкальных альбомов,музыкальные сайты ucoz,шаблон +ради музыкального сайта,радио хит.

  • 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.

  • @Shane: Search for “custom fields”. You can use custom fields for what you want..

  • Graveto

    Hi,

    first of all I want to say this is a very usefull thing to know. Thank you!
    But…I’ve got a problem with the hack because I want to use it in my header.php to generate a dropdown-navigation which includes small discriptions.
    Everything works fine. The discription appears where it should.
    They are the smaller words are under the main menue points:

    http://annettes.alfahosting.org/MLI/

    But when I visit a subpage the discription disappears.

    Why does this happen?
    Has anyone a idea to solve this?

    Thank you…

  • Hey! thank you very much!

  • Hi,
    Could you help me pls. with displaying list of pages with at least 3-5 custom fields ? I’m almost for 14 days searching on internet, I have only found this article, but there is only one custom field:
    http://www.wprecipes.com/how-to-use-a-custom-blurb-when-listing-pages
    , I asked him for the help to add more fields but no answer .
    Is it hard to modify it, to add other fields , to implement it with your code?
    I have changed my wp-pages into girl’s portfolios so i need list of girls with the girl’s names(page title), country,province,city,age and one thumbnail image(custom field.
    Thanks a lot Daniel

  • @Daniel, it is pretty simple, you can use the get_post_meta function multiple times (as many times as the number of custom fields you need) and just change the custom field name given in the function’s arguments.

  • It’s not recommended to edit WP core files. If you wish to distribute your WP theme you have to attach the core files saying “hay guys, goto your WP installation folder and replace core WP files with this in oder to get the theme working”. If that poor guy was using an update version of WP then your file will replace it and :( :( who knows what will happen.

  • I am using the lates wp version and cant find the file u specified… Can u tell me where is that function defined in new version. I really need this….

  • [...] to wp_list_pages and found a couple of posts on how to achieve this. In the first post I found by Shantanu one had to hack classes.php, a WordPress core file, and the second one by Theme Shaper was lacking [...]

Leave a Reply

  

  

  

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

SUBSCRIBE!





Tweet