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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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…
Method 2
@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
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0