Wordpress-在URL段中添加自定义分类法

时间:2018-10-11 12:21:09

标签: php wordpress

我正在努力为自定义帖子类型实现特定的URL结构。

我创建了一个名为“成人词典”的自定义帖子类型,该帖子将分类为词汇表。

我希望能够产生以下结构。

  • 主要(列出所有类别)-/ adult-dictionary
  • 类别(列出所有 帖子)-/ adult-dictionary / category-name
  • 帖子(显示帖子)- / adult-dictionary / category-name / post-name

理想情况下,我想在不使用插件的情况下实现这一目标。

这是我的代码(我添加了%category%来显示我要输出的位置)。

/**
* Register Custom Post Type
*/

function create_dictionary_post_type() {

$labels = array(
    'name'                  => _x( 'Adult Dictionary', 'Post Type General Name', 'theme-name' ),
    'singular_name'         => _x( 'Adult Dictionary', 'Post Type Singular Name', 'theme-name' ),
    'menu_name'             => __( 'Adult Dictionary', 'theme-name' ),
    'name_admin_bar'        => __( 'Adult Dictionary', 'theme-name' ),
    'archives'              => __( 'Adult Dictionary Archives', 'theme-name' ),
    'attributes'            => __( 'Adult Dictionary Attributes', 'theme-name' ),
    'parent_item_colon'     => __( 'Parent Post:', 'theme-name' ),
    'all_items'             => __( 'All Posts', 'theme-name' ),
    'add_new_item'          => __( 'Add New Post', 'theme-name' ),
    'add_new'               => __( 'Add New', 'theme-name' ),
    'new_item'              => __( 'New Post', 'theme-name' ),
    'edit_item'             => __( 'Edit Post', 'theme-name' ),
    'update_item'           => __( 'Update Post', 'theme-name' ),
    'view_item'             => __( 'View Post', 'theme-name' ),
    'view_items'            => __( 'View Posts', 'theme-name' ),
    'search_items'          => __( 'Search Adult Dictionary', 'theme-name' ),
    'not_found'             => __( 'Not found', 'theme-name' ),
    'not_found_in_trash'    => __( 'Not found in Trash', 'theme-name' ),
    'featured_image'        => __( 'Featured Image', 'theme-name' ),
    'set_featured_image'    => __( 'Set featured image', 'theme-name' ),
    'remove_featured_image' => __( 'Remove featured image', 'theme-name' ),
    'use_featured_image'    => __( 'Use as featured image', 'theme-name' ),
    'insert_into_item'      => __( 'Insert into post', 'theme-name' ),
    'uploaded_to_this_item' => __( 'Uploaded to this post', 'theme-name' ),
    'items_list'            => __( 'Post list', 'theme-name' ),
    'items_list_navigation' => __( 'Post list navigation', 'theme-name' ),
    'filter_items_list'     => __( 'Filter posts list', 'theme-name' ),
  );
  $args = array(
    'label'                 => __( 'Adult Dictionary', 'theme-name' ),
    'description'           => __( 'The Description', 'theme-name' ),
    'labels'                => $labels,
    'supports'              => array( 'title', 'editor'),
    'rewrite'               => array('slug' => 'adult-dictionary/%category%/', 'with_front' => false),
    'hierarchical'          => false,
    'public'                => true,
    'show_ui'               => true,
    'show_in_menu'          => true,
    'menu_position'         => 10,
    'menu_icon'             => 'dashicons-book',
    'show_in_admin_bar'     => true,
    'show_in_nav_menus'     => true,
    'can_export'            => true,
    'has_archive'           => true,
    'exclude_from_search'   => false,
    'publicly_queryable'    => true,
    'query_var'             => true,
    'capability_type'       => 'post',
  );
  register_post_type( 'adult-dictionary', $args );

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

/**
 * Register Custom Category
 */

 add_action( 'init', 'build_taxonomies', 0 );

 function build_taxonomies() {
     register_taxonomy(
     'glossary',
     'adult-dictionary',  // For this custom post type
     array(
         'hierarchical' => true,
         'label' => 'Glossary',
         'query_var' => true,
         'rewrite' => array(
           'slug' => 'adult-dictionary',
           'with_front' => false
         )
     )
   );
 }

0 个答案:

没有答案