Измените заголовок настраиваемой таксономии с помощью заголовка CPT - PullRequest
0 голосов
/ 14 июля 2020

Привет, пожалуйста, проверьте мои требования ниже.

Прежде всего, я зарегистрировал "каталог" CPT с именем ярлыка "каталог". Кроме того, я зарегистрировал настраиваемую таксономию «business_category». Я хочу представить структуру постоянных ссылок ниже.

  1. Ссылка на архив CPT: www.domain.com/directory/
  2. Ссылка на таксономию: www.domain.com/directory/category/TAXONOMY_NAME/
  3. CPT Single Page: www.domain.com/directory/POST_NAME

Итак, я использовал приведенный ниже код.

public function __construct( $plugin_name, $version ) {

    $this->plugin_name = $plugin_name;
    $this->version = $version;
    $this->PT = 'cc-directory';
    $this->name = 'Directory';
    $this->singular_name = 'Directory';
    $this->slug = 'directory';

}

public function register_post_type() {

     // Get supported features for Directory post type
     $supports = apply_filters('cc_directory_supports', array('editor', 'title','thumbnail'));

     $labels = array(
        'name'                  => $this->name,
        'singular_name'         => $this->singular_name,
        'add_new'               => 'Add New',
        'add_new_item'          => 'Add New '   . $this->singular_name,
        'edit_item'             => 'Edit '      . $this->singular_name,
        'new_item'              => 'New '       . $this->singular_name,
        'all_items'             => 'All '       . $this->name,
        'view_item'             => 'View '      . $this->name,
        'search_items'          => 'Search '    . $this->name,
        'not_found'             => 'No '        . strtolower($this->name) . ' found',
        'not_found_in_trash'    => 'No '        . strtolower($this->name) . ' found in Trash',
        'parent_item_colon'     => '',
        'menu_name'             => $this->name
    );
    
    $args = array(
        'labels'                => $labels,
        'public'                => true,
        'publicly_queryable'    => true,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'query_var'             => true,
        'rewrite'               => array( 'slug' => $this->slug ),
        'capability_type'       => 'post',
        'has_archive'           => true,
        'hierarchical'          => true,
        'menu_position'         => 11,
        'menu_icon'             => 'dashicons-book',
        'supports'              => $supports,
        'yarpp_support'         => true
    );

    register_post_type( $this->PT, $args );

    $plural_label = 'Categories';
    $singular_label = 'Category';

    register_taxonomy(
        'business_category',
        $this->PT,
        array(
            'label'                 => $plural_label,
            'labels'                => array(
                'name'              => $plural_label,
                'singular_name'     => $singular_label,
                'all_items'         => sprintf(__('All %s', 'claritycloud-directory'), $plural_label),
                'edit_item'         => sprintf(__('Edit %s', 'claritycloud-directory'), $singular_label),
                'view_item'         => sprintf(__('View %s', 'claritycloud-directory'), $singular_label),
                'update_item'       => sprintf(__('Update %s', 'claritycloud-directory'), $singular_label),
                'add_new_item'      => sprintf(__('Add New %s', 'claritycloud-directory'), $singular_label),
                'new_item_name'     => sprintf(__('New %s Name', 'claritycloud-directory'), $singular_label),
                'popular_items'     => sprintf(__('Popular %s', 'claritycloud-directory'), $plural_label),
                'search_items'      => sprintf(__('Search %s', 'claritycloud-directory'), $plural_label),
            ),
            'public'                => true,
            'show_ui'               => true,
            'show_in_rest'          => true,
            'hierarchical'          => true,
            'rewrite'               => array('slug' => 'business-category'),
        )
    );

}

Сейчас мои URL-адреса:

  1. http://localhost/demo-project/directory/
  2. http://localhost/demo-project/business-category/antiques/
  3. http://localhost/demo-project/directory/a-lil-bit-of-sas/

Будет как ниже:

  1. http://localhost/demo-project/directory/
  2. http://localhost/demo-project/directory/category/antiques/
  3. http://localhost/demo-project/directory/a-lil-bit-of-sas/

