I created a plugin and added a custom post type to the plugin. I then created a template using the hierarchy single-{custom-post-type}.php. So my cpt slug is wppro-events and the single post template is called single-wppro-events.php. I also added to the top of the template file the comment section below but my post template is not being used for the cpt. Any idea why it is not being used. Also page attributes on the edit page isn't showing the option to change the template either. Astra is the theme I am using. Any idea why this isn't working?
/*
Template Name: Layout One
Template Post Type: wppro-events
*/
I also tried the below code but still didn't work:
/* Filter the single_template with our custom function*/
add_filter('single_template', 'my_custom_template');
function my_custom_template($single) {
global $post;
/* Checks for single template by post type */
if ( $post->post_type == 'Event Items' ) {
if ( file_exists( PLUGIN_PATH . '/single-wppro-events.php' ) ) {
return PLUGIN_PATH . '/single-wppro-events.php';
}
}
return $single;
}
Related
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)
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
}
I am working on a plugin and that creates a custom post type and custom taxonomy for that post type. I simple created single page template that worked fine and now trying to use taxonomy template and that's never working for me. While I pasted that template in my theme folder and it works like a charm, can any one tell me how to use taxonomy template in the plugin ?
Here is my post type name: portfolio
single page template is: single-portfolio
taxonomy name is: portfolio_category
Taxonomy template is: taxonomy-portfolio_category
All those files are living in plugins main folder. Can any one point out why taxonomy works in theme folder but not in plugin folder ???.
I believe your question is answered here:
https://wordpress.stackexchange.com/questions/51022/default-taxonomy-template-in-plugin-override-in-theme, you only need to do something in reverse...
To use a taxonomy template from your plugin directory if one exists, otherwise fall back to the template in theme directory you would do something like this:
function override_tax_template($template){
// is your portfolio_category specific custom taxonomy being shown?
$taxonomy_array = array('portfolio_category');
foreach ($taxonomy_array as $taxonomy_single) {
if ( is_tax($taxonomy_single) ) {
if (file_exists(trailingslashit(BASE_PLUGIN_DIR) . 'taxonomy-'.$taxonomy_single.'.php')) {
$template = trailingslashit(BASE_PLUGIN_DIR) . 'taxonomy-'.$taxonomy_single.'.php';
} else {
$template = trailingslashit(get_stylesheet_directory()) . 'taxonomy-'.$taxonomy_single.'.php';
}
break;
}
}
return $template;
}
add_filter('template_include','override_tax_template');
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();
}
is there a way (plugin) to insert html/css/images ect.. automatically into every wordpress post? most of my posts are going to be very similar so is there a plugin that will automatically insert the pre written html/css/ ect in every post as opposed to me entering it manually every time.
Thank you in advance ;-)
You can write your own simple function for this, see my example below:
add_filter( 'default_content', 'my_editor_content' );
function my_editor_content( $content ) {
global $post_type;
if( $post_type == 'post') { /* Or your custom post type, pages etc. */
$content = 'Your custom HTML/CSS content here';
}
return $content;
}
Place this in functions.php and it will be the default content of every new post/page/custom post type you create.
For a list of available post types, please refer to the Codex
You could use a plugin such as Ad injection, it will allow you to do what you need without having to alter / amend / ad any code to the templates or files