我的WordPress网站具有用于显示视频的自定义类别。 URL中需要显示一些子类别, 现在它显示site.com/video/video-page-name, 并需要显示它, 例如site.com/video/SUBCATEGORY/video-page-name
【在URL上设置子类别名称时出现问题】这是来自function.php的代码片段
add_action( 'init', 'create_post_type_video' );
function create_post_type_video(){
register_post_type( 'video', array(
'labels'=>
array(
'name'=>
__('?????? ????? ????? ????? ????', 'video'), 'singular_name'=>
__('Video', 'video'), 'add_new'=>
__('Add new', 'video'), 'add_new_item'=>
__('Add', 'video'), 'edit'=>
__('Edit', 'video'), 'edit_item'=>
__('Edit video', 'video'), 'new_item'=>
__('New video', 'video'), 'view'=>
__('View', 'video'), 'view_item'=>
__('View video', 'video'), 'search_items'=>
__('Search videos', 'video'), 'not_found'=>
__('Not found', 'video'), 'not_found_in_trash'=>
__('Not found in trash', 'video'), 'filter_items_list'=>
__('Filter videos list', 'video'), 'items_list_navigation' =>
__('Videos list navigation', 'video'), 'items_list'=>
__('Videos list', 'video'), 'insert_into_item'=>
__('Insert into video', 'video'), 'uploaded_to_this_item' =>
__('Uploaded to this video', 'video'), 'featured_image'=>
__('Featured Image', 'video'), 'set_featured_image'=>
__('Set featured image', 'video'), 'remove_featured_image' =>
__('Remove featured image', 'video'), 'menu_name'=>
__('???????', 'video'), 'name_admin_bar'=>
__('Video', 'video') ), 'menu_position'=>
7, 'description'=>
'Type of recording for video', 'menu_icon'=>
'dashicons-video-alt3', 'public'=>
true, //'publicly_queryable'=>
false, 'hierarchical'=>
false, 'has_archive'=>
false, 'supports'=>
array( 'title', 'editor', 'thumbnail' ), 'can_export'=>
false, 'taxonomies'=>
array( 'videocategory' )
));
}
add_action( 'init', 'create_taxonomy_videocategory' );
function create_taxonomy_videocategory() {
$labels = array(
'name'=>
'?????? ????? ????? ????? ????', 'singular_name'=>
'Videocategory', 'search_items'=>
'Search videocategories', 'all_items'=>
'All videocategories', 'parent_item'=>
'Parent videocategory', 'parent_item_colon'=>
'Parent videocategory', 'edit_item'=>
'Edit videocategory', 'update_item'=>
'Update videocategory', 'add_new_item'=>
'Add new videocategory', 'new_item_name'=>
'Name new videocategory', 'view_item'=>
'View videocategory', 'not_found'=>
'No videocategories found', 'no_terms'=>
'No videocategories', 'menu_name'=>
'Videocategories', 'items_list_navigation' =>
'Videocategories list navigation', 'items_list'=>
'Videocategories list', 'name_admin_bar'=>
'Videocategory', 'back_to_items'=>
'← Back to videocategories', 'popular_items'=>
'Popular videocategories'
);
$args = array(
'label'=>
'', 'labels'=>
$labels, 'public'=>
true, 'publicly_queryable'=>
true, 'show_in_nav_menus'=>
true, 'show_ui'=>
true, 'show_tagcloud'=>
true, 'hierarchical'=>
true, 'rewrite'=>
true, 'meta_box_cb'=>
null, 'show_admin_column'=>
true, '_builtin'=>
false, 'show_in_quick_edit'=>
true
);
register_taxonomy( 'videocategory', array( 'video' ), $args );
我在这里想念什么吗?
管理员的永久链接页面的屏幕截图也位于此处:https://prnt.sc/r9yy3c
#1因此, 你的register_taxonomy是好的, 但是你的register_post_type需要包含rewrite参数。
add_action( 'init', 'create_post_type_video' );
function create_post_type_video(){
register_post_type( 'video', array(
'labels'=>
array(
'name'=>
__('?????? ????? ????? ????? ????', 'video'), 'singular_name'=>
__('Video', 'video'), 'add_new'=>
__('Add new', 'video'), 'add_new_item'=>
__('Add', 'video'), 'edit'=>
__('Edit', 'video'), 'edit_item'=>
__('Edit video', 'video'), 'new_item'=>
__('New video', 'video'), 'view'=>
__('View', 'video'), 'view_item'=>
__('View video', 'video'), 'search_items'=>
__('Search videos', 'video'), 'not_found'=>
__('Not found', 'video'), 'not_found_in_trash'=>
__('Not found in trash', 'video'), 'filter_items_list'=>
__('Filter videos list', 'video'), 'items_list_navigation' =>
__('Videos list navigation', 'video'), 'items_list'=>
__('Videos list', 'video'), 'insert_into_item'=>
__('Insert into video', 'video'), 'uploaded_to_this_item' =>
__('Uploaded to this video', 'video'), 'featured_image'=>
__('Featured Image', 'video'), 'set_featured_image'=>
__('Set featured image', 'video'), 'remove_featured_image' =>
__('Remove featured image', 'video'), 'menu_name'=>
__('???????', 'video'), 'name_admin_bar'=>
__('Video', 'video') ), 'menu_position'=>
7, 'description'=>
'Type of recording for video', 'menu_icon'=>
'dashicons-video-alt3', 'public'=>
true, //'publicly_queryable'=>
false, 'hierarchical'=>
false, 'has_archive'=>
false, 'supports'=>
array( 'title', 'editor', 'thumbnail' ), 'can_export'=>
false, 'taxonomies'=>
array( 'videocategory' ), /* Add Rewrite and include your slug with replace defined below */
'rewrite'=>
array('slug' =>
'video/%videocategory%', 'with_front' =>
false), )
);
}
然后, 你需要过滤post_link和post_type_link, 以将%videocategory%替换为实际的类别名称。
/* Add Filter to declare your rewrite rule and find videocategory */
add_filter('post_link', 'my_category_permalink', 1, 3);
add_filter('post_type_link', 'my_category_permalink', 1, 3);
function my_category_permalink($permalink, $post_id, $leavename) {
// %category% to rewrite post type
if (strpos($permalink, '%videocategory%') === FALSE) return $permalink;
// Get post
$post = get_post($post_id);
if (!$post) return $permalink;
// Get taxonomy terms for your CPT Taxonomy
$terms = wp_get_object_terms($post->
ID, 'videocategory');
if (!is_wp_error($terms) &
&
!empty($terms) &
&
is_object($terms[0])){
$taxonomy_slug = $terms[0]->
slug;
}
else {
$taxonomy_slug = 'no-category';
}return str_replace('%videocategory%', $taxonomy_slug, $permalink);
}
推荐阅读
- 如何从WordPress博客文章页面删除评论和作者图标
- Linux From Scratch(LFS11.0)构建 LFS 系统 - Util-linux-2.37.2
- Linux 一键配置 Oracle 主机 /dev/shm
- MAC环境变量的配置小笔记
- Linux From Scratch(LFS11.0)构建 LFS 系统 - Man-DB-2.9.4
- Linux From Scratch(LFS11.0)构建 LFS 系统 - Procps-ng-3.3.17
- Linux From Scratch(LFS11.0)构建 LFS 系统 - Bash-5.1.8
- 使用Hi3861驱动交流电机变频器方法简介
- Linux From Scratch(LFS11.0)构建 LFS 系统 - Sed-4.8