我遇到了一个使用相同url的自定义分类法和自定义帖子类型的问题,现在已经解决了,但它创建了另一个bug。不显示使用自定义分类法标记的帖子。
我的帖子类型使用的url是/news,然后我有一个分类法,我需要有/news/cars
我可以获取自定义帖子类型的所有帖子,以便在索引模板上输出。
我还可以使用单个分类法页面,但我的查询不会显示使用自定义分类法标记的帖子,例如所有汽车帖子。
以下是我目前掌握的代码:
function register_custom_post_types() {
$args = array(
\'labels\' => array(
\'name\' => __( \'News\' ),
\'singular_name\' => __( \'News\' )
),
\'menu_position\' => 5,
\'public\' => true,
\'query_var\' => \'news\',
\'has_archive\' => \'news\',
\'hierarchical\' => true,
\'taxonomies\' => array(\'news_categories\'),
\'publicly_queryable\' => true,
\'capability_type\' => \'post\',
\'rewrite\' => array(\'with_front\' => false, \'slug\' => \'news/%news_categories%\'),
\'supports\' => array(\'title\', \'editor\', \'thumbnail\', \'revisions\', \'page-attributes\')
);
register_post_type( \'news\', $args);
}
add_action( \'init\', \'register_custom_post_types\' );
add_action( \'init\', \'create_my_taxonomies\', 0 );
function create_my_taxonomies() {
register_taxonomy(
\'news_categories\',
\'news\',
array(
\'labels\' => array(
\'name\' => \'News Categories\',
\'add_new_item\' => \'Add New Category\',
\'new_item_name\' => "New Category"
),
\'query_var\' => true,
\'show_ui\' => true,
\'show_tagcloud\' => false,
\'hierarchical\' => true,
\'rewrite\' => array(
\'with_front\' => false,
\'slug\' => \'news\'
)
)
);
}
add_filter(\'post_type_link\', \'news_term_permalink\', 10, 4);
function news_term_permalink($post_link, $post, $leavename, $sample) {
if ( false !== strpos( $post_link, \'%news_categories%\' ) ) {
$glossary_letter = get_the_terms( $post->ID, \'news_categories\' );
$post_link = str_replace( \'%news_categories%\', array_pop( $glossary_letter )->slug, $post_link );
}
return $post_link;