Проблема с настройкой имени подкатегории в URL - PullRequest
0 голосов
/ 01 марта 2020

Мой сайт WordPress имеет собственную категорию для показа видео. Есть несколько подкатегорий, которые должны быть показаны в URL. Теперь он показывает site.com/video/video-page-name и должен показывать его как site.com/video/SUBCATEGORY/video-page-name

Вот фрагмент кода из функции. 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 Ответ

1 голос
/ 02 марта 2020

Так что ваш 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);
}
...