Loop with break in the middle

时间:2013-09-21 作者:mantis

我正在尝试创建一个图库类型的页面,其中显示每个帖子类别中的一个帖子缩略图。问题是,我想在一个九方形网格的中间显示页面标题和指向公文包页面的链接。我在想最好的方法是在三列中显示四个缩略图,然后显示页面标题,然后再显示四个缩略图。我有前四个这样的:

Updated code:

 <?php
    $count = 0;
    $taxonomy = \'category\';//  e.g. post_tag, category
    $param_type = \'category__in\'; //  e.g. tag__in, category__in
    $term_args=array(
      \'orderby\' => \'name\',
      \'order\' => \'ASC\',
    );
    $terms = get_terms($taxonomy,$term_args);
    if ($terms) { ?>

    <ul class="row portfolio-entries">
     <?php               
      foreach( $terms as $term ) {
        $args=array(
          "$param_type" => array($term->term_id),
          \'post_type\' => \'post\',
          \'post_status\' => \'publish\',
          \'posts_per_page\' => 1,
          \'caller_get_posts\'=> 1
          );
        $my_query = null;
        $my_query = new WP_Query($args);
        $post_count = $my_query->post_count;

        if( $my_query->have_posts() ) : 

            $count++;               
            if ($count <= 4) {          
            while ($my_query->have_posts()) : $my_query->the_post(); 
            ?>                      
                        <li class="span4 box portfolio-entry">
                                    <div class="hover-state align-right">
                                        <h2><?php the_category(); ?></h2>

                                        <h3><?php the_title(); ?></h3>
                                        <em>Click for details</em>
                                    </div><!-- end hover-state -->
                                    <?php if (has_post_thumbnail()) : ?>
                                    <figure><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a></figure>
                                    <?php endif; ?>                           
                        </li>

    <?php 
            endwhile;           
            } 
        endif;

      } /* end foreach term*/
      ?>                        
                        <li class="span4 portfolio-entry">
                            <header class="align-center">
                                <h1>Galleries</h1>

                                <div class="cta align-center">
                                    <a href="<?php echo home_url(); ?>/portfolio" class="btn btn-primary">See Full Portfolio</a>
                                </div><!-- end cta -->
                            </header>
                        </li>

        <?php 
        if ($count >= 4) {
            while ($my_query->have_posts()) : $my_query->the_post(); 
            ?>                      
                        <li class="span4 box portfolio-entry">
                                    <div class="hover-state align-right">
                                        <h2><?php the_category(); ?></h2>

                                        <h3><?php the_title(); ?></h3>
                                        <em>Click for details</em>
                                    </div><!-- end hover-state -->
                                    <?php if (has_post_thumbnail()) : ?>
                                    <figure><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a></figure>
                                    <?php endif; ?>                           
                        </li>                                       
    <?php endwhile; 
     } ?>
        </ul>   <?php
    } /* end if terms */
                wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
我的问题是第二个循环。我已经得到了if声明if ($count > $more_cats_to_show && $count <= $max_count) 但我似乎无法获得接下来的4个缩略图。

2 个回复
SO网友:Caroline

尝试使用基于post编号计数的循环计数器-提供更大的灵活性

<?php if (have_posts()) : ?>
   <?php $count = 0; ?>
       <?php while (have_posts()) : the_post(); ?>
           <?php $count++; ?>
    // if you are in your first post
           <?php if ($count == 1) : ?>
    // do whatever you want to display your posts *assuming you want a featured top level*
           <?php elseif ( in_array($count, array( 2,3,4,5 ) )) : ?>      
    // do whatever you want to display your posts
           <?php elseif ($count == 6 ) : ?>  
    // do whatever you want to display your posts - this is probably the middle guy on a 3x3 grid with a featured 1st post
           <?php elseif ( in_array($count, array( 7,8,9,10 ) )) : ?>      
           <?php else : ?>
           <?php endif; ?>
        <?php endwhile; ?>
    <?php else : ?>
<?php endif; ?>

SO网友:Caroline

This is 100% untested, 但你可能想试试这个/胡闹一下

<?php 
$args = array(
    \'orderby\' => \'name\',
    \'order\' => \'ASC\',
);
$categories = get_categories( $args );
    foreach( $categories as $category ) 
        {
            $loopcounter = 0; 
            $args = array(
                \'post_type\' => \'post\',
                \'post_status\' => \'publish\',
                \'posts_per_page\' => 1,
                \'caller_get_posts\'=> 1
                );
                $the_query = new WP_Query( $args );
                while ( $the_query->have_posts() ) : $the_query->the_post();    $loopcounter++;  ?>
                    <?php if ( in_array($loopcounter, array( 1,2,3,4 ) )) : ?>      

                    <li class="span<?php echo $loopcounter; ?> box portfolio-entry loopcounter-<?php echo $loopcounter; ?>">
                                                        <div class="hover-state align-right">
                                                            <h2><?php the_category(); ?></h2>

                                                            <h3><?php the_title(); ?></h3>
                                                            <em>Click for details</em>
                                                        </div><!-- end hover-state -->
                                                        <?php if (has_post_thumbnail()) : ?>
                                                        <figure><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a></figure>
                                                        <?php endif; ?>                           
                    </li>

                    <?php elseif ($loopcounter == 5 ) : ?>  
                        <h3>Title whatever this is goes here</h3>
                    <?php elseif ( in_array($loopcounter, array( 6,7,8,9 ) )) : ?>      
                        <li class="span<?php echo $loopcounter; ?> box portfolio-entry loopcounter-<?php echo $loopcounter; ?>">
                                                            <div class="hover-state align-right">
                                                                <h2><?php the_category(); ?></h2>

                                                                <h3><?php the_title(); ?></h3>
                                                                <em>Click for details</em>
                                                            </div><!-- end hover-state -->
                                                            <?php if (has_post_thumbnail()) : ?>
                                                            <figure><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a></figure>
                                                            <?php endif; ?>                           
                        </li>               
                            <?php else : ?>
                             <?php endif; ?>
                <?php endwhile; } wp_reset_postdata();  ?>

结束

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post