您好我是PHP的新手,我在循环中将一个类添加到一组三个项目时遇到问题。所以基本上我需要项目1-3在循环中没有类,项目4-6有类,项目7-9没有类,项目10-12没有类等等。我知道我需要添加一个计数器,但我不知道如何编写if语句。
感谢任何帮助。提前谢谢!
抱歉,这是代码的示例
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 :(得分: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(...);
...
答案 1 :(得分: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>