如果您想要像/新闻这样的存档页面,需要在像/新闻/最新这样的存档页面和像/新闻/最新/%postname%这样的发布详细信息页面之后使用自定义slug,下面是帮助我的代码。。
将此代码添加到函数。php
add_action( \'init\', \'create_news\' );
function create_news() {
register_post_type( \'news\',
array(
\'labels\' => array(
\'name\' => \'News\',
\'singular_name\' => \'News\',
),
\'taxonomies\' => array( \'category\'),
\'rewrite\' => array( \'slug\' => \'%latest_tag%\', \'with_front\' => false ),
\'has_archive\' => \'news\'
)
);
register_taxonomy(
\'latest_tag\',
\'news\',
array(
\'rewrite\' => array( \'slug\' => \'latest\', \'with_front\' => false ),
)
);
}
之后,将下面的代码放入相同的函数中。php。。
function custom_slug_permalinks( $post_link, $post ){
if ( is_object( $post ) && $post->post_type == \'news\' ){
$terms = wp_get_object_terms( $post->ID, \'state_tag\' );
if( $terms ){
return str_replace( \'%latest_tag%\' , $terms[0]->slug , $post_link );
}
}
return $post_link;
}
add_filter( \'post_type_link\', \'custom_slug_permalinks\', 1, 2 );
没有必要将“has\\u archive”添加为true,相反,您可以在那里使用自定义的post slug名称,wordpress将转到archive。php。。在那之后,
Do not forget to go settings->permalinks and click on save button :)