如何在 wordpress 中的每个类别中只显示两个帖子?
How can I display only two posts from each category in wordpress?
我必须显示每个类别的 2 个帖子。下面的代码获取属于每个类别的所有帖子。我的结构是基于选项卡的。我附上我的设计图片。我的标签包含 "all (shows all posts)"、"symptoms (shows posts related to symptoms only" 等,点击每个标签显示相关帖子。
<?php $recent = new WP_Query("post_type=post&posts_per_page=-1&orderby=date&order=DESC");
$count=0;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
while($recent->have_posts()) : $recent->the_post(); ?>
<?php $c = get_the_category();
$cat = $c[0]->cat_name;
$slug = $c[0]->category_nicename;
?>
<div class="element-item transition <?php echo $slug;?>" data-category="<?php echo $slug;?>">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<div class="descrip">
<h2><?php the_title(); ?></h2>
</div>
</a>
</div>
<?php endwhile; wp_reset_postdata(); ?>
[![在此处输入图片描述][2]][2]
如有任何帮助,我们将不胜感激
这样试试
<?php
$cat_args = array(
'orderby' => 'date',
'order' => 'DESC',
'child_of' => 0,
'parent' => '',
'type' => 'post',
'hide_empty' => true,
'taxonomy' => 'category',
);
$categories = get_categories( $cat_args );
foreach ( $categories as $category ) {
$query_args = array(
'post_type' => 'post',
'category_name' => $category->slug,
'posts_per_page' => 2,
'orderby' => 'date',
'order' => 'DESC'
);
$recent = new WP_Query($query_args);
while( $recent->have_posts() ) :
$recent->the_post();
?>
<div class="element-item transition <?php echo $category->slug;?>" data-category="<?php echo $category->slug;?>">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<div class="descrip">
<h2><?php the_title(); ?></h2>
</div>
</a>
</div>
<?php endwhile;
}
wp_reset_postdata();
?>
首先列出所有类别,然后在 foreach
中循环遍历每个类别,并仅查询 2 个最近的类别。
请注意,您可以通过这种方式列出任何分类法。我只是把
'taxonomy' => 'category',
但这可以是分配给您的自定义 post 类型的分类法。
参数列表看起来有点像这样:
$args = array(
'type' => 'post',
'child_of' => 0,
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'taxonomy' => 'category',
'pad_counts' => false
);
希望这对您有所帮助。
我必须显示每个类别的 2 个帖子。下面的代码获取属于每个类别的所有帖子。我的结构是基于选项卡的。我附上我的设计图片。我的标签包含 "all (shows all posts)"、"symptoms (shows posts related to symptoms only" 等,点击每个标签显示相关帖子。
<?php $recent = new WP_Query("post_type=post&posts_per_page=-1&orderby=date&order=DESC");
$count=0;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
while($recent->have_posts()) : $recent->the_post(); ?>
<?php $c = get_the_category();
$cat = $c[0]->cat_name;
$slug = $c[0]->category_nicename;
?>
<div class="element-item transition <?php echo $slug;?>" data-category="<?php echo $slug;?>">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<div class="descrip">
<h2><?php the_title(); ?></h2>
</div>
</a>
</div>
<?php endwhile; wp_reset_postdata(); ?>
[![在此处输入图片描述][2]][2]
如有任何帮助,我们将不胜感激
这样试试
<?php
$cat_args = array(
'orderby' => 'date',
'order' => 'DESC',
'child_of' => 0,
'parent' => '',
'type' => 'post',
'hide_empty' => true,
'taxonomy' => 'category',
);
$categories = get_categories( $cat_args );
foreach ( $categories as $category ) {
$query_args = array(
'post_type' => 'post',
'category_name' => $category->slug,
'posts_per_page' => 2,
'orderby' => 'date',
'order' => 'DESC'
);
$recent = new WP_Query($query_args);
while( $recent->have_posts() ) :
$recent->the_post();
?>
<div class="element-item transition <?php echo $category->slug;?>" data-category="<?php echo $category->slug;?>">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<div class="descrip">
<h2><?php the_title(); ?></h2>
</div>
</a>
</div>
<?php endwhile;
}
wp_reset_postdata();
?>
首先列出所有类别,然后在 foreach
中循环遍历每个类别,并仅查询 2 个最近的类别。
请注意,您可以通过这种方式列出任何分类法。我只是把
'taxonomy' => 'category',
但这可以是分配给您的自定义 post 类型的分类法。
参数列表看起来有点像这样:
$args = array(
'type' => 'post',
'child_of' => 0,
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'taxonomy' => 'category',
'pad_counts' => false
);
希望这对您有所帮助。