Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

How to get individual single.php file for custom taxonomy terms WordPress

  • SOLVED

1. I've looked everywhere on the internet and many people ask this question and there are many different answers. I've tried everything. Nothing works, maybe it conflicts with my theme or whatever. Who knows.

Anyway. My posts (<strong>not</strong> custom posts) have a custom taxonomy called "fields". I have two terms under fields called "important" and "other". I would like to change the single.php file for every post under "other" and every post under "important", while keeping all other posts as the default single.php.

How can this be done?

2. How can I display the custom taxonomy in post meta, much like "Category", "Author" and "Comments" in the attached image.

My website is www.neliti.com and the theme is mesocolumn. I use a child theme.

Thankyou for anyone who can help.

Answers (6)

2015-06-25

timDesain Nanang answers:

1. individual single.php
you can use (put the following code into theme's functions.php):

add_filter( 'template_include', 'wpq_template_include' );
function wpq_template_include( $template ) {
global $post;

if(is_single()){
if(has_term('important', 'fields')){
$template = locate_template(array('single-important.php', 'single.php'));
}
elseif(has_term('other', 'fields'))
$template = locate_template(array('single-other.php', 'single.php'));
}

return $template;
}

ref: https://codex.wordpress.org/Plugin_API/Filter_Reference/template_include

2.
Do you mean like this: [[LINK href="http://wpquestions.com/question/showLoggedIn/id/9803"]]http://wpquestions.com/question/showLoggedIn/id/9803[[/LINK]]


neliti comments:

Ok halfway there.

I ended up using sections of your code in my single.php. Looks something like this (also mixed in with my custom single.php for categories):

<?php
if (in_category('104')) {
include ('single-104.php');
return;
}
if(has_term('other', 'field')) {
include ('single-field.php');
return;
}
else {
include ('single-research.php');
return;
}
?>

OK, for part 2:
see the image I've attached. Basically, I'd like "field" to appear in the post-meta.php, like "categories", "author" and "comments".