几天来,我一直在尝试创建带有类别的自定义帖子类型。到目前为止,我已经完成了这项工作,我可以轻松地添加内容,并将其分配给一个类别。我的代码如下。
我不明白,似乎无法工作的是创建一个存档页面来显示一个类别的帖子。
例如:我的帖子类型称为Adverts。我的类别称为摄影师。
页面是否可以动态计算出您所在的类别并显示属于该类别的所有自定义帖子?
function my_custom_post_advert() {
$labels = array(
'name' => _x( 'Adverts', 'post type general name' ),
'singular_name' => _x( 'Advert', 'post type singular name' ),
'add_new' => _x( 'Add New', 'advert' ),
'add_new_item' => __( 'Add New Advert' ),
'edit_item' => __( 'Edit Advert' ),
'new_item' => __( 'New Advert' ),
'all_items' => __( 'All Adverts' ),
'view_item' => __( 'View Advert' ),
'search_items' => __( 'Search Adverts' ),
'not_found' => __( 'No adverts found' ),
'not_found_in_trash' => __( 'No adverts found in the Trash' ),
'parent_item_colon' => '',
'menu_name' => 'Adverts'
);
$args = array(
'labels' => $labels,
'description' => 'Holds our adverts and advert specific data',
'public' => true,
'menu_position' => 5,
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'category' ),
'has_archive' => true,
);
register_post_type( 'advert', $args );
}
add_action( 'init', 'my_custom_post_advert' );
function my_taxonomies_advert() {
$labels = array(
'name' => _x( 'Advert Categories', 'taxonomy general name' ),
'singular_name' => _x( 'Advert Category', 'taxonomy singular name' ),
'search_items' => __( 'Search Advert Categories' ),
'all_items' => __( 'All Advert Categories' ),
'parent_item' => __( 'Parent Advert Category' ),
'parent_item_colon' => __( 'Parent Advert Category:' ),
'edit_item' => __( 'Edit Advert Category' ),
'update_item' => __( 'Update Advert Category' ),
'add_new_item' => __( 'Add New Advert Category' ),
'new_item_name' => __( 'New Advert Category' ),
'menu_name' => __( 'Advert Categories' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
);
register_taxonomy( 'advert_category', 'advert', $args );
}
add_action( 'init', 'my_taxonomies_advert', 0 );