简体   繁体   English

Post__not_in 在最近的帖子小部件中被忽略

[英]Post__not_in getting ignored in recent posts widget

I have this code, and I tried adding in post__not_in as suggested by another post I found on this website, and it didn't break anything but didn't work either.我有这段代码,我尝试按照我在这个网站上找到的另一篇文章的建议添加 post__not_in ,它没有破坏任何东西,但也没有工作。 I also tried different examples.我也尝试了不同的例子。

Here's the php code I have这是我的 php 代码

<?php 
$args = array( 'posts_per_page' => '4', 'post__not_in' => array( $post->ID ) );
$recent_posts = new WP_Query($args);
while( $recent_posts->have_posts() ) :  
    $recent_posts->the_post() ?>
    <li>

        <?php if ( has_post_thumbnail() ) : ?>
            <?php the_post_thumbnail( $size, $attr ); ?>
        <?php endif ?> 

    </li>
<?php endwhile; ?>
<?php wp_reset_postdata(); # reset post data so that other queries/loops work ?> 

It doesn't get rid of the current post on the list它不会摆脱列表中的当前帖子

I think Post_ID is not passed correctly.我认为 Post_ID 没有正确传递。 In some cases, such as when you're outside the loop, you may need to use get_queried_object_id() instead of get_the_ID() .在某些情况下,例如当您在循环之外时,您可能需要使用get_queried_object_id()而不是get_the_ID()

$post_ID = get_queried_object_id();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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