最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How we exclude current post form related posts

programmeradmin2浏览0评论

When user open any post, then on the right side they can see related posts to that post. But currently in related posts my current open post also showing, how i can exclude that post which is currently open.

When user open any post, then on the right side they can see related posts to that post. But currently in related posts my current open post also showing, how i can exclude that post which is currently open.

Share Improve this question asked Mar 13, 2013 at 10:33 AdiAdi 3402 gold badges9 silver badges23 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 3

Use the parameter 'post__not_in' to exclude post IDs:

$posts = new WP_Query(
    array (
        'post__not_in' => array(get_the_ID()) // exclude current post ID
    )
);

If you could give us some code snippets of what you are currently using, it would help us formulate an answer. Unfortunately I don't have enough reputation to say this as a comment on the OP, so I will provide my answer based on the knowledge provided in the OP.

When loading the current post (presumably by using single.php), you can save the post ID in a PHP variable, like so:

<?php $do_not_duplicate = $post->ID; ?>

Later, in your loop to display related posts, you need an if-statement to skip the loop if the ID's match. Like so:

<?php
    // Get related posts
    $related = new WP_Query( $args );
    while ( $related->have_posts() ) : $related->the_post();
        if (in_array($post->ID, $do_not_duplicate)) continue;
?>
<!-- Your html for related post listing -->
<?php 
    endwhile;           // End 'while have_posts()'
    wp_reset_postdata();    // Reset Post Data
?>
发布评论

评论列表(0)

  1. 暂无评论