好的,我为你工作过:
首先,我注册了自定义帖子类型和分类:
add_action(\'init\', \'my_init\');
add_action(\'admin_init\', \'my_admin_init\');
function my_admin_init() {
$role = get_role( \'administrator\' );
$role->add_cap( \'edit_event\' );
$role->add_cap( \'read_event\' );
$role->add_cap( \'delete_event\' );
$role->add_cap( \'edit_events\' );
$role->add_cap( \'read_events\' );
$role->add_cap( \'delete_events\' );
$role->add_cap( \'edit_others_events\' );
$role->add_cap( \'publish_events\' );
$role->add_cap( \'read_private_events\' );
$role->add_cap( \'delete_private_events\' );
$role->add_cap( \'delete_published_events\' );
$role->add_cap( \'edit_private_events\' );
$role->add_cap( \'edit_published_events\' );
$role->add_cap( \'create_events\' );
$role->add_cap( \'delete_others_events\' );
$role->add_cap( \'delete_others_event\' );
}
function my_init() {
global $post;
$labels = array(
\'name\' => __( \'event\', \'webto\' ),
\'singular_name\' => __( \'Header component\', \'webto\' ),
\'menu_name\' => __( \'Event\', \'webto\' ),
\'parent_item_colon\' => __( \'Parental:\', \'webto\' ),
\'all_items\' => __( \'event\', \'webto\' ),
\'view_item\' => __( \'View\', \'webto\' ),
\'add_new_item\' => __( \'Add new event\', \'webto\' ),
\'add_new\' => __( \'Add new\', \'webto\' ),
\'edit_item\' => __( \'Edit event\', \'webto\' ),
\'update_item\' => __( \'Update event \', \'webto\' ),
\'search_items\' => __( \'Find event\', \'webto\' ),
\'not_found\' => __( \'Not found\', \'webto\' ),
\'not_found_in_trash\' => __( \'Not found in the bin\', \'webto\' ),
);
$args = array(
\'labels\' => $labels,
\'supports\' => array(\'title\'),
\'public\' => false,
\'publicly_queryable\' => false,
\'capability_type\' => array(\'event\', \'events\'),
\'map_meta_cap\' => true,
\'show_in_menu\' => true,
\'rewrite\' => array( \'slug\' => \'events\' ),
\'query_var\' => true,
\'show_in_rest\' => false,
\'show_ui\' => true,
\'hierarchical\' => false,
\'menu_position\' => 1,
\'menu_icon\' => \'dashicons-welcome-widgets-menus\',
);
register_post_type( \'events\', $args );
$labels = array(
\'name\' => _x( \'county\', \'taxonomy general name\' ),
\'singular_name\' => _x( \'county\', \'taxonomy singular name\' ),
\'search_items\' => __( \'Search county\' ),
\'all_items\' => __( \'All counties\' ),
\'parent_item\' => __( \'Parent county\' ),
\'parent_item_colon\' => __( \'Parent county:\' ),
\'edit_item\' => __( \'Edit county\' ),
\'update_item\' => __( \'Update county\' ),
\'add_new_item\' => __( \'Add New county\' ),
\'new_item_name\' => __( \'New county Name\' ),
\'menu_name\' => __( \'counties\' )
);
// Register Taxonomy
register_taxonomy(\'county\', array(\'events\'), array(
\'hierarchical\' => true,
\'public\' => true,
\'publicly_queryable\' => true,
\'labels\' => $labels,
\'show_ui\' => true,
\'show_admin_column\' => true,
\'query_var\' => true
));
flush_rewrite_rules();
}
我得到以下信息:
创建您的帖子并为其分配县
counties(县)页面上的counties(县)将具有可查看的链接
我们可以为特定的县创建存档页
taxonomy-county.php
同时显示您的帖子
// templates/your-template/taxonomy-county.php
<?php
global $post;
$county = get_queried_object();
$args = array(
\'post_type\' => \'events\',
\'numberposts\' => -1,
\'tax_query\' => array(
array(
\'taxonomy\' => $county->taxonomy,
\'field\' => \'term_id\',
\'terms\' => $county->term_id
)
)
);
// Get posts by current county
$events = get_posts($args);
// All counties
$all_counties = get_terms($county->taxonomy);
foreach($all_counties as $county) {
$args = array(
\'post_type\' => \'events\',
\'numberposts\' => -1,
\'tax_query\' => array(
array(
\'taxonomy\' => $county->taxonomy,
\'field\' => \'term_id\',
\'terms\' => $county->term_id
)
)
);
// get posts by county
$events = get_posts($args);
}