Changing Wordpress template for specific product category page - wordpress

I'm trying to change the template of a specific product category page, but I can't for the life of me get it to work. I've tried taxonomy-product_cat-the-category but to no avail. I'm using wpml with the categories in 3 different languages if that makes a difference. I've tried with the slugs of each of the 3 langauges but still doesn't work. Don't understand what could be causing this.

First copy taxonomy-product_cat.php & archive-product.php from WooCommerce Plugin directory - wordpress\wp-content\plugins\woocommerce\templates to your theme - wordpress\wp-content\themes\your-theme\woocommerce.
Then make copy of archive-product.php files in your theme's woocommerce directory like archive-product-2.php, archive-product-3.php etc. Later you are going to modify these files as per your category.
Then open taxonomy-product_cat.php file. The code will look like below one.
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
wc_get_template( 'archive-product.php' );
We need to modify the template call wc_get_template() in this code with our conditions.
First get the current category slug & then we can compare the slug. As per the slug, we will call different archive product files.
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
// Get current category slug
global $wp_query;
$cat_slug = $wp_query->query_vars['product_cat'];
// Call template conditionally
if($cat_slug == 'accessories') {
wc_get_template( 'archive-product-2.php' );
} else {
wc_get_template( 'archive-product.php' );
}
Updates
In my experience, the best approach is avoid WooCommerce template overriding by copying in to theme files. The WooCommerce have regular updates on the templates and it is better to update templates on each time to avoid future issues. So if possible, maximum use filter hooks other than template overriding.

You can can't change the template of WP for a specific category but
For only overriding design: You can do css by using body.term-yourcategoryname at starting of each class, ID or tag name like this:
body.term-car h1 {
color: red;
font-size: 22px;
}
For enhancing Layout: or add some new feature you can use wooommerce default functions like: $term->name; in archive-product.php in your child theme.
example:
if($term->name == "car") {
// your code will goes here
}

Related

Custom template for one specific post in a custom post type in wordpress

