在分类 slug 上重写 slug 自定义帖子类型

时间:2021-01-04 13:36:16

标签: php wordpress

我有自定义帖子类型 -> 书籍和分类 -> 流派。

现在我有 post slug domain.com/book/book-name

  1. 我需要为 post 获取 slug:domain.com/fantasy/book-name(幻想它的分类法“流派”的关键),它是如何制作的?

  2. 现在我有了存档分类 slug:domain.com/genre/fantasy/,domain.com/fantasy/ 上有什么变化?

我有这个 php 文件,这个创建和注册帖子类型和分类:

<?php

add_action('init', 'my_custom_init');

function my_custom_init(){
    register_post_type('book', array(
        'labels'             => array(
            'name'               => 'Books', 
            'singular_name'      => 'Book', 
            'add_new'            => 'Add Books',
            'add_new_item'       => 'Add Book',
            'edit_item'          => 'Edit Book',
            'new_item'           => 'New Book',
            'view_item'          => 'View Book',
            'search_items'       => 'Find Book',
            'parent_item_colon'  => '',
            'menu_name'          => 'Books'

        ),
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => true,
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => false,
        'menu_position'      => null,
        'supports'           => array('title','editor','author','thumbnail','excerpt','comments')
    ) );
}


// reg taxonomy (create_book_taxonomies)
add_action( 'init', 'create_book_taxonomies' );


function create_book_taxonomies(){

   
    register_taxonomy('genre', array('book'), array(
        'hierarchical'  => true,
        'labels'        => array(
            'name'              => _x( 'Genres', 'taxonomy general name' ),
            'singular_name'     => _x( 'Genre', 'taxonomy singular name' ),
            'search_items'      =>  __( 'Search Genres' ),
            'all_items'         => __( 'All Genres' ),
            'parent_item'       => __( 'Parent Genre' ),
            'parent_item_colon' => __( 'Parent Genre:' ),
            'edit_item'         => __( 'Edit Genre' ),
            'update_item'       => __( 'Update Genre' ),
            'add_new_item'      => __( 'Add New Genre' ),
            'new_item_name'     => __( 'New Genre Name' ),
            'menu_name'         => __( 'Genre' ),
        ),
        'show_ui'       => true,
        'query_var'     => true,
        //'rewrite'       => array( 'slug' => 'the_genre' ), 
    ));
}

0 个答案:

没有答案