Theme template for single product that is category based?

Is it possible to theme product pages per category?

Say I have 2 categories textbooks and elearning

I'd like the individual textbook products to show up one way...and the individual elearning products to show up another way.

I have a textbook info meta box with about 10 custom fields that I need to display for textbooks in a certain layout..and the elearning products have a different meta box with about 5 custom fields that will be laid out differently.

I created my mp_product.php to show my textbook products...and it's working great. And I was in the process of doing checks for category and displaying based on that...when i thought I'd ask before I face rolled the code...

I see that I could do mp_product-PRODUCTNAME or PRODUCTID but is/could it be mp_product-CATEGORY?

So, does this all make sense?

  • aecnu

    Greetings mobiousdesign,

    Great Question!

    Is it possible to theme product pages per category?

    Almost anything is possible with open source and being able to code :slight_smile:

    However, you may not have to re-invent the wheel by possibly using one of these two plugins, GYS Themed Categories plugin or Custom Category Templates plugin

    If you try one or both of them out let me know how it goes for you.

    Cheers, Joe

  • mobiousdesign

    Just doing a copy/paste from my question over at stackexchange...anyone have any hints for me?

    So, I'm a hacker...not in the I'm good at computers sense, but the I hack away blindly til I get what I want...I'm learning more every day. I have grasped some pretty hard topics...but some of the basics elude me, So here's what I'm doing:

    Marketpress allows the standard theming of the store, I could do for a single product (mp_product-PRODUCTNAME.php, mp_product-PRODUCTID.php, or mp_product.php) and the same with category list pages (mp_category-CATEGORYSLUG.php, mp_category-CATEGORYID.php, mp_category.php, mp_taxonomy.php, mp_productlist.php) etc etc etc.

    What I'm wanting to do is have different mp_product.php templates for different categories. I asked over at WPMUDEV and they suggested I take a look atGYS Themed Categories plugin or Custom Category Templates plugin And I did...GYS seems to be the way to go. But even plugin author says it doesn't work with custom taxonomy. GYS does exactly what I want to do, I just need to hack it to work they way I want.

    So, here's the original GYS php and here's where I am so far with this.

    Plugin author even addresses the multiple categories possiblity...but I'm going on the assumption that there will (in my case) only ever be one category. With that in mind...here's how far I've gotten.

    First, I added these actions to my functions.php:

    if(class_exists('GYSThemedCategories')) {
    if(isset($GYSThemedCategories)) {
    add_action('product_category_edit_form_fields',array(&$GYSThemedCategories,'EditCategoryForm'));
    add_action('product_category_add_form_fields',array(&$GYSThemedCategories,'EditCategoryForm'));
    add_action('create_product_category',array(&$GYSThemedCategories,'SaveCategory'));
    add_action('edit_product_category',array(&$GYSThemedCategories,'SaveCategory'));
    }
    }

    Which makes the setting and selecting of the child theme for my custom taxonomy work.

    Then I changed

    // get Post ID from URL
    $pid=url_to_postid($url);

    to

    // get Post ID from URL with bwp_url_to_postid
    $pid=bwp_url_to_postid($url);

    here's what bwp_url_to_postid is And it pulls the Post_ID from the URL even in a custom post type.

    And then I run into a snag.

    If I manually set $cid (the variable to tell the plugin which child theme to use) to 14 (the tag_id of my custom taxonomy called textbook) everything works like a dream. It pulls mp_product from a child theme that I set up (the child theme has style.css and mp_product.php)

    I was trying to figure out how I can get the cat_id/term_id by post_id for this purpose. I can get to the post_id...and if I can get the cat_id/term_id I can continue and make this work.

    What I've tried so far is:

    $terms = wp_get_object_terms( $pid, 'product_category', array('fields'=>'ids'))
    $ids = wp_list_pluck( $terms, 'term_id' );
    $cid = array_pop($ids)

    Also tried get_the_terms with basically the same result...

    and it I'm failing here...as I stated...I'm a hacker...no real knowledge...just trying to use logic to make things work. Let me know if there's any thing else I need to add to this question. (which in a nutshell, in this class of GYSThemedCategories, how do I get the term_id from the post_id to continue the process)

  • mobiousdesign

    So I'm still playing around with this with the plugin author...but in the mean time...a workaround for anyone else trying to do something like this, I used template parts in the mp_product.php template. Of course this goes on the assumption of only 1 category per product...but for me it's working.

    <?php
        $terms = get_the_terms( $pid, 'product_category' );
        	foreach( $terms as $term ) {
        	$cids [0] = $term->term_id;
        	unset($term);
        }
        $cat = array_pop($cids);
    if ($cat == 14) { //textbook
        get_template_part( 'ctech-store-tb', 'store' );
    } else { //iMod
        get_template_part( 'ctech-store-imodkl', 'store' );
    } ?>

    Template Part for the win....

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.