Monday, March 11, 2013

Custom listing of pages in wordpress

There are several ways to customize which pages are listed in a directory. The simplest way to do this is by using either ‘include’ or ‘exclude’ but these are far from flexible if your content is constantly changing these would have to be modified each time a new page is added. All of these examples use the wp_list_pages.


    Including pages:
    This only includes pages listed.
    wp_list_pages(“title_li&include=10,19,21,24,26,51″);
    //IMPORTANT-> The numbers are the page ids to be included.
    Exluding pages:
    This method includes all pages not listed in the function.
    wp_list_pages(“title_li&exclude=55,51″);
    //IMPORTANT-> The numbers are the page ids to be excluded.
    Using Custom Fields:
    The best way is to use a key/value pair and the defined array values from wordpress. It’s a bit more complicated but once you understand it you’re off and running. It uses a custom field to limit the pages listed. Here only pages with the custom field ‘directory’ where the value = ’1′ (‘meta_key’=>’directory’,'meta_value’=>’1′) will appear.
    NOTE: It’s possible to organize the listings by adding multiple custom fields to the same page. Here are the required steps:

        Create a custom field to be used as the selector for the pages to be listed:(See adding custom fields on the wordpress.org site for full instructions).
        Add the custom field to the page:
        Edit the page and add the custom field from the drop down if it’s already been created OR create it on that page if it doesn’t already exist. The field name and value can be anything you’d like.
        This example uses ‘directory’ as the field name a value = ’1′ if it’s to be added.
        ( ‘meta_key’=>’directory’,'meta_value’=>’1′)
        NOTE: Custom fields in a wordpress array are referred to as ‘meta_key’ and their values are referred to as ‘meta_value

            PART 1: Create the key/value pair.
            $args = array(‘meta_key’=>’directory’,'meta_value’=>’1′)


            PART 2: Use the array as the argument in wp_list_pages
            wp_list_pages( $args );

            Putting it all together: (don’t forget the php tags! )

            <?php
                $args = array(‘meta_key’=>’directory’,'meta_value’=>’1′);
                wp_list_pages( $args );
            ?>

One final note:

This array isn’t a ‘self defined’ array that you make up! It’s actually part of the wordpress functionality.  There are other options available.  One of the most frequently asked questions is how do I remove the title above the listing?  (In this case, the first line would show ‘pages’).

Most coders try to add it as part of the wp_list_pages method as you would with the exclude and include arguments.  With the array arguments, it’s actually a part of the array options!
This is the code to remove the title:

<?php
    $args = array(‘meta_key’=>’directory’,'meta_value’=>’1′, ‘title_li’=>”)
    wp_list_pages( $args );
?>

Notice that the value for title_li is empty! (‘title_li’=>”)

View the full list of the options available on the wordpress site at Template Tags / wp_list_pages

0 comments:

Post a Comment