如何将自定义分类法类别中的所有帖子列入自定义模板?以下是代码。
所以我创建了一个这样的自定义分类:
register_taxonomy('miniblog_category', 'post', array(
// Hierarchical taxonomy (like categories)
'hierarchical' => true,
// This array of options controls the labels displayed in the WordPress Admin UI
'labels' => array(
'name' => _x( 'MiniBlog Categories', 'taxonomy general name' ),
'singular_name' => _x( 'MiniBlog Category', 'taxonomy singular name' ),
'search_items' => __( 'Search MiniBlog Categories' ),
'all_items' => __( 'All MiniBlog Categories' ),
'parent_item' => __( 'Parent MiniBlog Category' ),
'parent_item_colon' => __( 'Parent MiniBlog Category:' ),
'edit_item' => __( 'Edit MiniBlog Category' ),
'update_item' => __( 'Update MiniBlog Category' ),
'add_new_item' => __( 'Add New MiniBlog Category' ),
'new_item_name' => __( 'New MiniBlog Category Name' ),
'menu_name' => __( 'MiniBlog Categories' ),
),
// Control the slugs used for this taxonomy
'rewrite' => array(
'slug' => 'miniblog',
'with_front' => false,
),
));
之后我还创建了一个显示在作者页面(右侧边栏)上的自定义小部件。此小组件显示所有当前作者的类别以及帖子编号(下面的代码):
...
$current_author_id = get_the_author_meta('ID');
$categories_by_post_no = array();
foreach (get_terms('miniblog_category') as $mbc) {
// metadata term( blogger_id )
$taxonomy_blogger_id = get_term_meta($mbc->term_id, 'blogger_id', true) ;
if ($taxonomy_blogger_id == $current_author_id) {
$arg_author_posts = array(
'post_type' => 'post',
'post_status' => 'published',
'miniblog_category' => $mbc->slug,
'numberposts' => -1,
);
$term_link = get_term_link($mbc->term_id, $mbc->slug);
$categories_by_post_no[] = array(
'name' => $mbc->name,
'post_count' => count( get_posts( $arg_author_posts ) ),
'term_link' => get_term_link($mbc),
);
}
}
if (! empty($categories_by_post_no) ) {
echo '<ul class="menu">';
foreach ($categories_by_post_no as $cbpn) {
echo "<li><a href='" . $cbpn['term_link'] . "'>$cbpn[name] ($cbpn[post_count])<a/></li>";
};
echo'</ul>';
} else {
echo '<span>No articles!</span>';
}
...
在我的情况下,链接($ cbpn [&#39; term_link&#39;])是这样的: - 迷你日志/文化 - 迷你日志/性质 - 迷你日志/南美洲 ... 但是当我点击这些链接时,我收到了404未找到的错误。 我试图在主题文件夹中创建这样的文件(taxonomy-miniblog.php,taxonomy-miniblog-category.php),但它不起作用。我是否必须为每个类别创建文件,例如taxonomy-miniblog-culture.php,taxonomy-miniblog-nature.php ...(没办法!!)?