1

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

2
  • Have you changed the reading options the in the admin about the first page showing something else but the normal home?
    – kevin
    Commented Jan 11, 2012 at 13:40
  • @Kevin: No its a normal homepage. No static page..
    – Sisir
    Commented Jan 11, 2012 at 13:47

1 Answer 1

2

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%;}
3
  • 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' ). Commented Jan 11, 2012 at 18:23
  • @ChipBennett: tried that but $cols is undefined inside the template part.
    – Sisir
    Commented Jan 14, 2012 at 17:54
  • Globalize it, and call the global inside the template-part file? Commented Jan 14, 2012 at 22:37

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.