自定义分类页面不起作用
Posted
技术标签:
【中文标题】自定义分类页面不起作用【英文标题】:Custom Taxonomy Page not working 【发布时间】:2016-07-29 14:08:51 【问题描述】:我创建了自定义帖子类型“目录”并注册了自定义分类“目录类别”。但无法访问分类页面“taxonomy-directory-category.php”。出现“错误 404 - 未找到”错误。
function create_post_type_listing()
register_post_type('directory',
array(
'labels' => array(
'name' => __( 'Listings'),
'singular_name' => __( 'Listing'),
'add_new' => __('Add New Listing' ),
'add_new_item' => __('Add New Listing'),
'edit' => __( 'Edit Listing' ),
'edit_item' => __( 'Edit Listing' ),
'new_item' => __( 'New listing' ),
'view' => __( 'View Listing' ),
'view_item' => __( 'View Listing' ),
'search_items' => __( 'Search Listings' ),
'not_found' => __( 'No listings found' ),
'not_found_in_trash' => __( 'No listings found in Trash' ),
'featured_image' => __( 'Listing Image' ),
'set_featured_image' => __( 'Set Listing Image' ),
'remove_featured_image' => __( 'Remove Listing Image' ),
'use_featured_image' => __( 'Use Listing Image' )
),
'public' => true,
'menu_position' => 5,
'menu_icon' => plugins_url( 'images/listing-20x20.png', __FILE__ ),
'rewrite' => array(
'slug' => __('directory')
),
'supports' => array( 'title','editor','thumbnail')));
add_action( 'init', 'create_listing_taxonomies', 0 );
function create_listing_taxonomies()
// Add new taxonomy, make it hierarchical (like categories)
$labels = array(
'name' => 'Listing Categories',
'singular_name' => 'Listing Category',
'search_items' => 'Listing Categories',
'all_items' => 'All Listing Categories',
'parent_item' => 'Parent Listing Category',
'parent_item_colon' => 'Parent Listing Category:',
'edit_item' => 'Edit Listing Category',
'update_item' => 'Update Listing Category',
'add_new_item' => 'Add New Listing Category',
'new_item_name' => 'New Listing Category',
'menu_name' => 'Listing Category',
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'directory-category' ),
);
register_taxonomy( 'directory-category', array( 'directory' ), $args );
function listing_flush_rules()
//defines the post type so the rules can be flushed.
create_post_type_listing();
//and flush the rules.
flush_rewrite_rules();
register_activation_hook(__FILE__, 'listing_flush_rules' );
add_action( 'init', 'create_post_type_listing' );
请帮助我了解分类页面结构。
问候, 舒巴吉特萨哈
【问题讨论】:
【参考方案1】:请将以下代码添加到您的functions.php或插件页面中
add_action('init', 'create_taxonomies', 0);
function create_taxonomies()
//change start///
$title = 'Your title';
$slug = 'yourslug';
$your_plugin_textdomain = 'textdomain';
//change end///
$labels = array(
'name' => _x($title, $your_plugin_textdomain),
'singular_name' => _x($title, $your_plugin_textdomain),
'search_items' => __('Search Genres'),
'all_items' => __('All ' . $title),
'parent_item' => __('Parent ' . $title),
'parent_item_colon' => __('Parent ' . $title . ':'),
'edit_item' => __('Edit ' . $title),
'update_item' => __('Update ' . $title),
'add_new_item' => __('Add New ' . $title),
'new_item_name' => __('New ' . $title . ' Name'),
'menu_name' => __( ucfirst($title)),
);
$args = array(
'hierarchical' => true,
'labels' => ($labels),
'show_ui' => true,
// 'show_admin_column' => true, // to display taxonomy in post table
'query_var' => true,
'rewrite' => array('slug' => $slug),
);
register_taxonomy($slug, array($catname[$key]), $args);
【讨论】:
很高兴你遇到了问题。以上是关于自定义分类页面不起作用的主要内容,如果未能解决你的问题,请参考以下文章