我正在使用WordPress 4.7.3中的分类法创建自定义帖子类型,但由于某些原因,分类页面无法正常工作。我的自定义帖子类型是“事件”,但访问“mysite.com/events/event-category”或访问“mysite.com/events”时,archive-events.php模板文件无法正常工作
以下是我在functions.php中的代码,我在这里做错了什么?洛尔
add_action( 'init', 'register_events', 20 );
function register_events() {
$labels = array(
'name' => _x( 'All Events', 'events','sonal' ),
'singular_name' => _x( 'Event', 'events', 'sonal' ),
'add_new' => _x( 'Add New', 'events', 'sonal' ),
'add_new_item' => _x( 'Add New Event', 'events', 'sonal' ),
'edit_item' => _x( 'Edit Event', 'events', 'sonal' ),
'new_item' => _x( 'New Event', 'events', 'sonal' ),
'view_item' => _x( 'View Event', 'events', 'sonal' ),
'search_items' => _x( 'Search Events', 'events', 'sonal' ),
'not_found' => _x( 'No Events found...', 'events', 'sonal' ),
'not_found_in_trash' => _x( 'No Events found in Trash', 'events', 'sonal' ),
'parent_item_colon' => _x( 'Parent Event:', 'events', 'sonal' ),
'menu_name' => _x( 'Events', 'events', 'sonal' ),
);
$args = array(
'labels' => __( $labels, 'local' ),
'hierarchical' => true,
'description' => 'events',
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'revisions' ),
'taxonomies' => array( 'events_category'),
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-tickets-alt',
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'query_var' => true,
'can_export' => true,
'rewrite' => array('slug' => 'events/%events_category%','with_front' => FALSE),
'public' => true,
'has_archive' => 'events_category',
'capability_type' => 'post'
);
register_post_type( 'events', $args );
}
//Create Taxonomies (Categories)
add_action( 'init', 'create_events_taxonomies', 20 );
function create_events_taxonomies() {
$labels = array(
'name' => _x( 'Event Categories', 'taxonomy general name', 'sonal' ),
'singular_name' => _x( 'Event Category', 'taxonomy singular name', 'sonal' ),
'search_items' => __( 'Search Event Categories', 'sonal' ),
'all_items' => __( 'All Event Categories', 'sonal' ),
'parent_item' => __( 'Parent Event Category', 'sonal' ),
'parent_item_colon' => __( 'Parent Event Category:', 'sonal' ),
'edit_item' => __( 'Edit Event Category', 'sonal' ),
'update_item' => __( 'Update Event Category', 'sonal' ),
'add_new_item' => __( 'Add New Event Category', 'sonal' ),
'new_item_name' => __( 'New Event Category Name', 'sonal' ),
'menu_name' => __( 'Event Categories', 'sonal' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'events', 'with_front' => false ),
);
register_taxonomy( 'events_category', array( 'events' ), $args );
}
//Set Permalinks
function wpa_events_permalinks( $post_link, $post ){
if ( is_object( $post ) && $post->post_type == 'events' ){
$terms = wp_get_object_terms( $post->ID, 'events_category' );
if( $terms ){
return str_replace( '%events_category%' , $terms[0]->slug , $post_link );
}
}
return $post_link;
}
add_filter( 'post_type_link', 'wpa_events_permalinks', 1, 2 );
答案 0 :(得分:2)
从事件自定义帖子类型
的重写参数中删除/%events_category%在:
'rewrite' => array('slug' => 'events/%events_category%','with_front' => FALSE),
之后:
'rewrite' => array('slug' => 'events','with_front' => FALSE),
再次在事件自定义帖子类型的参数中,将“has_archive”设置为true
在
'has_archive' => 'events_category',
后:
'has_archive' => true,
在设置中刷新永久链接 - >固定链接 请务必使用
选择自定义结构/%category%/%postname%/
然后转到
yoursite.com/events/
archive-events.php文件应该可以运行