Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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; ?>
share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

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.

share|improve this answer
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 '13 at 20:53
    
Thanks i wasn't know that. –  Rahil Wazir Sep 3 '13 at 21:05
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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