繁体   English   中英

显示最近的博客文章,但排除当前文章

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM