我有这段代码可以正常工作,但问题在于显示数据的顺序。现在,当您检查子类别位置时,它几乎没有混乱并且帖子重复。我可以做些什么来显示一个类别树,其中每个类别和子类别中都包含帖子,例如:
<h1>Prime Category 1</h1>
<ul>
<li>Post 1</li>
<li>Post 2</li>
<li>...</li>
</ul>
<h2>Sub Category 1</h2>
<ul>
<li>Post 1</li>
<li> Post 2</li>
<li>...</li>
</ul>
<h2>Sub Category 2</h2>
<ul>
<li>Post 1</li>
<li> ...</li>
</ul>
<h1>Prime Category 2</h1>
<h2>Sub Category</h2>
<ul>
<li>Post 1</li>
<li> ...</li>
</ul>
<h2>Sub Category</h2>
<ul>
<li>Post 1</li>
<li> ...</li>
</ul>
这是我的代码
<?php
$post_type = 'biblioteka';
$tax = 'kategoria-pozycji';
$tax_terms = get_terms( $tax );
if ($tax_terms) {
foreach ($tax_terms as $tax_term) {
$args = array(
'post_type' => $post_type,
'child_of' => $tax_term->term_id,
"$tax" => $tax_term->slug,
'post_status' => 'publish',
'posts_per_page' => 2,
'hierarchical' => true,
'caller_get_posts'=> 1);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) : ?>
<h1><?php echo $tax_term->name; ?></h1>
<ul>
<?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
<li id="post-<?php the_ID(); ?>">
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endwhile; ?>
</ul>
<?php else : ?>
<?php endif; wp_reset_query();
}
}
?>
感谢大家的帮助!