I'm trying to create specific templates based on custom taxonomy subcategory slugs.
Custom taxonomy is registered with CPT UI plugin.
An example explaining the problem:
Registered custom taxonomy: Fruits
Fruits has a category: Apples
Apples has a subcategories with slugs: red-apple, green-apple, yellow-apple
My goal is to use specific template for each subcategory: red-apple, green-apple and yellow-apple
What I have tried so far:
I created a files such as taxonomy-fruits-red-apple.php, taxonomy-apples-red-apple.php etc - none worked.
Used the following code:
function wpd_subcategory_template( $template ) {
$cat = get_queried_object();
if ( isset( $cat ) && $cat->category_parent ) {
$template = locate_template( 'my-template.php' );
}
return $template;
}
add_filter( 'category_template', 'wpd_subcategory_template' );
None of these methods worked.
Any ideas?
Thx in advance!
Related
I created a Custom Post Type, and it has some categories in it.
I created forms with User Frontend Pro for CPT categories.
I’m looking for a solution for assigning the custom templates to that records to show them on the website.
For example;
Assume that I have a CPT named Project.
The project post type has two categories; “Business” and “Ideas”
Users can post their entries with forms to these categories and their posts listed under the account dashboard.
The issue is that;
Business categories should be shown with the category-business.php custom template, and in a similar way, the Ideas category should be shown with category-ideas.php.
How can I add custom template to CPT categories?
P.S. I tried the template hierarchy solution that is in the WP documents, but it's not working. Because there is a custom view function for content. I looking for a code snippet that adds the template page attributes to custom posts automatically, if it is possible.
Assuming that you are just using the default WordPress category you can the pre_get_posts Hook to include the project as part of the category page if they are not showing by default
function include_project_to_cat_pages( $query ) {
if ( $query->is_category() && $query->is_main_query() ) {
$query->set( 'post_type', array( 'post', 'project' ) );
}
}
add_action( 'pre_get_posts', 'include_project_to_cat_pages' );
I solved that issue with a filter.
add_filter( 'single_template', function( $template ) {
global $post;
$cat = get_the_category()[0];
if ( $post->post_type === 'project' && $cat->slug === 'business') {
$locate_template = locate_template( "custom-business-template.php" );
if ( ! empty( $locate_template ) ) {
$template = $locate_template;
}
}
return $template;
} );
Thank you to all contributors and the community.
I have a task to create a template which is specific to the category. So Lets say I have 10 categories but I want to create a specific template for lets say 3 of them. So if the category is a,b or c I will apply a certain template.
And then when I create a post and attach it to the specific category I need to show that specific template associated to the category.
Any headers?
The Advanced Custom Fields https://www.advancedcustomfields.com/ plugin should allow you to show different templates based off categories. It has some pretty fancy functionality, but can't remember if it can do exactly this.
There is a free version, so give it a try. Let me know how you go ;)
Delete everything in single.php
Insert the ‘switching’ code (see below)
Create 3(three) new templates with unique names. like : single-a,single-b,single-c.
On the server, the magical fairy dust in your modified single.php will automatically load the correct template when the page is requested
Please try below Code for the same.
if (in_category('21')) {include (TEMPLATEPATH . '/single-a.php');
}
else if (in_category('22')) {include (TEMPLATEPATH . '/single-b.php');
}
else if (in_category('23')) {include (TEMPLATEPATH . '/single-c.php');
}
else { include (TEMPLATEPATH . '/single-29.php');
}
There is single-a,single-b,single-c are 3 templates for different categories and your main code in it.
If you refer to Category_Templates
Wordpress will auto retrieve category files in the following format:
category-slug.php or category-ID.php
Let's say you have 3 categories, category a, category b, category c, to assign each of the template differently you can easily create category-a.php, category-b.php, category-c.php and place your desire template within the file and Wordpress will handle the rest.
Here you can use category_template
function wp_category_template( $template ) {
$cat = get_queried_object(); // get category object
if( 1 ) // check condition
$template = locate_template( 'template.php' ); // load template
return $template;
}
add_filter( 'category_template', 'wp_category_template' );
or as #shashi suggested you can use plugin custom-category-template
You have 3 options :
Option 1 : You can create 3 templates and name them according to the WordPress Template Hierarchy like this :
category-1.php
category-2.php
category-3.php
Option 2 : Use PHP code in your functions file to load 1 template for 3 different categories :
add_filter( 'template_include', 'custom_category_template', 99 );
function custom_category_template( $template ) {
if ( is_category(array( 1,2,3 ) ) ) {
$new_template = locate_template( array( 'custom.php' ) );
if ( '' != $new_template ) {
return $new_template;
}
}
return $template;
}
Use the in_category or is_category conditional tag depending on whether you want to load the template for posts in specific categories or only for the category archive page.
Option 3 : You can use the code in option 2 with the category_template filter :
add_filter( 'category_template', 'custom_category_template' );
function custom_category_template( $template ) {
if ( is_category(array( 1,2,3 ) ) ) {
$template = locate_template( 'custom.php' );
}
return $template;
}
Assumes your category i.d's are 1, 2 and 3. Swap out these to match your installations category i.d's
I've been pulling my hair out with this all day, please forgive the short description, I just need to validate my sanity!!
As the title says, I'm trying to create two or three different single-product layouts within woocommerce. The minimum is trying to achieve would be to have multiple single-product folders each with their own name and configurations.
No matter which way I try to override the single-product.php and make this file use logic to check for the product_cat and give out templates accordingly, I either the page not loading or what I write is skipped over and the default is loaded.
So far I've been through the following methods multiple times, trying to piece together what may be outdated code or otherwise causing all the fuss:
WooCommerce - How to create multiple single product template based on category?
Woocommerce single product - template by categories
Creating a different template file for certain Product Categories - Wordpress/Woocommerce?
I was more hoping someone may know something about this that I'm obviously missing as there are many articles out there on what to try and most claim success but I'm unable to do so.
[Update] using template_include code from #helgatheviking
No success just yet but here's where I'm up to;
File structure
team-shops is the category I'm trying to get
/mytheme/woocommerce/single-product.php - no changes
/mytheme/woocommerce/content-single-product.php
/mytheme/woocommerce/single-product-team-shops.php - changed line 37 to<?php wc_get_template_part( 'content', 'single-product-team-shops' ); ?>
/mytheme/woocommerce/content-single-product-team-shops.php - added additional id to #product-id (line 39)
/mytheme/woocommerce/single-product-team-shops/ folder with all single product files to change.
As I said above this isn't working but hopefully with what I've provided the problem may be more obvious.
Thanks again for any help :)
[Think I've got it]
Ok so I think I've something that works, at least for now it seems to, still have some further testing to do but any thoughts more than welcome, this is what I've got so far along with a single-product-team-shops folder in my theme
add_filter( 'woocommerce_locate_template', 'so_25789472_locate_template', 10, 3 );
function so_25789472_locate_template( $template, $template_name, $template_path ){
$term_id = 2854;
$taxonomy_name = 'product_cat';
$term_children = get_term_children( $term_id, $taxonomy_name );
foreach ( $term_children as $child ) {
// on single posts with mock category and only for single-product/something.php templates
if( is_product() && has_term( $child, 'product_cat' ) && strpos( $template_name, 'single-product/') !== false ){
// replace single-product with single-product-mock in template name
$mock_template_name = str_replace("single-product/", "single-product-team-shops/", $template_name );
// look for templates in the single-product-mock/ folder
$mock_template = locate_template(
array(
trailingslashit( $template_path ) . $mock_template_name,
$mock_template_name
)
);
// if found, replace template with that in the single-product-mock/ folder
if ( $mock_template ) {
$template = $mock_template;
}
}}
return $template;
}
Use a single-product-custom.php template for any product in the "custom" category:
add_filter( 'template_include', 'so_43621049_template_include' );
function so_43621049_template_include( $template ) {
if ( is_singular('product') && (has_term( 'custom', 'product_cat')) ) {
$template = get_stylesheet_directory() . '/woocommerce/single-product-custom.php';
}
return $template;
}
NB: If you use the same action hooks in your single-product-custom.php template you will get the same look as the default single-product.php. You could 'rename' all the hooks and then could add existing functions (such as those for add to cart buttons, etc) to the new hooks in order to achieve a totally custom look.
I realize this is a wordpress plugin but I'm hoping someone can help me out with the overall php structure of my code.
<?php
add_filter( 'wprss_auto_create_terms', 'filter_categories', 10, 3 );
/**
* Filter categories to only allow "Plugin Reviews".
* Change "Plugin Reviews" into "Plugins" if found.
*/
function filter_categories( $categories, $taxonomy, $source_id ) {
// Check for feed source 3878
if ( $source_id == '3878' ) {
// Loop all categories
foreach( $categories as $i => $category ) {
// If "Plugin Reviews" is found, change it to "Plugins"
if ( $category['name'] == 'Plugin Reviews' )
$categories[$i]['name'] = 'Plugins';
// If not "Plugin Reviews", remove it
else unset( $categories[$i] );
}
}
// Return the categories
return $categories;
}
I'm able to change any feed category names with the name "Plugin Reviews" to "Plugins" because all current posts in the feed currently contain the category "Plugin Reviews". If I try a category that all the posts do not have they are all call Uncategorized regardless if some have the category name or not. What am I missing and any ideas how this could be fixed?
I have created a new taxonomy for products on woocommerce.
Now, when I go to the taxonomy view and it shows 404.
how do i make it show the products just like in any other regular category on the site?
tnx ahead :)
I tried this code and it works for me.
I created a taxonomy name 'team' and here is my code.
add_filter('template_include', 'team_set_template');
function team_set_template( $template ){
if(is_tax('team')) :
$taxonomy = 'team';
$term = get_query_var($taxonomy);
$prod_term = get_terms($taxonomy, 'slug='.$term.'');
$term_slug = $prod_term[0]->slug;
$t_id = $prod_term[0]->term_id;
$term_meta = get_option( "taxonomy_$t_id" );
$term_meta['team_access_pin'];
wc_get_template( 'archive-product.php' );
else :
wc_get_template( 'archive-product.php' );
endif;
}
Note: I used this code in functions.php file. If anyone wants to use this code. Please replace the name 'team' with your custom taxonomy name.
Thanks,
Satya
This problem occurs when WordPress does not find any template for the custom taxonomy.
1.The solution is to create a WooCommerce template named as "taxonomy-tax_name.php" (ideally in a child theme).
2.For this you could copy the contents of an archive template like archive-product.php and modify it by doing a WP_Query() with arguments for post_type=>"product" and add a "tax_query" with the newly made custom taxonomy slug.
3.This would get you the required products and now can be displayed as required.