【发布时间】:2016-04-14 02:24:22
【问题描述】:
我在查看某个类别中的所有帖子时遇到问题。
我在functions.php中创建了一个自定义帖子和类别,如下所示:
function create_posttype() {
register_post_type( 'tours',
// CPT Options
array(
'labels' => array(
'name' => __( 'Tours' ),
'singular_name' => __( 'Tour' )
),
'public' => true,
//'has_archive' => true,
'rewrite' => array('slug' => 'tours'),
//'taxonomies' => array('category'), //A added
'hierarchical' => true //A added
)
);
}
// Hooking up our function to theme setup
add_action( 'init', 'create_posttype' );
function my_taxonomies_tours() {
$labels = array(
'name' => _x( 'Tour Categories', 'taxonomy general name' ),
'singular_name' => _x( 'Tour Category', 'taxonomy singular name' ),
'search_items' => __( 'Search Tour Categories' ),
'all_items' => __( 'All Tour Categories' ),
'parent_item' => __( 'Parent Tour Category' ),
'parent_item_colon' => __( 'Parent Tour Category:' ),
'edit_item' => __( 'Edit Tour Category' ),
'update_item' => __( 'Update Tour Category' ),
'add_new_item' => __( 'Add New Tour Category' ),
'new_item_name' => __( 'New Tour Category' ),
'menu_name' => __( 'Tour Categories' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
);
register_taxonomy( 'tour_category', 'tours', $args );
}
add_action( 'init', 'my_taxonomies_tours', 0 );
我创建了一个模板来显示所有类别,希望您可以单击一个类别并让它列出所有帖子。在我的模板中,我有以下内容:
$custom_terms = get_terms('tour_category');
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array('post_type' => 'tours',
'tax_query' => array(
array(
'taxonomy' => 'tour_category',
'field' => 'slug',
'terms' => $custom_term->slug,
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
echo '<h2>'.$custom_term->name.'</h2>';
}
}
我目前列出的类别,但我不知道如何以这种方式链接它,以便您可以查看该特定类别中的所有帖子。任何帮助将不胜感激。 使用 Wordpress 4.4
【问题讨论】: