我正在尝试在页面上显示按子分类法分组的项目,如下所示。。。
子分类法1(h2)
第1项第2项
子分类法2(h2)
第3项第4项
我确实成功地获得了分类法的子分类法并获得了它们的帖子,不幸的是,当使用以下代码时,我两次获得了所有父分类法帖子,而不是按照各自的子分类法将它们分组,如。。。
子分类法1(h2)
第1项第2项第3项第4项
子分类法2(h2)
第1项第2项第3项第4项
我有点不知道我的代码到底做错了什么,这些代码使帖子显示在两个组下,而不仅仅是它们所属的子分类法下,如果能给我一个指针,我会非常感激。
<?php
$termID = 8; // Parent A ID
$taxonomyName = "product_category";
$termchildren = get_term_children( $termID, $taxonomyName ); ?>
<?php
foreach ($termchildren as $child) {
$term = get_term_by( \'id\', $child, $taxonomyName );
?>
<h2><?php echo $term->name; ?></h2>
<?php
$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
$wp_query = new WP_Query( array(
\'post_type\' => \'product\',
\'product_category\' =>$term->name,
\'meta_query\' => array(
\'relation\' => \'AND\',
array(
\'key\' => \'product_active\',
\'value\' => \'1\'
),
),
\'posts_per_page\' => -1,
\'orderby\' => \'menu_order\',
\'order\' => \'ASC\',
\'paged\'=> $paged
)); ?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<?php the_title(); ?> <img src="<?php $image = wp_get_attachment_image_src(get_field(\'product_image\'), \'full\'); ?><?php echo $image[0]; ?>" alt="<?php the_title(); ?>" /><br />
<?php the_content(); ?>
<?php endwhile;
wp_reset_query();
?>
<?php
unset($term);
}
?>