简体   繁体   中英

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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