I am developing a wordpress website which uses woocommerce for e-commerce functionality. I have 3 categories on the website and each one will have it's own template assigned for products within these categories.
I have created the templates and have got two of them working fine. However I'm not sure how to call the third template within my single-product.php file which contains the following code to change the templates depending on what category the product is assigned to:
<?php while ( have_posts() ) : the_post(); ?>
<?php global $post;
$terms = wp_get_post_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ) $categories[] = $term->slug;
if ( in_array( 'legal', $categories ) ) {
woocommerce_get_template_part( 'content', 'single-product-legal' );
} else {
woocommerce_get_template_part( 'content', 'single-product-merc' );
} ?>
<?php endwhile; // end of the loop. ?>
the templates i have are:
single-product-legal (custom template)
single-product-merc (default woocommerce template)
single-product-show (custom template)
The categories are legal, show and merchandise.
I need help with the php code so I can switch between the 3 templates. Not sure if I should use a switch statement, or how to implement it or if I could use elseif or how to implement that. Even if there's a completely different way to achieve this, I'd love to know.
Any pointers would be appreciated.
The best it to use elseif:
if ( in_array( 'legal', $categories ) ) {
woocommerce_get_template_part( 'content', 'single-product-legal');
}elseif (in_array('show', $categories)){
woocommerce_get_template_part('content', 'single-product-show');
}else {
woocommerce_get_template_part( 'content', 'single-product-merc');
}
Rather than adding a seperate elseif for merchandise you can just do it in the else like above because by default if it is not legal or show it must be merchandise, but you may just add another elseif as well with the same basic structure.
Related
So i try to show a custom taxonomy on the frontend.
The custom taxonomy is from a plugin i use and is called wcs-instructor.
I just want to show a Single Page with the content of the taxonomy by listing the names and images of the Instructors, the image is a custom field.
When i try to Query the taxonomy with get_term or with WP_Term_Query everything shows up nicely as long as im logged in with my Admin Account (havent actually tested another account).
When i log out and open the Page again its blank.
The structur is as follows.
Custom Taxonomy: wcs-instructor
Page Endpoint: /trainer
Custom Single page: page-trainer.php
get_header();
?>
<main id="primary" class="site-main">
<?php
while ( have_posts() ) :
the_post();
get_template_part( 'template-parts/content', 'trainer' );
endwhile;
?>
</main><!-- #main -->
<?php
get_footer();
content-trainer.php (striped some html thats unnecessary)
<div class="content">
<?php
$term_query = new WP_Term_Query(
array( 'taxonomy' => 'wcs-instructor',) );
if ( ! empty( $term_query->terms ) ) {
foreach ( $term_query ->terms as $term ) {
echo $term->name;
}}
else {
echo 'No term found.';
}
?>
</div>
I know that the code above only gives me the results that are not empty, but the taxonomy got entries that aren't.
Also the Code below also doesn't work.
<?php
$terms = get_terms( array(
'taxonomy' => 'wcs-instructor',
'hide_empty' => false,
) );
foreach ( $terms as $term )
{
echo $term->term_id;
echo '<br>';
echo $term->name;
}
?>
And i just cant find the reason why because when i go to /wcs-instructor/testinstructor i can view the content even when im not logged in.
Any hints on how to debug this behavior or why it's behaving like this in the first place?
Sounds like that taxonomy might have the publicly_queryable arg set to false. Not sure which plugin you are using to manage your custom taxonomies but the CPT UI Plugin has a setting for that argument.
The Public Queryable setting for taxonomies in CPT UI plugin
I've created a advanced-custom-field of the type true/false. The purpose is to either show some products of my woocommerce shop or not. For some reason, the result is always NULL and I don't know why. I also sat the field only show on the shop page (which uses the archive-products.php)
Here is what I try:
if ( woocommerce_product_loop() ) {
woocommerce_product_loop_start();
if ( wc_get_loop_prop( 'total' ) ) {
while ( have_posts() ) {
the_post();
do_action( 'woocommerce_shop_loop' );
wc_get_template_part( 'content', 'product' );
}
}
if(get_field('show_tab')) : ?>
<li class="extra">';
...show some stuff
</li>
<?php endif;
echo '</ul>';
} else {
do_action( 'woocommerce_no_products_found' );
}
Like mentioned above, the True/False field always returns NULL
Can someone help me out
Your get_field('show_tab') is outside of the loop of the products. Put it inside the while loop.
You are using get_field() outside of the Loop, so you need to also give it the ID of the post you are trying to get the field from, like so:
if(get_field('show_tab', $post_id)) : ?>
Having said that, your code looks a bit messy, so I'm assuming that you meant for it to be within the loop. If so, move that all up, perhaps after do_action( 'woocommerce_shop_loop' ); then you won't need to add the ID and it should work as is.
So I have decided I wanted 2 different templates for my Woocommerce shop.
I found a nice little chunk that basically told me to edit taxonomy-product_cat.php
All of my shop pages are category/archive pages. And My Parent Categories have about 50 Sub Cats a piece.
So My code is as follows in taxonomy-product_cat.php :
if (is_product_category( 'outdoor-furniture' ) ){ wc_get_template( 'archive-product.php' );
}
else { wc_get_template( 'archive-product-list.php' );
}
The problem is that archive-product.php is only being applied to the "outdoor-furniture" category and none of its subcategories.
I understand I could list || is_product_category( 'another-cat' ) but to do it for 50 is ridiculous.
I have tried several things for several hours. Stuff that involved filters to the functions file. If statements for this taxonomy-product_cat file. I cant seem to get anything working.
As always any help is greatly appreciated.
Cheers
Just testing your code:
if (is_product_category( 'books' ) || is_product_category( 'magazines' ) ):
wc_get_template( 'archive-product.php' );
else:
wc_get_template( 'archive-product-list.php' );
echo 'Hellllllooooo world';
endif;
Actually worked for me in switching the Woocommerce templates depending on the product category in view.
High All,
I found a working solution:
if ( has_term( 'outdoor-furniture', 'product_cat' ) ) {
wc_get_template( 'archive-product.php' );
}
else { wc_get_template( 'archive-product-list.php' );
}
Thanks for anyone who viewd. Hope this helps someone in the future!
Cheers
I am trying to make different template file depending on the category a certain Woocommerce product is listed in.
I have Googled endlessly to find a solution. I have tried creating a template with the name "taxonomy-product_cat-[slugnamehere].php" and it didn't work. I have tried making making a template file, then, using if/else statements setting the template file in single_product.php. It isn't working either.
The category for my product is called 'box'. Below is the code from single_product; it isn't seeming to work. Any advice would be appreciated:
} else if ( is_product_category() ) {
if ( is_product_category( 'box' ) ) {
woocommerce_get_template_part( 'content', 'single-product-sample-box-4');
}
}
else {
woocommerce_get_template_part( 'content', 'single-product');
}
I want, if the category is 'box' is selected, for the content-single-product-sample-box-4 template to be displayed - instead only the content-single-product template is being displayed.
I found an answer! For anyone else struggling with this I will post my solution... I was only trying to change the template for ONE product (it was a sample box and therefore different from all other items).
Instead of this:
} else if ( is_product_category() ) {
if ( is_product_category( 'box' ) ) {
woocommerce_get_template_part( 'content', 'single-product-sample-box-4');
}
}
else {
woocommerce_get_template_part( 'content', 'single-product');
}
I did this:
} else if ( get_the_ID() == [PAGE ID GOES HERE] ) {
woocommerce_get_template_part( 'content', 'single-product-sample-box-4');
}
else {
woocommerce_get_template_part( 'content', 'single-product');
}
I've a weird problem, some posts appears in categories where they are not in.
When I look in my backoffice and filter by categories, some post appears there but they are not checked in.
The resultat is that in the front office they appear too.
This is my category.php (but I don't think it's the matter)
<?php
get_header();
?>
<section id="wrapper" class="page <?php echo get_query_var('cat'); ?>">
<div id="container">
<?php
$category = get_category(get_query_var('cat'));
$cat_id = $category->cat_ID;
query_posts('showposts=1&cat='.$cat_id);
if ( have_posts() ) :
while ( have_posts() ) : the_post();
get_template_part( 'content', get_post_format() );
endwhile;
endif;
?>
</div>
</section>
<?php
get_footer();
?>
I looked in the table "_term_relationships" and everything is right, they're not in the wrong categories.
So maybe someone have a clue to find out ?
PS : I'm using WPML, but if I desactive it, it's the same problem
You should not use query_posts(),
see (https://wordpress.stackexchange.com/questions/1753/when-should-you-use-wp-query-vs-query-posts-vs-get-posts)
try this:
<?php
$category = get_category(get_query_var('cat'));
$cat_id = $category->cat_ID;
$args = array( 'category' => $cat_id );
$query2 = new WP_Query($args);
if ( $query2->have_posts() ) :
while ( $query2->have_posts() ) :
$query2->the_post();
get_template_part( 'content', get_post_format() );
endwhile;
endif;
?>
First of all, never use query_posts to construct any type of query
Note: This function isn't meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination).
Secondly, never change the the main query for a custom query on any type of archive page or home page. The correct way is to use pre_get_posts to alter the query variables before the main query executes. Check out this post I've done a while ago
Thirdly, category pages in Wordpress does work in a strange way. When a category page is visited, it will display posts from the selected category and posts from the selected category's child categories. I bet this is what you are seeing. This is pretty normal behavior. If you need to change this, have a look at this answer on WPSE by #ialocin. For the benefit of this answer, here is the solution
add_filter(
'parse_tax_query',
'wpse163572_do_not_include_children_in_category_archive_parse_tax_query'
);
function wpse163572_do_not_include_children_in_category_archive_parse_tax_query( $query ) {
if (
! is_admin()
&& $query->is_main_query()
&& $query->is_category()
) {
// as seen here: https://wordpress.stackexchange.com/a/140952/22534
$query->tax_query->queries[0]['include_children'] = 0;
}
}