[英]Show recent blog posts, but exclude current post
我想在我的單博客頁面底部顯示 3 個最近的帖子,但沒有當前帖子。
我的代碼:
<div class="blog__posts">
<h2><?php esc_html_e('andere blogberichten', 'dfib-theme'); ?></h2>
<a href="<?php echo get_permalink( get_option( 'page_for_posts' ) ); ?>" class="btn btn-overview"><?php esc_html_e('alle blogberichten', 'dfib-theme'); ?></a>
<div class="blog__posts--wrapper">
<?php
$the_query = new WP_Query( array(
'posts_per_page' => 2,
));
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="blog__single">
<a href="<?php the_permalink(); ?>"><div class="blog__single--img" style="background-image: url(<?php echo get_the_post_thumbnail_url();?>);"></div></a>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<a href="<?php the_permalink(); ?>">lees meer</a>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php __('No News'); ?></p>
<?php endif; ?>
</div>
</div>
它顯示 3 個帖子,但是當我訪問最近的帖子時,相同的帖子再次顯示在底部。 有沒有辦法每次都排除當前的?
我剛剛在這個網站上找到了解決方案: https://pineco.de/snippets/exclude-current-post-from-wp_query/
我剛剛將這部分添加到我的查詢中:
'post__not_in' => array(get_the_ID())
您必須使用post__not_in
排除當前帖子。
在 WP_Query 數組中添加post__not_in
,如下所示。
<?php
$the_query = new WP_Query( array(
'posts_per_page' => 2,
'post__not_in' => array( get_the_ID() )
));
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.