Wordpress没有分类法的主要负责人,只有术语的存档。
E、 例如,您可以获得urlexample.com/series/series-about-fish
thta显示该系列中的所有帖子,但no 存档文件example.com/series/
显示所有系列中的帖子。
实现这一点的最简单方法是创建page template, 使用slug在仪表板中创建页面series
并指定模板,然后在模板文件中创建一个使用自定义WP_Query
使用正确的taxonomy\\u查询参数。。
如果每个帖子只属于one serie,写起来很简单a custom function that return a multidimensional array of posts, where every first level item is an array with 2 elements: one keysed \'term\' contain the series term object, the second keyed \'posts\' contain all the posts for that series:
在里面functions.php
放置类似以下内容:
function get_series_posts() {
$terms = get_terms(\'series\', array(\'fields\'=>\'ids\') );
$tax_query = array(
\'taxonomy\' => \'series\',
\'field\' => \'id\',
\'terms\' => $terms,
);
$args = array(\'posts_per_page\' => -1, \'tax_query\' => array($tax_query));
$q = new WP_Query( $args );
$posts_ordered = array();
$done = 0;
while ( $q->have_posts() ) : $q->the_post();
global $post;
// all the post series array
$series = get_the_terms($post->ID, \'series\');
// the last series (if there is only one series per post no matter the order)
$serie = array_shift( $series );
if ( ! isset($posts_ordered[$serie->slug]) )
$posts_ordered[$serie->slug] = array(\'term\' => $serie);
if ( ! isset($posts_ordered[$serie->slug][\'posts\']) )
$posts_ordered[$serie->slug][\'posts\'] = array();
$posts_ordered[$serie->slug][\'posts\'][] = $post;
endwhile;
wp_reset_postdata();
return $posts_ordered;
}
之后,您可以在页面模板中执行以下操作:
$posts = get_series_posts();
if ( ! empty($posts) ) {
echo \'<ul>\'; // open series list
foreach ( $posts as $loop_data) {
echo \'<li>\'; // open series list item
echo \'<a href="\' . get_term_link($loop_data[\'term\'], \'series\') . \'">\';
echo $loop_data[\'term\']->name . \'</a>\';
echo \'<ul>\'; // open post list
global $post;
foreach ( $loop_data[\'posts\'] as $post) { // loop throug posts of a series
setup_postdata($post);
printf(\'<li><a href="%s">%s</a></li>\', get_permalink(), get_the_title() );
}
echo \'</ul>\'; // close post list;
wp_reset_postdata();
echo \'</li>\'; // close series list item
}
echo \'</ul>\'; // close series list
}