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.

Scenario: I have a template part loop-product.php which i am calling from home.php, taxonomy.php and a page template template-products.php. Inside that template part loop-product.php file i am setting the number of columns by checking is_home() conditionals.

Problem: The conditional works fine for homepage, taxonomy page but its still set on the page template. On the product item i have added a class columns-$cols so you can check it on the site. Its showing columns-4 on the product template page where it suppose to show columns-3.

<?php
    $j = 1;
    if(is_home()){
        $cols = 4;
    }else{
        $cols = 3;
    }
    if(have_posts()):while(have_posts()):the_post();

        // getting thumbnail image here 
    ?>

            <div class="product-item <?php echo 'columns-'.$cols ?>" <?php if($j%$cols == 0){ echo 'style="margin-right:0;"'; } ?>>
              <a href="<?php the_permalink(); ?>">
                <span class="product-img">
                    <img src="<?php echo $img[0]; ?>" width="100" height="75" alt="<?php the_title(); ?>">
                </span>
                <span class="desc">
                    <h2><?php the_title(); ?></h2>
                <?php
                    if($price): ?>
                    <span class="price">Price: <?php echo $price; ?> BDT</span>
                <?php endif; ?>                    

                     </span>
                </a>
             </div>
             <?php if($j%$cols == 0){ echo '<div class="clear"></div>'; } ?>

             <?php $j++; endwhile; endif; ?>

                <div class="clear"></div>

Check the site here http://computercarebd.com/ Appreciate your time. Thanks!

Page template code: http://pastebin.com/CzRgzzVF

share|improve this question
    
Have you changed the reading options the in the admin about the first page showing something else but the normal home? –  kevin Jan 11 '12 at 13:40
    
@Kevin: No its a normal homepage. No static page.. –  Sisir Jan 11 '12 at 13:47
add comment

1 Answer

Why not use the body class to target these elements with CSS. You could do something like the following and make your PHP a little cleaner.

.product-item{width:33%;}
.home .product-item{width:25%;}
share|improve this answer
    
Along the same lines, since you are using custom template files anyway, why not just define $cols explicitly in each template file, rather than defining it in the template-part file? e.g. just define $cols = 3 immediately before calling get_template_part( 'loop', 'products' ). –  Chip Bennett Jan 11 '12 at 18:23
    
@ChipBennett: tried that but $cols is undefined inside the template part. –  Sisir Jan 14 '12 at 17:54
    
Globalize it, and call the global inside the template-part file? –  Chip Bennett Jan 14 '12 at 22:37
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.