0
我在查看類別中的所有帖子時遇到問題。無法查看類別中的所有帖子 - Wordpress
我創建了一個自定義後和類別與它走在了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