我正在尝试在自定义帖子类型的URL中显示类别。
示例:
website.com/custom-post-type/main-category/sub-category/post-name
当前,URL仅显示第一类。如何使所有类别显示在帖子永久链接中?
这是我到目前为止所拥有的
自定义帖子类型:
add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'system',
array(
'labels' => array(
'name' => __( 'Systems' ),
'singular_name' => __( 'System' )
),
'capability_type' => 'post',
'supports' => array('title','editor','comments'),
'public' => true,
'has_archive' => true,
'rewrite' => array( 'slug' => '/system/%category%' ),
'taxonomies' => array('category')
)
);
}
更改固定链接的功能
function url_category( $post_link, $id = 0 ){
$post = get_post($id);
if ( is_object( $post ) ){
$terms = wp_get_object_terms( $post->ID, 'category' );
$urlp = $terms[0]->slug;
if( $terms ){
return str_replace( '%category%', $urlp , $post_link );
}
}
return $post_link;
}
add_filter( 'post_type_link', 'url_category', 1, 3 );