0
我在WordPress 4.7.3中創建了一個包含分類法的自定義帖子類型,但由於某種原因,分類法頁面無法正常工作。我的自定義帖子類型是「events」,但是當訪問「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);