如何在 wordpress 短代码循环中添加条件
How add condition in wordpress shortcode loop
我想要 2 的倍数关闭 div 并使用简码 post 循环在 wp 循环中打开一个新的 div。
if($i % 2 == 0) {echo '</li><li>';}
$i++;
如何做到?
一般循环:
<?php
$i = 1;
//added before to ensure it gets opened
echo '<li>';
if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post();
// post stuff...
?>
<div class="frCol"><a href="#"><?php the_post_thumbnail('thumbnail', array('class' => 'imgthumb')); ?></a>
<div class="item-info">
<h2><?php the_title(); ?></h2>
</div>
</div>
<?php
// if multiple of 3 close div and open a new div
if($i % 2 == 0) {echo '</li><li>';}
$i++; endwhile; endif;
//make sure open div is closed
echo '</li>';
?>
这是简码:
function testimonial_thumb_shortcode($atts){
extract(shortcode_atts(array(
'category' => ''
), $atts));
$q = new WP_Query(array(
'posts_per_page' => -1,
'post_type' => 'testimonials',
'testimonial_cat' => $category
));
$list = ' <ul>';
while($q->have_posts()):
$q->the_post();
$idd = get_the_ID();
$author_photo = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'thumbnail');
$list .= '/*Main loop*/';
endwhile;
$list.= '</ul>';
wp_reset_query();
return $list;
}
add_shortcode('tthumb', 'testimonial_thumb_shortcode');
根据您的评论,这可能有帮助:
function testimonial_thumb_shortcode($atts){
extract(shortcode_atts(array(
'category' => ''
), $atts));
$q = new WP_Query(array(
'posts_per_page' => -1,
'post_type' => 'testimonials',
'testimonial_cat' => $category
));
$list = ' <ul><li>';
$i = 0; //init the counter
while($q->have_posts()):
$q->the_post();
$idd = get_the_ID();
$author_photo = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'thumbnail');
$list .= '/*Main loop*/';
$i++; //increase counter
if($i % 2 == 0){
$list .= '</li><li>';
}
endwhile;
$list.= '</li></ul>';
wp_reset_query();
return $list;
}
add_shortcode('tthumb', 'testimonial_thumb_shortcode');
我想要 2 的倍数关闭 div 并使用简码 post 循环在 wp 循环中打开一个新的 div。
if($i % 2 == 0) {echo '</li><li>';}
$i++;
如何做到?
一般循环:
<?php
$i = 1;
//added before to ensure it gets opened
echo '<li>';
if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post();
// post stuff...
?>
<div class="frCol"><a href="#"><?php the_post_thumbnail('thumbnail', array('class' => 'imgthumb')); ?></a>
<div class="item-info">
<h2><?php the_title(); ?></h2>
</div>
</div>
<?php
// if multiple of 3 close div and open a new div
if($i % 2 == 0) {echo '</li><li>';}
$i++; endwhile; endif;
//make sure open div is closed
echo '</li>';
?>
这是简码:
function testimonial_thumb_shortcode($atts){
extract(shortcode_atts(array(
'category' => ''
), $atts));
$q = new WP_Query(array(
'posts_per_page' => -1,
'post_type' => 'testimonials',
'testimonial_cat' => $category
));
$list = ' <ul>';
while($q->have_posts()):
$q->the_post();
$idd = get_the_ID();
$author_photo = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'thumbnail');
$list .= '/*Main loop*/';
endwhile;
$list.= '</ul>';
wp_reset_query();
return $list;
}
add_shortcode('tthumb', 'testimonial_thumb_shortcode');
根据您的评论,这可能有帮助:
function testimonial_thumb_shortcode($atts){
extract(shortcode_atts(array(
'category' => ''
), $atts));
$q = new WP_Query(array(
'posts_per_page' => -1,
'post_type' => 'testimonials',
'testimonial_cat' => $category
));
$list = ' <ul><li>';
$i = 0; //init the counter
while($q->have_posts()):
$q->the_post();
$idd = get_the_ID();
$author_photo = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'thumbnail');
$list .= '/*Main loop*/';
$i++; //increase counter
if($i % 2 == 0){
$list .= '</li><li>';
}
endwhile;
$list.= '</li></ul>';
wp_reset_query();
return $list;
}
add_shortcode('tthumb', 'testimonial_thumb_shortcode');