【发布时间】:2020-01-25 12:16:53
【问题描述】:
因此,在此代码示例中,我将每个帖子的所有类别输出到前端,在所需的 DIV 内,在 The Loop 的下方。
这很棒。
但出于布局目的,我希望能够将其限制为仅从每个帖子输出两个类别,即使它有 5 或 10 个相关联。
有什么代码建议吗?
<?php
// The Query
$args = array(
'post_type' => 'post',
'posts_per_page' => 36,
'orderby' => 'date',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'menuelement',
'field' => 'slug',
'terms' => array('news', 'debate', 'depth'),
),
),
);
$menuelements = new WP_Query( $args );
// checks if has posts
if ($menuelements->have_posts()) : ?>
<div class="kmn-posts-row">
<?php
// The Loop
while ($menuelements->have_posts()) : $menuelements->the_post(); ?>
<div class="kmn-posts-category">
<span><?php echo get_modified_term_list( get_the_ID(), 'category', '', '', '', array(1)); ?></span>
</div>
<?php
endwhile;
?>
</div>
<?php
else :
echo 'No posts in query.';
endif;
/* Restore original Post Data
* NB: Because we are using new WP_Query we aren't stomping on the
* original $wp_query and it does not need to be reset with
* wp_reset_query(). We just need to set the post data back up with
* wp_reset_postdata().
*/
wp_reset_postdata();
【问题讨论】:
标签: php wordpress categories