将自定义帖子类型分类显示为存档页面



您好,我有一个名为results的自定义帖子类型。我还使用分类法为该特定帖子类型创建了类别。我不确定我是否正确设置了它,但我拥有的代码有效,所以我坚持使用它。如果您看到更好的方法或任何错误,请告诉我。

我能够创建自定义帖子并为其设置类别。接下来,我想创建一个类别页面,该页面的作用类似于常规存档.php但仅适用于自定义帖子类型的类别。

因此,假设我有一个用于results的自定义帖子,并且我将其类别设置为car accidents我想要一种方法来显示它们,就像存档一样.php用于普通帖子。

我尝试访问这样的网址,但即使我有存档结果,我也被发送到 404 页面.php

www.myurl.com/results/categories/car-accidents

这是我用来设置自定义帖子类型和分类的代码。对不起,如果它很长,但我觉得有必要包括所有内容。

// Create custom post type
function create_posttype() {
register_post_type( 'Results',
array(
'labels' => array(
'name' => __( 'Results' ),
'singular_name' => __( 'Results' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'results'),
'taxonomies'  => array( 'results', 'result-category' ),
)
);
}
add_action( 'init', 'create_posttype' );
//Create category for specific post type
function tr_create_my_taxonomy() {
register_taxonomy(
'results-categories',
'results',
array(
'label' => __( 'Result Categories' ),
'rewrite' => array( 'slug' => 'result-category' ),
'hierarchical' => true,
'has_archive' => true
)
);
}
add_action( 'init', 'tr_create_my_taxonomy' );

我是否遗漏了阻止此 url 工作的内容?

www.myurl.com/results/categories/car-accidents

提前致谢

function namespace_add_custom_types( $query ) {
if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
$query->set( 'post_type', array(
'post', 'nav_menu_item', 'cmc-description'
));
return $query;
}
}
add_filter( 'pre_get_posts', 'namespace_add_custom_types' );
// Create custom post type
function create_posttype() {
register_post_type( 'Results',
array(
'labels' => array(
'name' => __( 'Results' ),
'singular_name' => __( 'Results' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'results'),
'taxonomies'  => array( 'results', 'result-category' ),
)
);
flush_rewrite_rules();
}
add_action( 'init', 'create_posttype' );
//Create category for specific post type
function tr_create_my_taxonomy() {
register_taxonomy(
'results-categories',
'results',
array(
'label' => __( 'Result Categories' ),
'rewrite' => array( 'slug' => 'result-category' ),
'hierarchical' => true,
'has_archive' => true
)
);
}
add_action( 'init', 'tr_create_my_taxonomy' );

我刚刚进行了此更改,您能否将其复制到您的代码中,看看它是否运行良好

最新更新