仅显示 Wordpress 中每个类别的最新 post
Display only the latest post for each category in Wordpress
我知道如何显示我网站的最新posts,但我想知道是否有办法列出最新的posts,只显示一个post ] 每个类别。假设我们有 4 个类别,那么列表中只会显示 4 post 个,并根据发布日期排序。
我应该怎么办?
这是我现在的代码:
<?php $postid = get_the_ID(); ?>
<?php
$args = array(
'post_type' => 'topics',
);
?>
<?php $query = new WP_Query( $args ); ?>
<?php if($query -> have_posts()): ?>
<?php while($query -> have_posts()): $query->the_post();?>
<article class="article">
<a href="<?php the_permalink(); ?>">
<?php
if ($terms = get_the_terms($post->ID, 'topics_category')) :
foreach ( $terms as $term ) :
$term_slug = $term -> slug;
$term_link = get_term_link( $term );
?>
<p class="article__category">
<?php
echo $term->name;
?>
</p>
<?php
endforeach;
endif;
?>
<h2 class="article__title"><?php the_title(); ?></h2>
<p class="article__date"><?php the_time('Y年m月d日'); ?></p>
</a>
</article>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
您应该反过来,即首先使用 get_terms() 获取类别,然后在 foreach 循环中使用 WP_Query 为每个类别获取一个 post。在 WP_Query args 中,您应该传递 posts_per_page => 1 参数以及 tax_query 参数。有关可用参数,请查看此处:https://gist.github.com/luetkemj/2023628
希望对您有所帮助
您可以替换您的代码
<?php $postid = get_the_ID(); ?>
<?php
$args = array(
'post_type' => 'topics',
'post_status' => 'publish'
);
?>
<?php $query = new WP_Query( $args ); ?>
<?php if($query -> have_posts()): ?>
<?php while($query -> have_posts()): $query->the_post();?>
<article class="article">
<a href="<?php the_permalink(); ?>">
<?php
if ($product_terms = wp_get_object_terms($post->ID, 'topics_category')) :
foreach ( $product_terms as $term ) :
$term_slug = $term->slug;
$term_link = get_term_link( $term, 'topics_category' );
?>
<p class="article__category">
<?php
echo $term->name;
?>
</p>
<?php
endforeach;
endif;
?>
<h2 class="article__title"><?php the_title(); ?></h2>
<p class="article__date"><?php the_time('Y年m月d日'); ?></p>
</a>
</article>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
如果它不起作用,则将函数 wp_get_object_terms
替换为 wp_get_post_terms
知识
https://codex.wordpress.org/Function_Reference/wp_get_post_terms
和
https://codex.wordpress.org/Function_Reference/wp_get_object_terms
我知道如何显示我网站的最新posts,但我想知道是否有办法列出最新的posts,只显示一个post ] 每个类别。假设我们有 4 个类别,那么列表中只会显示 4 post 个,并根据发布日期排序。 我应该怎么办? 这是我现在的代码:
<?php $postid = get_the_ID(); ?>
<?php
$args = array(
'post_type' => 'topics',
);
?>
<?php $query = new WP_Query( $args ); ?>
<?php if($query -> have_posts()): ?>
<?php while($query -> have_posts()): $query->the_post();?>
<article class="article">
<a href="<?php the_permalink(); ?>">
<?php
if ($terms = get_the_terms($post->ID, 'topics_category')) :
foreach ( $terms as $term ) :
$term_slug = $term -> slug;
$term_link = get_term_link( $term );
?>
<p class="article__category">
<?php
echo $term->name;
?>
</p>
<?php
endforeach;
endif;
?>
<h2 class="article__title"><?php the_title(); ?></h2>
<p class="article__date"><?php the_time('Y年m月d日'); ?></p>
</a>
</article>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
您应该反过来,即首先使用 get_terms() 获取类别,然后在 foreach 循环中使用 WP_Query 为每个类别获取一个 post。在 WP_Query args 中,您应该传递 posts_per_page => 1 参数以及 tax_query 参数。有关可用参数,请查看此处:https://gist.github.com/luetkemj/2023628
希望对您有所帮助
您可以替换您的代码
<?php $postid = get_the_ID(); ?>
<?php
$args = array(
'post_type' => 'topics',
'post_status' => 'publish'
);
?>
<?php $query = new WP_Query( $args ); ?>
<?php if($query -> have_posts()): ?>
<?php while($query -> have_posts()): $query->the_post();?>
<article class="article">
<a href="<?php the_permalink(); ?>">
<?php
if ($product_terms = wp_get_object_terms($post->ID, 'topics_category')) :
foreach ( $product_terms as $term ) :
$term_slug = $term->slug;
$term_link = get_term_link( $term, 'topics_category' );
?>
<p class="article__category">
<?php
echo $term->name;
?>
</p>
<?php
endforeach;
endif;
?>
<h2 class="article__title"><?php the_title(); ?></h2>
<p class="article__date"><?php the_time('Y年m月d日'); ?></p>
</a>
</article>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
如果它不起作用,则将函数 wp_get_object_terms
替换为 wp_get_post_terms
知识
https://codex.wordpress.org/Function_Reference/wp_get_post_terms
和
https://codex.wordpress.org/Function_Reference/wp_get_object_terms