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.

I have a set of custom post types with the capability_type argument set to 'page'

I want to include these in wp_list_pages() or similar so I can use the dynamic classes (such as .current_page_item and the like).

I've read this post but I'm not sure it's exactly what I'm looking for, can anyone help me out with a code sample / more in-depth explanation.

share|improve this question
1  
I see that your answer accept rate is only 25%. You might get more answers if you take the effort to select a best answer on each of your past questions. Just a thought... –  MikeSchinkel Jan 22 '11 at 23:37
add comment

2 Answers

up vote 9 down vote accepted

The wp_list_pages() function calls get_pages(), which can't be easily overridden with a different post type. Here's a basic modification of that function which calls get_posts() instead. This takes basically the same arguments as wp_list_pages, with one additional: *post_type* (set as the name of your post type).

function wp_list_post_types( $args ) {
    $defaults = array(
        'numberposts'  => -1,
        'offset'       => 0,
        'orderby'      => 'menu_order, post_title',
        'order'      => 'ASC',
        'post_type'    => 'page',
        'depth'        => 0,
        'show_date'    => '',
        'date_format'  => get_option('date_format'),
        'child_of'     => 0,
        'exclude'      => '',
            'include'      => '',
        'title_li'     => __('Pages'),
        'echo'         => 1,
        'link_before'  => '',
        'link_after'   => '',
        'exclude_tree' => '' );

    $r = wp_parse_args( $args, $defaults );
    extract( $r, EXTR_SKIP );

    $output = '';
    $current_page = 0;

    // sanitize, mostly to keep spaces out
    $r['exclude'] = preg_replace('/[^0-9,]/', '', $r['exclude']);

    // Allow plugins to filter an array of excluded pages (but don't put a nullstring into the array)
    $exclude_array = ( $r['exclude'] ) ? explode(',', $r['exclude']) : array();
    $r['exclude'] = implode( ',', apply_filters('wp_list_post_types_excludes', $exclude_array) );

    // Query pages.
    $r['hierarchical'] = 0;
    $pages = get_posts($r);

    if ( !empty($pages) ) {
        if ( $r['title_li'] )
            $output .= '<li class="pagenav">' . $r['title_li'] . '<ul>';

        global $wp_query;
        if ( ($r['post_type'] == get_query_var('post_type')) || is_attachment() )
            $current_page = $wp_query->get_queried_object_id();
        $output .= walk_page_tree($pages, $r['depth'], $current_page, $r);

        if ( $r['title_li'] )
            $output .= '</ul></li>';
    }

    $output = apply_filters('wp_list_pages', $output, $r);

    if ( $r['echo'] )
        echo $output;
    else
        return $output;
}

Note: its mostly copy-pasted from source. There's certainly a few arguments left in there that aren't doing anything and there might well be some use cases I haven't thought of that would break it. It works, surprisingly, though, with both hierarchical and non-hierarchical post types, though...

share|improve this answer
    
Hi Goldenapples, this is great & displays the posts in the correct format, but doesn't pass the class current_page_item when you go to a page in the list. Any help would be really appreciated! –  George Wiscombe Oct 26 '10 at 8:25
2  
Ah... There was a test for is_page() left in there. I replaced it with if ($r['post_type'] == get_query_var('post_type')). It adds the correct 'current_page_item' class now. I edited the code above. –  goldenapples Oct 26 '10 at 16:29
add comment

@goldenapples: Excellent post! I was searching for this and got here. One thing I noticed is the orderby was not working properly, and got it fixed by adding the following line to the default arguments:

'order' => 'ASC',

Thanks, Camilo

share|improve this answer
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.