我在functions.php中设置了自定义分类法(menu_category)和自定义帖子类型(menu_item),如下所示:
/* Set up custom post type for Menu Items */
function create_menu_items_post_type() {
$plugin_directory = plugins_url('images/', __FILE__ );
register_post_type( 'menu_item',
array(
'labels' => array(
'singular_name' => __( 'Menu item'),
'name' => __( 'The Menu'),
'add_new' => __( 'Add menu item'),
'add_new_item' => __( 'Add menu item'),
'edit_item' => __( 'Edit menu item'),
'new_item' => __( 'New menu item'),
'search_items' => __( 'Search menu items'),
'not_found' => __( 'No menu items found'),
'not_found_in_trash'=> __( 'No menu items found in trash'),
'all_items' => __( 'All menu items','sbr')
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'menu_item'),
'publicly_queryable' => true,
'hierarchical' => true,
'show_ui' => true,
'show_in_menu' => true,
'exclude_from_search' => true,
'query_var' => true,
'menu_position' => 27,
'can_export' => true,
'menu_icon' => get_template_directory_uri() . '/images/system/menu.svg',
'supports' => array('title', 'revisions', 'author'),
'capability_type' => 'post',
'taxonomies' => array('menu_category'),
'capabilities' => array(
'create_posts' => 'edit_posts',
),
'map_meta_cap' => true,
)
);
}
add_action( 'init', 'create_menu_items_post_type' );
/* Set up custom taxonomy for Menu Categories */
function create_menu_category_taxonomy() {
$labels = array(
'name' => _x( 'Menu categories', 'taxonomy general name' ),
'singular_name' => _x( 'Menu category', 'taxonomy singular name' ),
'search_items' => __( 'Search menu categories' ),
'all_items' => __( 'All menu categories' ),
'parent_item' => __( 'Parent menu categories' ),
'parent_item_colon' => __( 'Parent menu categories:' ),
'edit_item' => __( 'Edit menu category' ),
'update_item' => __( 'Update menu category' ),
'add_new_item' => __( 'Add new menu category' ),
'new_item_name' => __( 'New menu category' ),
'menu_name' => __( 'Menu categories' ),
);
register_taxonomy(
'menu_category',
'menu_item',
array(
'label' => __( 'Menu Category' ),
'rewrite' => array( 'slug' => 'menu_category' ),
'hierarchical' => true,
'labels' => $labels
)
);
}
add_action( 'init', 'create_menu_category_taxonomy' );
...当我在WP管理员时工作正常 - 我可以添加新的类别,帖子等
但在前端我试图返回所有menu_category
类别和menu_item
帖子的列表,如下所示:
$the_menus = get_categories(array(
'echo' => 0,
'hide_empty' => false,
'taxonomy' => 'menu_category',
'hierarchical' => 1,
'show_count' => 0
)); ?>
<?php
foreach ($the_menus as $the_menu) {
$the_menu_args = array(
'posts_per_page' => -1,
'post_type' => 'menu_item',
'showposts' => -1,
'post_status' => 'publish',
'cat' => $the_menu->cat_ID
);
$term = get_queried_object();
$the_menu_tasks = new WP_Query($the_menu_args);
$the_menu_slug = $the_menu->slug;
$the_menu_ID = $the_menu->cat_ID;
$page_slug = $term->slug;
?>
<li>
<a href="<?php echo get_category_link( $the_menu_ID ); ?>">
<?php echo $the_menu->cat_name; ?>
</a>
</li>
<?php
}
wp_reset_postdata();
问题是这个循环什么也没有返回。
如果我从'taxonomy' => 'menu_category'
删除$the_menus
并从'post_type' => 'menu_item'
删除$the_menu_args
,它将返回所有正常的类别和帖子(你知道,常规帖子和类别)。
因此,当我指定自定义分类和自定义帖子类型时,它似乎只会失败。
我究竟做错了什么?
PS:这里提到的'菜单项'是我想要建立的餐馆菜单。它与WordPress菜单无关:-P
要从自定义分类中获取帖子,我们需要在参数中传递税务查询。请尝试以下代码。它在我的最终工作正常。
<?php
$the_menus = get_terms( array(
'taxonomy' => 'menu_category',
'hide_empty' => false,
));
foreach ($the_menus as $the_menu) {
$args = array(
'post_type' => 'menu_item',
'posts_per_page' => -1,
'post_status' => 'publish',
'tax_query' => array(
array (
'taxonomy' => 'menu_category',
'field' => 'term_id',
'terms' => $the_menu->term_id,
)
),
);
$term = get_queried_object();
$the_menu_tasks = new WP_Query($args);
$the_menu_slug = $the_menu->slug;
$the_menu_ID = $the_menu->term_id;
$page_slug = $term->slug;
?>
<!-- get category names here -->
<li>
<a href="<?php echo get_term_link( $the_menu_ID ); ?>">
<?php echo $the_menu->name; ?>
</a>
</li>
<!-- get category names here //END -->
<?php
/* GET posts according to custom taxonomy id */
if ($the_menu_tasks->have_posts()){
while($the_menu_tasks->have_posts()) : $the_menu_tasks->the_post();
echo get_the_title() .'<br />';
endwhile;
}else{
echo "Sorry! Posts not found";
}
/* GET posts according to custom taxonomy id -- END */
}
?>