我正在尝试从中包含子类别的类别中获取\\u posts()。要展示一个例子,请看下面的图片。
所以,正如你所看到的。家长和孩子在里面都有帖子。我只想预览孩子们的内容一次,而不是两次。下图显示了我的意思。您可以看到,某些项目被检索了两次。
我已经创建了一个短代码,但我不知道如何使它的第一部分仅获取父类别中的帖子(首先从甜点中获取),然后显示子类别(烘焙饼干面团和煎饼)中的帖子(如果存在)。
add_shortcode(\'TEDS_MENU\', \'fetch_teds_menu_items\');
function fetch_teds_menu_items($atts)
{
$atts = shortcode_atts(array(
\'category_name\' => \'\'
), $atts);
$category = get_term_by(\'name\', $atts[\'category_name\'], \'category\');
if (!$category) {
return \'\';
}
$args = array(
\'category__in\' => [$category->term_id],
\'post_type\' => \'menu\',
\'numberposts\' => -1,
\'post_status\' => \'publish\'
);
$subcategories = get_categories(
array(
\'parent\' => $category->term_id
)
);
$output = \'<section id="\' . $category->slug . \'">\';
$menu_items = get_posts($args);
foreach ($menu_items as $menu_item) {
setup_postdata($menu_item);
$output .= \'<div class="teds-menu-item-wrapper">\';
$output .= \'<h3 class="teds-menu-item__title">\' . $menu_item->post_title . \'</h3>\';
$output .= \'<div class="teds-menu-item">\';
$output .= \'<div class="teds-menu-item-description">\';
$output .= \'<p class="teds-menu-item-description__text">\' . $menu_item->post_content . \'</p>\';
$output .= \'</div>\';
$output .= \'<ul class="teds-menu-prices-list">\';
if (get_post_meta($menu_item->ID, \'regular_size_price\')[0] || get_post_meta($menu_item->ID, \'large_size_price\')[0]) {
$output .= \'<li class="menu-prices-list--item">R \' . get_post_meta($menu_item->ID, \'regular_size_price\')[0] . \' EGP</li>\';
$output .= \'<li class="menu-prices-list--item">L \' . get_post_meta($menu_item->ID, \'large_size_price\')[0] . \' EGP</li>\';
}
if (get_post_meta($menu_item->ID, \'price\')[0]) {
$output .= \'<li class="teds-menu-prices-list--item">\' . get_post_meta($menu_item->ID, \'price\')[0] . \' EGP</li>\';
}
$output .= \'</ul>\';
$output .= \'</div>\';
$output .= \'</div>\';
}
if ($subcategories) {
$output .= \'<section class="teds-menu-subcategory">\';
foreach ($subcategories as $subcategory) {
$output .= \'<div class="teds-menu-subcategory__content">\';
$output .= \'<h2 class="teds-subcategory-title">\' . $subcategory->name . \'</h2>\';
$subcategory_items = get_posts(array(
\'category\' => $subcategory->cat_ID,
\'post_type\' => \'menu\',
\'numberposts\' => -1,
\'post_status\' => \'publish\'
));
foreach ($subcategory_items as $subcategory_item) {
$output .= \'<div class="teds-subcategory-item">\';
$output .= \'<div class="teds-subcategory-item__content">\';
$output .= \'<h3 class="teds-subcategory-item__title">\' . $subcategory_item->post_title . \'</h3>\';
if ($subcategory_item->post_content) {
$output .= \'<p class="class="menu-prices-list--item teds-subcategory-item__desc">\' . $subcategory_item->post_content . \'</p>\';
}
$output .= \'</div>\';
if (get_post_meta($subcategory_item->ID, \'price\')[0]) {
$output .= \'<ul class="teds-menu-prices-list">\';
$output .= \'<li class="teds-menu-prices-list--item">\' . get_post_meta($subcategory_item->ID, \'price\')[0] . \' EGP</li>\';
$output .= \'</ul>\';
}
$output .= \'</div>\';
}
$output .= \'</div>\';
wp_reset_postdata();
}
wp_reset_postdata();
$output .= \'</section>\';
}
$output .= "</section>";
wp_reset_postdata();
return $output;
}
UPDATE: 下列的
@Sally CJ\'s的答案解决了某些帖子显示两次的问题。但在其他一些类别中,帖子已经消失。例如,以下类别包括帖子,但不再显示:
最合适的回答,由SO网友:Sally CJ 整理而成
如果不想在子类别中包含帖子,请使用category__in
参数:
$args = array(
\'category__in\' => [ get_cat_ID( $category_name ) ], // use category__in
//\'category_name\' => $category_name, // and not this.
\'post_type\' => \'menu\',
\'numberposts\' => -1,
\'post_status\' => \'publish\'
);
虽然上面的内容可以满足您的需求,但下面的内容可能会对您有所帮助。。
function fetch_teds_menu_items($atts)
{
$atts = shortcode_atts(array(
\'category_name\' => \'\'
), $atts);
if ( ! $category = get_term_by( \'name\', $atts[\'category_name\'], \'category\' ) ) {
return \'\';
}
$args = array(
\'category__in\' => [ $category->term_id ],
\'post_type\' => \'menu\',
\'numberposts\' => -1,
\'post_status\' => \'publish\'
);
$meta_data = get_term_meta($category->term_id, \'category_featured_image\', TRUE);
// $category_image = wp_get_attachment_url($meta_data);
$subcategories = get_categories(
array(
\'parent\' => $category->term_id
)
);
$output = \'<section id="\' . $category->slug . \'">\';
...
return $output;
}
一、 e.我使用
get_term_by()
按名称获取完整类别对象(不是slug,但可以是slug)。
PS:Thecategory
参数还包括子类别&mdash;\'category\' => $subcategory->cat_ID,
.