Просто нужно изменить второй URL-адрес выше.

Кто-нибудь, пожалуйста, посоветуйте мне?

Спасибо, Субханкар

1 Ответ

0 голосов
/ 15 июля 2020

Наконец, я получил решение здесь:

add_action ('init', 'cpt_resources');

function cpt_resources () {

$labels = array(
    'name' => _x('Resources', 'snt'),
    'singular_name' => _x('Resource', 'snt'),
    'add_new' => _x('Add Resource', 'snt'),
    'add_new_item' => __('Add Resource'),
    'edit_item' => __('Edit Resource'),
    'new_item' => __('New Resource'),
    'view_item' => __('View Resource'),
    'search_items' => __('Search Resources'),
    'not_found' =>  __('Nothing found'),
    'not_found_in_trash' => __('Nothing found in Trash'),
    'parent_item_colon' => ''
);

$args = array(
    'labels' => $labels,
    'taxonomies' => array('resource_type'),
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'query_var' => true,
    'has_archive'           => true,
    'rewrite'   => array( 'slug' => 'resources' ),
    'capability_type' => 'post',
    'hierarchical' => false,
    'menu_position' => null,
    'supports' => array('title','thumbnail', 'editor' ),
  ); 

register_post_type( 'resources_post_type' , $args );

 

}

function resource_type () {

$labels = array(
    'name'                       => _x( 'Resource Types', 'Taxonomy General Name', 'snt' ),
    'singular_name'              => _x( 'Resource Type', 'Taxonomy Singular Name', 'snt' ),
    'menu_name'                  => __( 'Resource Types', 'snt' ),
    'all_items'                  => __( 'All Items', 'snt' ),
    'parent_item'                => __( 'Parent Item', 'snt' ),
    'parent_item_colon'          => __( 'Parent Item:', 'snt' ),
    'new_item_name'              => __( 'New Item Name', 'snt' ),
    'add_new_item'               => __( 'Add New Item', 'snt' ),
    'edit_item'                  => __( 'Edit Item', 'snt' ),
    'update_item'                => __( 'Update Item', 'snt' ),
    'view_item'                  => __( 'View Item', 'snt' ),
    'separate_items_with_commas' => __( 'Separate items with commas', 'snt' ),
    'add_or_remove_items'        => __( 'Add or remove items', 'snt' ),
    'choose_from_most_used'      => __( 'Choose from the most used', 'snt' ),
    'popular_items'              => __( 'Popular Items', 'snt' ),
    'search_items'               => __( 'Search Items', 'snt' ),
    'not_found'                  => __( 'Not Found', 'snt' ),
    'no_terms'                   => __( 'No items', 'snt' ),
    'items_list'                 => __( 'Items list', 'snt' ),
    'items_list_navigation'      => __( 'Items list navigation', 'snt' ),
);
$args = array(
    'labels'                     => $labels,
    'hierarchical'               => true,
    'public'                     => true,
    'show_ui'                    => true,
    'show_admin_column'          => true,
    'show_in_nav_menus'          => true,
    'show_tagcloud'              => true,
    'rewrite'                    => array('slug' => 'resources/category')
);
register_taxonomy( 'resource_type', array( 'resources_post_type' ), $args );

} add_action ('init', 'resource_type', 0);

function resources_cpt_generating_rule ($ wp_rewrite) {

$rules = array();
$terms = get_terms( array(
    'taxonomy' => 'resource_type',
    'hide_empty' => false,
) );

$post_type = 'resources_post_type';

foreach ($terms as $term) {    
            
    $rules['resources/category/' . $term->slug . '/([^/]*)$'] = 'index.php?post_type=' . $post_type. '&resources_post_type=$matches[1]&name=$matches[1]';
                    
}

// merge with global rules
$wp_rewrite->rules = $rules + $wp_rewrite->rules;

}

add_filter ('generate_rewrite_rules', 'resources_cpt_generating_rule');

Результат: http://localhost/demo-wp/resources/

http://localhost/demo-wp/resources/category/test-resources/

http://localhost/demo-wp/resources/final-check/

...