在URL上设置子类别名称的问题

问题描述 投票:0回答:1

我的WordPress网站具有用于显示视频的自定义类别构建。URL中需要显示一些子类别。现在它显示site.com/video/video-page-name,并需要显示它,例如site.com/video/SUBCATEGORY/video-page-name

这是来自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

wordpress url wordpress-theming permalinks custom-wordpress-pages
1个回答
0
投票

所以您的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_linkpost_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);
}
© www.soinside.com 2019 - 2024. All rights reserved.