最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Post template not applying on theme (potentially rendering as Page template)

programmeradmin1浏览0评论

I have a Post template, when I select it in the Post options, it saves, but doesn't apply the template on the frontend. Any ideas why this is happening? The posts in question are in the default posts area.

Here is the start of my template file, which is interestingly titled as Casino Live Dealer Template Page Template in the theme editor. This insinuates to me that Wordpress is reading this as a page template.

<?php
/*
Template Name: Casino Live Dealer Template
Template Post Type: post
*/

I have a Post template, when I select it in the Post options, it saves, but doesn't apply the template on the frontend. Any ideas why this is happening? The posts in question are in the default posts area.

Here is the start of my template file, which is interestingly titled as Casino Live Dealer Template Page Template in the theme editor. This insinuates to me that Wordpress is reading this as a page template.

<?php
/*
Template Name: Casino Live Dealer Template
Template Post Type: post
*/
Share Improve this question asked Sep 9, 2020 at 10:21 Matt MaclennanMatt Maclennan 1111 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

Fixed it! I have now created a single folder in my theme's directory and put category specific templates in there. To enable this, I added this to the theme's function file

/*
* Define a constant path to our single template folder
*/
define(SINGLE_PATH, TEMPLATEPATH . '/single');
 
/**
* Filter the single_template with our custom function
*/
add_filter('single_template', 'my_single_template');
 
/**
* Single template function which will choose our template
*/
function my_single_template($single) {
global $wp_query, $post;
 
/**
* Checks for single template by category
* Check by category slug and ID
*/
foreach((array)get_the_category() as $cat) :
 
if(file_exists(SINGLE_PATH . '/single-cat-' . $cat->slug . '.php'))
return SINGLE_PATH . '/single-cat-' . $cat->slug . '.php';
 
elseif(file_exists(SINGLE_PATH . '/single-cat-' . $cat->term_id . '.php'))
return SINGLE_PATH . '/single-cat-' . $cat->term_id . '.php';
 
endforeach;
}

This was thanks to this article!

发布评论

评论列表(0)

  1. 暂无评论