0

I want to multiple of 2 close div and open a new div in wp loop using shortcode post loop.

if($i % 2 == 0) {echo '</li><li>';}

  $i++;

How Can do it?

General loop:

<?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>';
 ?>

This is the 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>';
    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');
5
  • I have tested it and it works right. Could you explain what is exactly the problem? Commented Mar 7, 2015 at 17:07
  • How can I work it by using wordpress shortcode... Commented Mar 7, 2015 at 17:44
  • Still not clear - could you outline somehow the desired output? Commented Mar 7, 2015 at 18:05
  • I want to same output by using wordpress shortcode like [shortcode] Commented Mar 7, 2015 at 18:08
  • I want to use the condition if($i % 2 == 0) {echo '</li><li>';} in shortcode: Commented Mar 7, 2015 at 18:30

1 Answer 1

1

Based on your comment, maybe this helps:

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');
Sign up to request clarification or add additional context in comments.

5 Comments

I need to use in custom post query loop in a shortcode.
i really don't understand you, sorry
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');
How can I use if($i % 2 == 0) {echo '</li><li>';} in above shortcode?
Another thing need to know? Where need to use other <li> and </li>. Actually the output will be look like: <ul><li><div>Here loop post</div><div>Here loop post</div></li><li><div>Here loop post</div><div>Here loop post</div></li><li><div>Here loop post</div><div>Here loop post</div></li><li><div>Here loop post</div><div>Here loop post</div></li></ul>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.