Custom template for one specific post in a custom post type in wordpress - 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)

Related

CPT template is not being used for post

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;
}

Can I make a page appear as a blog post in wordpress?

I created this page on my site: http://christmaspast.media/christmas-podcast-guide/ Each of the items on the page is a post that I created with a custom post type.
My question is whether I can treat this entire page as a blog post, and have it appear on my blog homepage along with all the other posts in my blog feed.
If not, is there a way to accomplish the same thing with a post? (AFAIK, it's not possible to embed multiple custom posts into another post, which is why I created a page instead.)
You need to use a filter pre_get_posts to modify the main query. Check Docs.
Replace custom_post_type string with yours.
function pref_add_custom_post_type_to_loop( $query ) {
// don't run in admin area
if ( ! is_admin() ) {
// add custom post types
if ( $query->is_feed() || ($query->is_main_query() && $query->is_front_page()) ) {
$query->set('post_type', array('post', 'custom_post_type'));
}
}
}
add_action('pre_get_posts', 'pref_add_custom_post_type_to_loop');

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();
}
}

Unable to use custom post type taxonomy template in the plugin

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');

is there a way (plugin) to insert html/css/images ect automatically into every wordpress post?

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

Resources