简体   繁体   English

Wordpress PHP-如何在查询中排除博客文章?

[英]Wordpress PHP - How to exclude a blog post in a query?

I'm about to customize my templates for the news views. 我将为新闻视图自定义模板。 Within article which gets handled by the single.php I added a section with the heading "You could also be interested" and then I display 2 random blog posts. 在由single.php处理的文章中,我添加了一个标题为“您也可能感兴趣”的部分,然后显示2个随机博客文章。

The problem is that the article which I'm viewing also gets displayed in that section. 问题是我正在查看的文章也显示在该部分中。 Is there a way I can exclude it? 有什么办法可以排除它吗?

If you need further information let me know. 如果您需要更多信息,请告诉我。

<?php 
    $posts = new WP_Query( array( 'post_type' => 'post', 'posts_per_page' => $limit, 'category__in' => $categories_query, 'orderby' => 'rand', 'order' => $order, 'showposts' => 2 ) );

    $elements = jevelin_option( 'post_elements' );


?>
<div class="container-fluid more-articles">
  <h2>More interesting articles</h2>
  <div class="row equal-height">


    <?php    while ( $posts->have_posts() ) {    

          $posts->the_post(); 

        ?>

    <div class="col-xs-12 col-md-6 teaser-col">
      <div class="row">
        <div class="col-xs-12 col-md-6 article-teaser-picture">
          <?php echo the_post_thumbnail(); ?>
        </div>
        <div class="col-xs-12 col-md-6 article-teaser">
          <div class="article-teaser-text">
            <h3>
              <?php echo the_title(); ?>
            </h3>
            <?php the_excerpt(); ?>
          </div>
          <div class="sh-button-container sh-button-style-3">
            <a href="<?php echo esc_url( get_permalink() ); ?>" class="sh-button sh-button-large sh-button-icon-left">
              <span class="sh-button-icon">
              <i class="ti-control-forward"></i>
            </span>
              <span class="sh-button-text">Read more</span>
            </a>
          </div>
        </div>
      </div>
    </div>
    <?php

    }

  ?>

  </div>
</div>
$currentID = get_the_ID();

将此行添加到WP_Query

'post__not_in' => array($currentID)

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

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