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
Related
I'm trying to make my first plugin and I got stuck. Here is the idea.
When my plugin is activated it will create one post type and two taxonomies for that post type (in my case the post type name is 'Ads').
Also, I created two template pages, one to display the listing of all ads post type articles and the other for a single page for the same post type.
Now my problem is how to tell WordPress to look for the templates from a plugin folder rather than the theme folder when the plugin is active.?
Is it something I can do in the plugin file or I have to create another file for this purpose?
This should do what you are looking for:
First, this hook to tell WordPress which is your single CPT template in your plugin
From this answer you get the single_template hook and how to load it.
Define a constant to replace "plugin_dir_path( FILE )" if you use it elsewhere in your plugin, like this:
define('YOUR_PLUGIN_DIR_PATH', trailingslashit(plugin_dir_path( __FILE__ )) );
https://wordpress.stackexchange.com/questions/17385/custom-post-type-templates-from-plugin-folder
function load_single_ad_template( $template ) {
global $post;
if ( 'ads' === $post->post_type && locate_template( ['single-ads.php'] ) !== $template ) {
/*
* This is an 'ads' post
* AND a 'single ad template' is not found on
* theme or child theme directories, so load it
* from our plugin directory from inside a /templates folder.
*/
return YOUR_PLUGIN_DIR_PATH . 'templates/single-ads.php';
}
return $template;
}
add_filter( 'single_template', 'load_single_ad_template', 10, 1 );
And then for the ads archive template, the 'archive_template' hook, like this:
function load_archive_ads_template( $archive_template ) {
global $post;
if ( is_post_type_archive ( 'ads' ) ) {
$archive_template = YOUR_PLUGIN_DIR_PATH . 'templates/archive-ads.php';
}
return $archive_template;
}
add_filter( 'archive_template', 'load_archive_ads_template', 10, 1 ) ;
Official documentation:
https://developer.wordpress.org/reference/hooks/type_template/
https://codex.wordpress.org/Plugin_API/Filter_Reference/archive_template
This is untested, but should work, however, let me know.
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'm currently using archive.php as a generic listing for categories, tags, authors, etc. My theme have 4 custom post type and i want each post type have separate archive template.
How to add separate archive template for each custom post type?
I have a code but not working is there i m missing some thing?
add_filter( 'template_include', 'wpsites_cpt_archive_page_template', 99 );
function wpsites_cpt_archive_page_template( $template ) {
if ( is_post_type_archive(array( first-post-type ) ) ) {
$new_template = locate_template( array( 'your-cpt-template.php' ) );
if ( '' != $new_template ) {
return $new_template ;
}
}
return $template;
}
checkout wordpress template files docs,
you don't need to use conditionals for separating post type template,
you can simply create archive-{your-post-type}.php file to have different archive template for each post type.
for taxonomy;
taxonomy-{taxonomy}-{term}.php
taxonomy-{taxonomy}.php
tag-{slug}.php
tag-{id}.php
category-{slug}.php
category-{ID}.php
Additionally, if your theme supports standard loop hooks like Genesis Framework, You can use conditional to build custom loop depending on Post type.
lets say you have custom post types (CTP).. cpt1, cpt2, cpt3 ...
then you just need to create archive templates files respectively like that
archive-cpt1.php , archive-cpt2.php , archive-cpt3.php .
How can i disable the comments form for a specific category in Wordpress.
And i mean with that every post the user will publish it in this category will not has a comment form content.
Rather than doing this in functions.php, you could create two template files. The first template file is your standard theme template, the second would be identical, except it wouldn't contain the comment code section.
Simply name the template file that doesn't have the comment code block category_{id}.php and upload to your theme folder. The ID is the ID of the category you want to disable comments on.
More information on category specific templates here https://developer.wordpress.org/themes/basics/template-hierarchy/#category
More information about the comment template here https://codex.wordpress.org/Function_Reference/comments_template
If you still want to do this via functions.php, see this blog post http://spicemailer.com/wordpress/disable-hide-comments-posts-specific-categories/ which uses the following code snippet
add_action( 'the_post', 'st_check_for_closed' );
function st_check_for_closed()
{
global $post;
$my_post_cat = wp_get_post_categories($post->ID);
$disabled_cat = array( "1", "3"); // this is he array of disabled categories. Feel free to edit this line as per your needs.
$my_result = array_intersect($my_post_cat,$disabled_cat);
if (empty ( $my_result ) )
{
return;
}
else {
add_filter( 'comments_open', 'st_close_comments_on_category', 10, 2 );
add_action('wp_enqueue_scripts', 'st_deregister_reply_js');
}
}
function st_deregister_reply_js()
{
wp_deregister_script( 'comment-reply' );
}
function st_close_comments_on_category ($open, $post_id)
{
$open = false;
}
I would like to setup different META syntax on my Wordpress website, depending on the template I use (5 templates for pages and 8 templates for articles.
I use SEO by yoast that only gives the possibility to add ONE META syntax for all pages oand one for ALL articles.
I tried add use this on my function.php for one template (example)
$pref_nom = get_post_meta($post->ID, $pref_nom, true);
function prefecture_filter_wp_title( $title ) {
if ( is_page_template( 'pages-prefectures.php' ) ) {
return $pref_nom;
}
return $title;
}
add_filter( 'wp_title', 'prefecture_filter_wp_title' );
But I doesn't work (notice that $pref_nom is not declared)
What's wrong ?
Thanks in advance for your help.