1

enter image description here

on my page category.php file i'm using product listing,

product block loop is ,

product-block , product-block , product-block last

last class no margin-left,

how to define third product "last class" in the category loop?

my content.php

<div class="goods_list catalog_goods">
            <div class="goods_item">
              <div class="foto">
                <div class="shadow"></div><?php the_excerpt(); ?>
                <a href="<?php the_permalink(); ?>"><?php echo get_the_post_thumbnail($id,  array(220,220)); ?></a>
              </div>
              <div class="bot">
               <a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a>

                <div class="stars"></div>

                <div class="order">
                  <a class="but_buy"><i class="icon-shopping-cart icon-large"></i> &nbsp;Buy </a>
                  <span class="price">$45.00</span>
                </div>
              </div>
            </div>
          </div>

i must define third div good_list catalog_goods last

my category.php file

  <div class="catalog_right">
        <div class="catalog_goods_block">


        <?php if ( have_posts() ) : ?>



            <?php twentyeleven_content_nav( 'nav-above' ); ?>

            <?php /* Start the Loop */ ?>

            <?php while ( have_posts() ) : the_post(); ?>
            <?php
                    /* Include the Post-Format-specific template for the content.
                     * If you want to overload this in a child theme then include a file
                     * called content-___.php (where ___ is the Post Format name) and that will be used instead.
                     */
                    get_template_part( 'content', get_post_format() );
                ?>

            <?php endwhile; ?>

1 Answer 1

1

You can do with php modulus % operator.

Lets work with your code.

    <?php if ( have_posts() ) : ?>



        <?php twentyeleven_content_nav( 'nav-above' ); ?>

        <?php /* Start the Loop */ ?>
        <?php $count = 1; ?>
        <?php while ( have_posts() ) : the_post(); ?>
        <?php
                /* Include the Post-Format-specific template for the content.
                 * If you want to overload this in a child theme then include a file
                 * called content-___.php (where ___ is the Post Format name) and that will be used instead.
                 */
                get_template_part( 'content', get_post_format() );
            ?>

        <?php endwhile; ?>

Add $count=1

Then in your content.php:

    <?php
        global $count;
        if ($count % 3 === 0) {
            $no_margin = 'last';
        }
    ?>    
    <div class="goods_list catalog_goods <?php echo $no_margin; ?>">
                    <div class="goods_item">
                      <div class="foto">
                        <div class="shadow"></div><?php the_excerpt(); ?>
                        <a href="<?php the_permalink(); ?>"><?php echo get_the_post_thumbnail($id,  array(220,220)); ?></a>
                      </div>
                      <div class="bot">
                       <a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a>

                        <div class="stars"></div>

                        <div class="order">
                          <a class="but_buy"><i class="icon-shopping-cart icon-large"></i> &nbsp;Buy </a>
                          <span class="price">$45.00</span>
                        </div>
                      </div>
                    </div>
                  </div>
<?php ++$count; ?>

Make the $count variable global so it can be accessible.

Heres the main part

Remainder of $count divided by 3

and add:

<div class="goods_list catalog_goods <?php echo $no_margin; ?>">

echo $no_margin

At the last add:

<?php ++$count; ?>

so it will increment until the loop finishes.

1
  • 1
    You don't actually have to create your own $count variable at all, WP does it for you: $wp_query->current_post. Just be aware it starts counting at 0.
    – vancoder
    Sep 3, 2013 at 20:53

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.