I have a little web app, that I want to run within a page in a custom post type on my WordPress site.
So I thought to just create a new template to put the specific code for this app there and assign this template to the custom post types page.
Sadly Wordpress is not showing me the template in my cpt.
The template file is called fretfind.php and the first lines contain:
/*
Template Name: Fretfind
Template Post Type: guitar
*/
'guitar' is the cpt slug.
Has anyone an idea why it doesn't show me the selecting option for this template. Are there any extra steps for Custom Post Type Templates?
Or is there generally another good practice to have just on one specific wordpress page custom php code? Something like a template for post ID?
If this was a page, you could have used a template file called page-{slug}.php, where {slug} is the slug of the specific page (see here: https://developer.wordpress.org/themes/template-files-section/page-template-files/#page-templates-within-the-template-hierarchy).
But since we're talking about CPT there is unfortunately only single-{$posttype}.php and no single-{$posttype}-{$slug}.php (see here: https://developer.wordpress.org/themes/template-files-section/custom-post-type-template-files/#custom-post-type-templates)
However, you can add support for such template files for CPT by adding this code to your theme's functions.php:
add_filter( 'single_template', function( $template ) {
global $post;
if ( $post->post_type === 'guitar' ) {
$locate_template = locate_template( "single-guitar-{$post->post_name}.php" );
if ( ! empty( $locate_template ) ) {
$template = $locate_template;
}
}
return $template;
} );
And then use the template file single-guitar-myspesificguitarslug.php (where myspesificguitarslug is the slug of the CPT page where you want this template to apply)

how to remove excerpt on category archive page in genesis framework?

I am trying to remove the excerpt ( content ) from the category archive page so only the title shown on the page, not the content.
I am currently trying the code below with no luck?
does anybody have any suggestions?
add_action ( 'genesis_before_entry' , 'designody_remove_entry_content_archives' );
function designody_remove_entry_content_archives() {
if (in_category('5')) {
remove_action( 'genesis_entry_content', 'genesis_do_post_content' );
}
}
my second solution... also not working
function replace_content( $output) {
if (is_category('5')) {
return '' ;
}
}
add_filter( 'get_the_excerpt', 'replace_content' );
I don't have a fully satisfactory Genesis-specific solution, and I'd be curious to know what StudioPress itself says, but I do have WordPress solutions.
First, to use the Genesis hook and a condition, you need to wrap it in a function:
To quote the Codex: "Warning: You can only use conditional query tags after the posts_selection action hook in WordPress (the wp action hook is the first one through which you can use these conditionals). For themes, this means the conditional tag will never work properly if you are using it in the body of functions.php, i.e. outside of a function."
https://codex.wordpress.org/Conditional_Tags
The following works to remove the excerpt on the specified category archive page:
add_action( 'loop_start', 'remove_content_on_category_pages' ) ;
function remove_content_on_category_pages() {
if ( is_category( '5' ) ) {
remove_action( 'genesis_entry_content', 'genesis_do_post_content' );
}
}
However, the in_category() conditional - which should help with entry content when it appears in other archives (categories, blog, etc.) - doesn't seem to work.
In order to get the desired results as I understand them - no post content, in this implementation "the excerpt", for this category on any type of archive page, but preserving it for individual posts - you can revert to the trusty the_content filter, but note that the the_content can be a tricky and powerful tool subject to unexpected secondary effects. That said, on a vanilla Genesis installation, the following tests out:
add_filter( 'the_content', 'test_gen_content' ) ;
function test_gen_content( $content ) {
/**
apply to all secondary queries don't apply if single page or post, or in
admin, otherwise obliterate - can use is_single() instead of is_singular()
if only posts are at issue
**/
if ( ! is_singular() && ! is_admin() && in_category( '157' ) ) {
return '' ;
}
return $content ;
}
I must say, however, that, given the peculiarities and complications of the Genesis framework, and the peculiarity of the request, out there in the wild I'd be very tempted to use a CSS kludge, shaped to requirements - at least failing direct instruction or intervention from a Genesis maven or StudioPress itself. Whether you needed to refine the CSS further to suit your particular installation, I can't say, but it might be a lot easier, actually, then sorting out the PHP even with the correct hooks etc.
//substitute appropriate category slug for "development-forums"
.home .category-development-forums .entry-content,
.blog .category-development-forums .entry-content,
.archive .category-development-forums .entry-content
{
display: none;
}

How to remove wordpress date archive pages?

I have developed my own wordpress theme, and learning all kinds of programming stuff, but my priority is the content, not the programming knownledge. I need to know how to remove archive date listing from wordpress?
Question 1: The google search results displayed like this: virmodrosti.com/2017/05/
I don't want any kind of date archive option, how do you disable that?
I also don't use any kind of plugin, and always like to do it on my own.
Question 2: I don't know why older entries doesn't work anymore
virmodrosti.com/zdravje/ this page works fine
virmodrosti.com/zdravje/page/2/ it redirects to 404 error page
I only choose option in wordpress to hide that annoying /category/ with dash . inside editor at permanlinks, Category base. Maybe somehow these stuff is kinda fighting with each other and doesn't work properly.
Thank you.
This is code from Digital Nomad theme I maintain:
function digitalnomad_remove_date_archives() {
//if we are on date archive page
if ( is_date() ) {
// theme sets alternatine archive page with table like list of all posts
$archive_page = get_option( 'digitalnomad_archive_page' );
if ( $archive_page ) {
// redirs to alternatine archive page if configured (good for SEO)
wp_redirect( esc_url( get_page_link( $archive_page ) ) );
die();
} else {
// otherwise error 404 is displayed
global $wp_query;
$wp_query->set_404();
}
}
}
add_action( 'template_redirect', 'digitalnomad_remove_date_archives' );
Use the smart Archive Page Remover wordpress plugin
or visit your theme's functions.php file
then insert this code
/* Register template redirect action callback */
add_action('template_redirect','makes_remove_wp_archives');
/* Remove archive*/
function makes_remove_wp_archives(){
// if we are on category or tag or date or author archive
if(is_category()|| is_tag()||is_author()){
global $wp_query;
$wp_query->set_404();
}
}

Replacing only parts of an archive and single page template of WordPress

I am having a bit of trouble here understanding how to do the following. I have searched for weeks now but cannot seem to find what I am looking for.
I have a custom post type 'product' and want to change which template gets loaded for the single product page as well as the archive for the products. I am using the following code to load include and load templates.
add_filter('template_include', function() {
if (is_post_type_archive('product')) {
$templatefilename = 'archive-product.php';
$template = WPVS_PATH . 'templates/' . $templatefilename;
return $template;
}
if ('product' == get_post_type() ){
$templatefilename = 'single-product.php';
$template = WPVS_PATH . 'templates/' . $templatefilename;
return $template;
}
});
The problem I am having is that it replaces the current theme's template instead of just the inner part of the content and archive areas.
Here is what I want to achieve:
Create a custom post type 'product' in a plugin - DONE (Was kinda easy!)
When opening a single product only change the content part. - I can do this with the_content filter hook. Simple enough. Any other suggestions is welcome.
When I go to the archive view for the 'product' custom post type I don't want to have it load the theme's default archive (list) view but instead a grid view from my plugin which I cannot seem to get right. I only want to change the inner part of the template, not the whole page.
I have created this plugin a few weeks ago using only shortcodes which works good but want to see if I can do it without the use of shortcodes by means of creating the custom post type and changing the inner template parts of the current active theme.
Can anybody steer me into the right direction here?
If I create a theme I can do what I am looking for but I want to create this into a plugin instead without adding or making changes to the active theme. The plugin should handle what is needed.
The same issue is discussed here but what I want is to develop something that is theme independent. No changes should be made in theme files and no theme files should be copied to the plugin.
WP - Use file in plugin directory as custom Page Template?
Recently I also had the same problem. Here's how I worked it out.
template_include filter accepts a parameter which is the selected template that you want to override (this what you are missing in your code).
I don't know but sometimes the filter hook need higher priority to work like 9999. But first check if it work with default priority, if don't change it.
I assume your both archive and single product template both have include get_header() and get_footer() which can be used for default selected theme (Or if the theme has different setup, setup accordingly).
This is simplified code:
add_filter('template_include', function($default_template) {
if (is_post_type_archive('product')) {
$templatefilename = 'archive-product.php';
$template = WPVS_PATH . 'templates/' . $templatefilename;
$default_template = $template;
} else if ('product' == get_post_type() ) {
$templatefilename = 'single-product.php';
$template = WPVS_PATH . 'templates/' . $templatefilename;
$default_template = $template;
}
// Load new template also fallback if both condition fails load default
return $default_template;
}, 9999); // set priority, only if not worked with default one
The best option in this case is to provide a shortcode to the user. So they can place it on any page that they want (or that you auto generate). That way you will place your content inside their theme.
Something like this:
add_shortcode( 'slotsl-game', 'embed_game' );
/**
* Print the game
* #return false|string
*/
function embed_game(){
ob_start();
$game = get_post();
include_once SLOTSL_PLUGIN_DIR . 'templates/slotsl-single-game.php';
return ob_get_clean();
}

WooCommerce search result template

I am working on a WooCommerce WordPress site. I have added WooCommerce search feature for product. But both main shop page and search result page have same template archive-product.php. But I want separate design for both pages. How can I do that?
As #arun said in the comments copy the archive-product.php and paste in the to the woocommerce folder inside your theme (if you don't have this folder, create one)
Open the file and split the content inside that file using a php if statement
if ( is_search() ) {
//put your search results markup here (you can copy some code from archive-product.php file and also from content-product.php to create a standard markup
} else {
// here goes the content that is already in that file (archive-product.php)
}
Just make sure this line of code always stays at the top of the file:
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
Based on #Capital Themes answer - you can inverse the function and add code for the non search form categories:
<?php if (! is_search() ) {
//Added code for non search form
} else {
// if want something only for search
} ?>
you must use file product-searchform.php in woocommerce/templates directore.
and customize search page by loop files in woocomerce/templates/global directore
And then with the is_search() method you can find out if you are in searching page or not.

Resources