dsilhx5830 2015-12-09 22:20
浏览 36
已采纳

从循环中排除帖子格式导致其他帖子无休止重复

I am trying to EXCLUDE a POST FORMAT (gallery) from the loop. I am able to successfully show just the post format without the other posts using similar tax_query but when I try and exclude the post format with the tax_query 'operator' => 'NOT IN' it does exclude that post format, but then it endlessly repeats and loops all of the other posts. I'm not sure what I'm doing wrong here. I only want each post to display once, but just not those with the post-format-gallery.

A page with ONLY the post format that successfully works:

<?php

get_header(); ?>

<div class="site-content clearfix">

    <?php if (is_active_sidebar('sidebar1')) : ?>

        <div class="main-column">

    <?php endif; ?>

        <?php if (have_posts()) :
            while (have_posts()) : the_post(); ?>
            
                <article class="page">
                    <h2><?php the_title(); ?></h2>

                    <?php $args = array(
                        'post_type'=> 'post',
                        'post_status' => 'publish',
                        'order' => 'ASC',
                        'tax_query' => array(
                            array(
                                'taxonomy' => 'post_format',
                                'field' => 'slug',
                                'terms' => array( 'post-format-gallery' )
                            )
                        )
                    );

                    $galleryPosts = new WP_Query ( $args );

                    if ($galleryPosts->have_posts()) :
                    while ($galleryPosts->have_posts()) : $galleryPosts->the_post(); ?>

                    <div class="page portfolio">
                        <?php the_post_thumbnail(); ?>
                        <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                        <?php the_excerpt(); ?>
                    </div>
                
                    <?php endwhile;

                        else :
                    
                    endif; 
                    wp_reset_postdata();

                    ?>

                </article>

        <?php endwhile;

            else :
                echo '<p>No content found</p>';
            endif; ?>

    <?php if (is_active_sidebar('sidebar1')) : ?>

        </div>

    <?php endif; ?>


    <?php get_sidebar(); ?>

</div>


<?php get_footer();

?>

Using similar code, with the attempt to EXCLUDE the post format works, but then endlessly loops and repeats all other posts:

    <article class="post">

        <?php $args = array(
            'post_type'=> 'post',
            'post_status' => 'publish',
            'order' => 'DESC',
            'tax_query' => array(
                array(
                    'taxonomy' => 'post_format',
                    'field' => 'slug',
                    'terms' => array( 'post-format-gallery' ),
                    'operator' => 'NOT IN',
                )
            )
        );

        $noGalleryPosts = new WP_Query ( $args );

        if($noGalleryPosts->have_posts()) :
            while($noGalleryPosts->have_posts()) : $noGalleryPosts->the_post();

        ?>

        <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
        <p class="post-info"><?php the_time('F jS, Y'); ?> 
        | by 
        <a href="<?php echo get_author_posts_url(get_the_author_meta('ID')); ?>"><?php the_author(); ?></a> 
        | Posted in
        <?php 
        $categories = get_the_category();
        $separator = ", ";
        $output = '';

        if ($categories){

            foreach ($categories as $category) {
                $output .= '<a href="' . get_category_link($category->term_id) . '">' . $category->cat_name . '</a>' . $separator;
            }

            echo trim($output, $separator); 
        }
        ?></p>
        
            <?php if (!is_single() ) { ?>

                        <P>
                        <?php echo get_the_excerpt(); ?> 
                        <a href="<?php the_permalink(); ?>">Read more&raquo;</a>
                        </p>


                <?php } elseif (is_single() ) {
                    the_post_thumbnail('banner-image');         
                    the_content();

            } else {

                the_content();          

            } ?>

        <?php endwhile;

                else :
                    
            endif; 
            wp_reset_postdata();

            ?>



    </article>

</div>
  • 写回答

1条回答 默认 最新

  • doushi3803 2015-12-09 23:20
    关注

    Solved it. The page that was repeating all of the blog posts was on content.php which index.php was linking to. I had a loop on the index which the content was inside so it was trying to run it over and over again.

    Solution: Remove the loop from index.php and leave content.php the same.

    EDIT: this original solution of mine caused all sorts of other problems so I simply changed the index.php file to the following and took the loop out of content.php

        <?php
    
    get_header(); ?>
    
    <div class="site-content clearfix">
    
        <?php if (is_active_sidebar('sidebar1')) : ?>
    
            <div class="main-column">
    
        <?php endif; ?>
    
    
    
                <?php if (have_posts()) :
    
                    $args = array(
                      'tax_query' => array(
                        array(
                          'taxonomy' => 'post_format',
                          'field' => 'slug',
                          'terms' => 'post-format-gallery',
                          'operator' => 'NOT IN'
                        )
                      )
                    );
                    query_posts( $args );
    
    
                while (have_posts()) : the_post();
    
                get_template_part('content', get_post_format());
    
                endwhile;
    
                else :
                    echo '<p>No content found</p>';
    
                endif; ?>
    
    
        <?php if (is_active_sidebar('sidebar1')) : ?>
    
            </div>
    
        <?php endif; ?>
    
    
        <?php get_sidebar(); ?>
    
    </div>
    
    
    
    <?php get_footer();
    
    ?>
    

    For the time being this solves my problem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题