0

I've made a custom template for a category called "shop" on the products tab. I've read some of the previous topics that mention doing an if conditional to the templete checking the category its in.

I've tried this and had no luck so went to making a template called single-product-shop.php, I then wrote a conditional to load the particular page template and placed it within single-product.php.

However Now I seem to generate a php warning.

            <?php while ( have_posts() ) : the_post(); ?>

<?php
        if( has_term( 'shop', 'product_cat' ) ) {
            $file = 'single-product-shop.php';
        } else {
            $file = 'single-product-default.php';
        }

        global $woocommerce;

        load_template( $woocommerce->template_url . $file );
?>

                <?php endwhile; // end of the loop. ?>

If someone could please point out what I'm doing wrong, I would be most greatful.

Thanks

  • where do you put single-product-shop.php template ? – Ravinder Kumar Dec 2 '13 at 17:39
2

Woocommerce has it's own function woocommerce_get_template_part() for loading template parts, use it like this:

woocommerce_get_template_part( $slug, $name );

which refers to the according file {$slug}-{$name}.php or used like this:

woocommerce_get_template_part( $slug );

refers to the file {$slug}.php, it automatically

Looks in yourtheme/slug(-name).php and yourtheme/woocommerce/slug(-name).php

it can be used with a conditional like this:

    if( has_term( 'shop', 'product_cat' ) ) {
        woocommerce_get_template_part( 'single-product-shop' );
    } else {
        woocommerce_get_template_part( 'content', 'single-product' );
    }
  • I understand this - Its what I've overwritten. I was looking for a way to use this and a conditional to load another template that I've created for the shop category? – dannyw24 Dec 2 '13 at 16:56
  • I just proposed this, because it's probably best to use function available for this purpose. Besides that, are you sure you have the file in right place, because your error message suggest otherwise. @user2478101 – Nicolai Dec 2 '13 at 17:04
  • 1
    Thanks Ialocin - this makes sense to me now. I've got the file in themes/choosentheme/woocommerce/single-product-shop.php, guessing I need to move it to the theme root. – dannyw24 Dec 2 '13 at 17:52
  • I'll try this and then tick your awnser. – dannyw24 Dec 2 '13 at 20:27

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