【发布时间】:2019-02-17 02:35:48
【问题描述】:
我正在尝试使用名为“事件”的自定义分类法过滤自定义帖子。我拥有的代码应该检查当前帖子的“事件”分类name,然后只列出同一组中的帖子。
我已经检查了法典页面https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters,但无法使其正常工作。有人有什么建议吗?
$post_terms = wp_get_object_terms($post->ID, 'events');
$post_term = $post_terms[0]->name;
$args = array(
'post_type' => 'events',
'post_status' => 'publish',
'posts_per_page' => -1,
'post__not_in' => array(124470, 124455),
'tax_query' => array(
array(
'taxonomy' => 'events',
'field' => 'name',
'terms' => $post_term,
),
),
);
更新,按要求在下面添加代码。
/* custom taxonomies */
function my_custom_events_taxonomies(){
register_taxonomy(
'events',
'events',
array(
'label' => 'Events type',
'rewrite' => array( 'slug' => 'Events'),
'hierarchical' => false,
'custom-fields' => true,
)
);
}
add_action ( 'init', 'my_custom_events_taxonomies');
自定义帖子类型如下:
function events_post_type() {
$labels = array(
'name' => _x( 'Events', 'Post Type General Name', 'remco' ),
'singular_name' => _x( 'Events', 'Post Type Singular Name', 'remco' ),
'menu_name' => __( 'Events', 'remco' ),
'name_admin_bar' => __( 'Events', 'remco' ),
'archives' => __( 'Events Archives', 'remco' ),
'parent_item_colon' => __( 'Parent Events:', 'remco' ),
'all_items' => __( 'All Events', 'remco' ),
'add_new_item' => __( 'Add New Events', 'remco' ),
'add_new' => __( 'Add New', 'remco' ),
'new_item' => __( 'New Item', 'remco' ),
'edit_item' => __( 'Edit Item', 'remco' ),
'update_item' => __( 'Update Item', 'remco' ),
'view_item' => __( 'View Item', 'remco' ),
'search_items' => __( 'Search Item', 'remco' ),
'not_found' => __( 'Not found', 'remco' ),
'not_found_in_trash' => __( 'Not found in Trash', 'remco' ),
'insert_into_item' => __( 'Insert into item', 'remco' ),
'uploaded_to_this_item' => __( 'Uploaded to this item', 'remco' ),
'items_list' => __( 'Items list', 'remco' ),
'items_list_navigation' => __( 'Items list navigation', 'remco' ),
'filter_items_list' => __( 'Filter items list', 'remco' ),
);
$args = array(
'label' => __( 'Events', 'remco' ),
'description' => __( 'Events', 'remco' ),
'labels' => $labels,
'supports' => array( 'title', 'editor','revisions', 'thumbnail', 'custom-fields' ),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 9,
'menu_icon' => 'dashicons-calendar' ,
'show_in_admin_bar' => true,
'show_in_nav_menus' => false,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
'taxonomies' => 'events'
);
register_post_type( 'Events', $args );
}
add_action( 'init', 'events_post_type', 0 );
【问题讨论】:
-
能否请您在您注册自定义帖子类型和分类的位置发布您的代码?
-
所以您已经自定义了帖子类型和具有相同名称“事件”的 texonomie?
-
从函数文件中添加了自定义分类代码。是的@NipunTyagi,我现在意识到这不是一件聪明的事情。
标签: wordpress custom-post-type custom-taxonomy