如何阻止循环在我的类别模板中重复?

时间:2012-12-24 作者:ByteMyPixel

出于某种原因,在查看类别时,帖子会重复-基本上整个循环重复的次数与该类别的帖子重复的次数相同。

E.G.:
如果category_a 有3篇文章,页面将显示:

post1-特色
post2
post3

post1-特色
post2
post3

post1-特色
post2
post3

如果category_a 将显示4个帖子:

post1-特色
post2
post3
post4

post1-特色
post2
post3
post4

post1-特色
post2
post3
post4

post1-特色
post2
post3
post4

我使用相同的帖子模板(content-feed.php) 对于主博客页面和类别页面。它在主博客页面上运行良好。

我不确定这是否重要或与此问题有关,但可能值得注意的是:在循环中,我首先拉一个“特色”帖子,然后拉下面的其他帖子(“非特色”)。同样,这在主博客页面上效果很好,这个问题只在分类页面上出现

这是整个帖子模板(content-feed.php):

<div class="row">

<?php while (have_posts()) : the_post(); ?>

        <?php               
            $args = array(
               \'posts_per_page\' => 1,
               \'post_type\' => \'post\',
               \'order\' => \'DESC\', 
               \'category__in\' => ($cat),
               \'meta_query\' => array( 
                  array(
                     \'key\' => \'is_featured\', 
                     \'value\' => \'1\', // only display the latest post marked featured, if there is one
                  )
               )
            );

            $first_query = new WP_Query( $args );
            if ( $first_query->have_posts() ):

            echo \'<div class="widget-box featured beige right">\';

               while( $first_query->have_posts() ) : $first_query->the_post();


               $attachment_id = get_field(\'image\');
               $size = \'customfeatblg\'; // (thumbnail, medium, large, full or custom size)
               $image = wp_get_attachment_image_src( $attachment_id, $size ); 

                       if( get_field(\'image\') ):
                           $thisimage = \'<a href="\' . get_permalink() . \'"><img src="\' . $image[0] . \'" alt="\' . get_the_title() .\'" width="290" height="92" /></a>\';  
                           $divclass = \'summary\';
                       else:
                           $divclass = \'fullsummary\';
                       endif; 

                       if( is_category() ):
                            $category = strip_tags( single_term_title(\'\',false) );
                          echo \'<div class="title main fs-45">\' . $category .\'<span></span></div>\';
                       else:
                            $category = get_the_category();
                          echo \'<div class="title main fs-45">\' . $category[0]->cat_name .\'<span></span></div>\';
                       endif;
        ?>

        <?php
            echo \'

                \' . get_template_part(\'templates/social-shareing\') .\'

                    <div class="the-content">

                        \' . $thisimage .\'
                        <div class="\' . $divclass . \'">
                            <h2 class="purple"><a href="\' . get_permalink() . \'">\' . get_the_title() .\'</a></h2>
                            <p class="date">\' . get_the_date() .\'</p>
                            <p>\' . get_the_excerpt() . \'&hellip; <br>
                            <a href="\' . get_permalink() . \'">More &rsaquo;</a></p>
                        </div>

                    </div>


                \';

           endwhile;
        ?>

        <?php
            echo \' <div class="bottom-cap"></div>
                </div><!-- [END] widget-box -->\';
            endif;
            wp_reset_postdata(); // to reset the loop               
        ?>


    <div class="feed med">
    <?php               
        $args = array(
           \'posts_per_page\' => 10,
           \'post_type\' => \'post\',
           \'category__in\' => ($cat),
           \'meta_query\' => array( 
              array(
                 \'key\' => \'is_featured\', 
                 \'value\' => \'1\',
                 \'compare\' => \'!=\', // only display posts not marked as featured - to prevent repeat from above
              )
           )
        );

        $second_query = new WP_Query( $args );
        if ( $second_query->have_posts() ):
           while( $second_query->have_posts() ) : $second_query->the_post();

           $titlechars = 45; // Character Limit
           $posttitle = get_the_title();
           $modtitle = substr($posttitle, 0, $titlechars);

           $contentchars = 120; // Character Limit
           $postcontent = get_the_excerpt();
           $modcontent = substr($postcontent, 0, $contentchars);

        echo \'<article>\';
    ?>

    <?php
        if( get_field(\'image\') ):

            $attachment_id = get_field(\'image\');
            $size = \'customfeatins\'; // (thumbnail, medium, large, full or custom size)
            $image = wp_get_attachment_image_src( $attachment_id, $size );

        echo \'<a href="\' . get_permalink() . \'"><img src="\' . $image[0] . \'" alt="\' . get_the_title() .\'" width="136" height="90" /></a>\';
    ?>

    <?php else : ?>

        <?php echo \'<a href="\' . get_permalink() . \'"><img src="\'. get_template_directory_uri() .\'/assets/img/content/bf-default.gif" alt="bf-default" width="136" height="90" /></a>\' ?>

    <?php endif; ?>

    <?php
        echo \'
            <h3 class="purple"><a href="\' . get_permalink() . \'">\' . $modtitle .\'</a></h3>
            <p class="date">\' . get_the_date() .\'</p>
            <p>\' . $modcontent . \'&hellip; <a href="\' . get_permalink() . \'">More &rsaquo;</a></p>

            </article>\';
    ?>

    <?php                              
        endwhile;
        endif;
        wp_reset_postdata(); // to reset the loop
    ?>
    </div><!-- [END] feed-->

<?php/*  <div class="pagination"><a href="#">1</a><a href="#">2</a><a href="#">3</a><a href="#">4</a><a href="#">5</a><a href="#">&rsaquo;</a></div>  */?>
<?php
    global $wp_query;

    $big = 999999999; // need an unlikely integer

    echo paginate_links( array(
        \'base\' => str_replace( $big, \'%#%\', esc_url( get_pagenum_link( $big ) ) ),
        \'format\' => \'?paged=%#%\',
        \'current\' => max( 1, get_query_var(\'paged\') ),
        \'total\' => $wp_query->max_num_pages
    ) );
?>

</div><!-- [END] row -->
<?php endwhile; ?>
这是我的category.php 模板:

<?php get_template_part(\'templates/nav\', \'category\'); ?> 
<?php get_template_part(\'templates/content\', \'feed\'); ?>
<?php if (hchw_sidebar()) : ?>
    <?php get_template_part(\'templates/sidebar\', \'right\'); ?>
<?php endif; ?>
我尝试添加/移动<?php wp_reset_query(); ?> 之后<?php endwhile; ?> 在里面content-feed.php 但这并没有改变任何事情。

有没有关于如何解决或防止这种情况发生的想法?

提前感谢您的帮助!

2 个回复
最合适的回答,由SO网友:ByteMyPixel 整理而成

啊,多亏拉尔夫912的帮助,我才找到了解决方案。我需要删除“父”循环-因此替换<?php while (have_posts()) : the_post(); ?> 具有<?php if (have_posts()) : ?> 并更换关闭装置<?php endwhile; ?> 具有<?php endif; ?> 修复了它。

无需在父循环中嵌套特征循环和非特征循环。

再次感谢Ralf912!

SO网友:Ralf912

有三个循环,两个嵌套在主循环中。主回路显示立柱,例如category_a. 第二个嵌套循环显示什么?这个循环似乎还将samne类别中的帖子显示为主循环。因此,请尝试删除第二个嵌套循环。

结束

相关推荐

posts_groupby problem

在我的搜索页面上,我想按帖子类型对帖子进行分组。为此,我有以下几点:add_filter(\'posts_groupby\', \'group_by_post_type\' ); function group_by_post_type( $groupby ) { global $wpdb; if( is_search() ) { return $wpdb->posts.\'.post_type\'; } } 在