2016-02-02 54 views
-2

嗨我是相当新的PHP,我有问题添加一个类到一个循环中的三个项目组。所以基本上我需要循环中的项目1-3没有类,项目4-6有类,项目7-9没有类,项目10-12没有类,等等。我知道我需要添加一个计数器,但我不知道如何编写if语句。如何将类添加到循环php中的某些项目?

任何帮助表示赞赏。提前致谢!

对不起这里是代码

global $wp_query; 
$wp_query = new WP_Query($args); 
if($wp_query->have_posts()): 
while($wp_query->have_posts()): $wp_query->the_post(); global $post; 

    echo '<article class="recipe"> 
     <div class="recipe-img"><a href="'.get_permalink().'">'.get_the_post_thumbnail($id, 'featured').'</a></div> 
     <div class="recipe-info"> 
      <span>'.get_the_date().'</span> | <span>'.get_the_author().'</span> 
      <a href="'.get_permalink().'"> 
       <h3 class="lato">'.get_the_title().'</h3> 
      </a> 
     </div> 
    </article>'; 

} 
endwhile; 
    genesis_posts_nav(); 
endif; 

我能够做到这一点的一个例子,但它只会增加该类每一个第三个项目。

$args = array(
      'post_type' => 'recipes', 
      'orderby' => 'date', 
      'order' => 'DESC', 
      'posts_per_page'=> '3', 
      'paged' => get_query_var('paged'), 
     ); 
      global $wp_query; 
      $wp_query = new WP_Query($args); 
      if($wp_query->have_posts()): 
      $count = 0; 
      while($wp_query->have_posts()): $wp_query->the_post(); global $post; 
      $count++; 
      if ($count % 3 == 0) { 

      echo '<article class="recipe third"> 
       <div class="recipe-img"><a href="'.get_permalink().'">'.get_the_post_thumbnail($id, 'featured').'</a></div> 
       <div class="recipe-info"> 
        <span>'.get_the_date().'</span> | <span>'.get_the_author().'</span> 
        <a href="'.get_permalink().'"> 
         <h3 class="lato">'.get_the_title().'</h3> 
        </a> 
       </div> 
      </article>'; 

      } else { 

      echo '<article class="recipe"> 
       <div class="recipe-img"><a href="'.get_permalink().'">'.get_the_post_thumbnail($id, 'featured').'</a></div> 
       <div class="recipe-info"> 
        <span>'.get_the_date().'</span> | <span>'.get_the_author().'</span> 
        <a href="'.get_permalink().'"> 
         <h3 class="lato">'.get_the_title().'</h3> 
        </a> 
       </div> 
      </article>'; 

      } 
      endwhile; 
      genesis_posts_nav(); 
     endif; 
+0

哇哦,一个代码示例真的是很必要的。即使你可以用另一种语言编写你需要的东西。 – Iwnnay

+1

请阅读[我可以问哪些主题](http://stackoverflow.com/help/on-topic) 和[如何提出一个好问题](http://stackoverflow.com/help/how-to - 问) 和[完美的问题](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) – RiggsFolly

+0

@Iwnnay我已更新我的问题与代码示例。 – Ozzigity

回答

0

只需添加一些额外的控制你的柜台,并改变你的if语句有点:

... 
while($wp_query->have_posts()): $wp_query->the_post(); global $post; 
    $count = $count > 6 ? 1 : $count + 1; 
    if (($count > 3) && ($count <= 6)) { 
     echo(...); 
... 
+0

Thanks @Codeartist!这几乎可行。在项目1-3我没有得到课堂和项目4-6我得到的课程。这很好,但我需要从职位7-9删除类。该代码仍然以7-9的等级离开课程。 – Ozzigity

+0

我修好了!我需要改变的是'$ count> = 6?' – Ozzigity

+0

对不起,我的描述有点困惑。我更新了答案。现在它的工作原理如下:1-3没有班级4-6班,然后计数器回到1和7-9没有班,10-12班没有班。等等。无论如何记住。这是反制。它会统计你的帖子。因此,尝试使用if语句并记住,当$ counter达到7时,它会在您的if条件之前重置为1。 – Codeartist

0
<html> 
<head> 
    <title>Counter Colors</title> 
<style type="text/css"> 
    span{display: inline-block;padding: 10px;border:solid 1px #ccc;} 
    .green{color: green;} 
</style> 
</head> 
<body> 
<?php 
//phpinfo(); 

$class = 'class="green"'; 
$put_class = 1; 

for ($i=1; $i <= 12 ; $i++) { 


    if($put_class == 1){ 
     echo '<span '.$class.'>'.$i.'</span>'; 
    } else{ 
     echo '<span>'.$i.'</span>'; 
    } 

    if($i%3 == 0){ 
     if($put_class == 0){$put_class = 1;} 
     else{$put_class = 0;} 
    } 

} 
?> 
</body> 
</html> 
相关问题