首页 文章

从循环中排除当前帖子

提问于
浏览
2

我想在帖子模板中为特定类别添加一个Wordpress循环,以取消当前帖子 .

我被建议使用:

<?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'));
?>

但我无法让它发挥作用 .

我的循环目前看起来像这样 .

<div class="video">
    <?php
        $catquery = new WP_Query( 'category_name=video&posts_per_page=4' );
        while($catquery->have_posts()) : $catquery->the_post();
    ?>

    <div>
        <a href="<?php the_permalink(); ?>">
            <?php the_post_thumbnail(); ?>
            <h2><?php the_title(); ?></h2>
        </a>
    </div>

    <?php endwhile; ?>

    <p class="more">M<br>O<br>R<br>E</p>
</div>

3 回答

  • 1

    试试这个代码 .

    $postid = get_the_ID();
        $args=array(
          'post__not_in'=> array($postid),
          'post_type' => 'post',
           'category_name'=>'video',
          'post_status' => 'publish',
          'posts_per_page' => 4
    
        );
    
    
        <div class="video">
            <?php
                $catquery = new WP_Query( $args );
                while($catquery->have_posts()) : $catquery->the_post();
            ?>
    
            <div>
                <a href="<?php the_permalink(); ?>">
                    <?php the_post_thumbnail(); ?>
                    <h2><?php the_title(); ?></h2>
                </a>
            </div>
    
            <?php endwhile; ?>
    
            <p class="more">M<br>O<br>R<br>E</p>
        </div>
    
  • -1

    使用

    'post__not_in' => array($post->ID)
    
  • 2

    两个代码块使用两种不同的技术用于Wordpress自定义循环...第一个修改全局查询,第二个创建新的自定义查询 . 我已经在下面概述了你的循环模板 .

    Example with suggested code, global query:

    循环遍历循环代码中的全局$ wp_query对象:

    <div class="video">
    <?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'));
    ?>
    
    <!-- use the global loop here -->
    
    <?php while ( have_posts() ) : the_post(); ?>
    
    
        <div>
            <a href="<?php the_permalink(); ?>">
                <?php the_post_thumbnail(); ?>
                <h2><?php the_title(); ?></h2>
            </a>
        </div>
    
    <?php endwhile; ?>
    
    <p class="more">M<br>O<br>R<br>E</p
    </div>
    

    Example with original code, custom query

    循环遍历自定义查询,添加“post__not_in”:

    <div class="video">
    <?php
        $catquery = new WP_Query(     'category_name=video&posts_per_page=4&post__not_in=' . $post->ID );
        while($catquery->have_posts()) : $catquery->the_post();
    ?>
    
        <div>
            <a href="<?php the_permalink(); ?>">
                <?php the_post_thumbnail(); ?>
                <h2><?php the_title(); ?></h2>
            </a>
        </div>
    
        <?php endwhile; ?>
    
        <p class="more">M<br>O<br>R<br>E</p>
    </div>
    

    对不起,如果我的原始答案不清楚,我最初认为你是在组合这两个代码块 .

相关问题