2015-08-28 59 views
1

我试图根据创建帖子的时间在我的博客帖子页面上创建不同的布局。因此博客文章1将有一个类,而文章2将有另一个类,而文章3将有一个最终的类。很难解释,但这里是我的代码,希望它会更有意义:Blog post layouts wordpress

 <?php 

      if(have_posts()): 

       while(have_posts()): the_post(); 

       $counter = 1; 

       ?> 

       <?php if($counter == 1){ ?> 

        <div class="new-row"> 

        <div class="boxes picture-box contact-smaller-box"> 

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

        </div><!--/.picture box--> 




        <div class="boxes white-box contact-smaller-box"> 

        <div class="box-inner right-side"> 

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

        <p><?php echo substr(get_the_excerpt(), 0,70); ?></p> 
        <a href="<?php the_permalink(); ?>" title="find out more" class="find-out pink-link">Read More</a> 

        </div><!--/.box inner--> 

        <div class="arrow arrow-left"><img src="..."></div> 


        </div><!--/.white box--> 

        </div><!--/.new row--> 

        <?php $counter = $counter++; ?> 


       <?php } if($counter == 2){?> 


        <!-- second section of the blog post content --> 



        <div class="new-row"> 

        <div class="boxes pink-box contact-smaller-box"> 

        <div class="box-inner"> 

        <h2><?php the_title(); ?></h2> 

        <p><?php echo substr(get_the_excerpt(), 0,70); ?></p> 

        <a href="<?php the_permalink(); ?>" title="find out more" class="find-out purple-link">Read More</a> 

        </div><!--/.box inner--> 

        <div class="arrow arrow-right"><img src="..."></div> 


        </div><!--/.pink box--> 





        <div class="boxes picture-box contact-smaller-box"> 

        <img src="..." alt="hearing aids for adults"> 

        </div><!--/.picture box--> 

        </div><!--/.new row--> 


       <?php } ?> 


      <?php 

       endwhile; 

      endif; 

     ?> 

在我的两个测试柱都表示作为第一类职位的时刻,所以即时通讯思想我的柜台工作不正常,或我有我的div在错误的位置做这项工作。即时通讯新的工作与PHP,所以我不知道我要去哪里错了。任何建议都会很棒。

回答

0

经过约4个小时的研究和测试不同的东西出来我已经找到了解决方案(credit):

<?php if (have_posts()) : ?> 
<?php $count = 0; ?> 
<?php while (have_posts()) : the_post(); ?> 
<?php $count++; ?> 
<?php if ($count == 1) : ?> 

Add your Custom Post Divs Here for the 1st post. 

<?php elseif ($count == 2) : ?>  

Add your Custom Post Divs Here for the 2nd post.   

<?php elseif ($count == 3) : ?> 

Add your Custom Post Divs Here for the 3rd post.  

<?php elseif ($count == 4) : ?> 

Add your Custom Post Divs Here for the 4th post.  

<?php else : ?> 

Add your Custom Post Divs Here for the rest of the posts. 

    <?php endif; ?> 
<?php endwhile; ?> 
<?php endif; ?> 

希望它可以帮助别人了。

相关问题