隐藏列表wordpress中当前选定的帖子

时间:2016-06-04 09:31:21

标签: wordpress

我在页面中显示了所有帖子。

所选帖子想要从列表中隐藏。

这是我的代码和截图。

<?php
global $post;
if(is_category() || is_single()){
foreach(get_the_category() as $category){
$current = $category->cat_ID;
$current_name = $category->cat_name;
$myposts = get_posts(array('category__in' => array($current)));
$myposts = get_posts('numberposts=50&category='.$current); 
    }
}
foreach($myposts as $post) : setup_postdata($post); 
?>
<div class="container-fluid">
<div class="row">
<div class="col-xs-12 col-sm-6 col-md-3">
    <div class="profile-desc">
        <figure class="bgimg" style="background-image:url('<?php echo wp_get_attachment_thumb_url( get_post_thumbnail_id( $post->ID ) ); ?>');"></figure>
        <div class="profile-heading"><?php the_title();?></div>
        <div class="profile-history"><?php the_content(); ?>
            <div class="read">
                <a class="more-link" href="<?php the_permalink();?>"><?php _e('Read More');?></a>
            </div>
        </div>
    </div>
</div>
<?php endforeach; ?>
</div>
</div>

截图

enter image description here

1 个答案:

答案 0 :(得分:1)

<?php
global $wp_query;
$cat_ID = get_the_category($post->ID);
$cat_ID = $cat_ID[0]->cat_ID;
$this_post = $post->ID;
query_posts(array('cat' => $cat_ID, 'post__not_in' => array($this_post), 'posts_per_page' => 14, 'orderby' => 'rand'));
?>

您需要更改查询。您可以向其添加post_not_in参数,因此它会将其排除。

比其他方法更快更安全。这也是WP codex推荐的方法: http://codex.wordpress.org/Class_Reference/WP_Query#Parameters