这个wp_list_pages()
函数调用get_pages()
, 无法用其他post类型轻松覆盖。下面是对调用get_posts()
相反这与wp_list_pages, 还有一个:*post\\U type*(设置为您的post类型的名称)。
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;
}
注意:它的大部分副本都是从源粘贴的。当然,还有一些论点没有起到任何作用,可能还有一些我没有想到的用例会破坏它。然而,令人惊讶的是,它适用于分级和非分级帖子类型,尽管。。。