Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

By default wp_list_pages spits out an unordered list of post titles. To save space in a sidebar menu I´m trying to create a list of page slugs instead of page titles.

I suppose I´ll need to write a custom walker.. I was hoping someone can help me with an easier solution or a code snippet.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

I would recommend using get_pages() rather than wp_list_pages() for this purpose.

The get_pages() function will return an array of objects of Pages, and you can use the array data to build your own HTML list. e.g.:

<?php
// Wrap in a function, for later output;
function wpse47989_list_pages() {
    // Get an array of pages using default arguments
    // See Codex for list of arguments
    $wpse47989_pages = get_pages();

    // Open the unordered list
    $page_list = '<ul>';

    // Loop through the results, and output an HTML list
    foreach ( $wpse47989_pages as $wpse47989_page ) {
        // Open the list item
        $page_list .= '<li>';
        // Open the link
        $page_list .= '<a href="' . $wpse47989->permalink . '">';
        // Link anchor text (post slug)
        $page_list .= $wpse47989_page->post_name;
        // Close the link
        $page_list .= '</a>';
        // Close the list item
        $page_list .= '</li>';
    }

    // Close the unordered list
    $page_list .= '</ul>';
}
?>

Then, of course, you just output the function wherever you want to display the list. (Note: this would be a good candidate for a custom Widget. You could even pass the function get_pages() arguments as Widget options.)

You may need to manipulate the foreach output for styling/etc.

share|improve this answer
    
Thanks Chip, this works great. I made a minor change to display the permalink of the pages: $page_list .= '<a href="' . get_page_link( $wpse47989->ID ) . '">'; –  Mattvic Apr 5 '12 at 16:36
    
Glad it worked! –  Chip Bennett Apr 5 '12 at 16:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.