Ask your WordPress questions! Pay money and get answers fast! (more info)

Custom Post Type - Single Template based on Taxonomy

Hello,

i tray to explain, in my website i created a CUSTOM POST TYPE "wpcbd", and a CUSTOM TAXONOMY "bd_categories". Wordpress by default allow me to create a file <strong>single-wpcbd.php</strong> to give to my custom post type his personalized template.

I need to be able to have something like this <strong>single-wpcbd-restaurants.php</strong> i mean a custom template for all custom post type that are under custom taxonomy "restaurants" like is already possible withthe default post type and the default categories.

I'm becoming creazy because after 2 days of serach there are no doc or tutorial that explain this.

Thanks

Answers (22)

2012-07-05

Luis Abarca answers:

Try with

archive-bd_categories.php

----

Sorry, i misundertood the question above.

You can create 2 files in a folder named parts, one for normal files of any category and another one for restaurants.

So you can use single-wpcbd.php and change the content when you are showing a post from that category

<?php
// single-wpcbd.php

if ( has_term( 'restaurants', 'bd_categories', $post ) ) {
// you may also use get_template_part('loop', 'restaurants');

include 'parts/single-wpcbd-restaurants.php';
} else {
include 'parts/single-wpcbd.php';
}


2012-07-05

Arnav Joy answers:

create a new file named as

taxonomy-bd_categories.php

and put following code there to list all the posts


<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>

<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

<h2 class="entry-title"><?php the_title(); ?></h2>


<div class="entry-content">
<?php the_content(); ?>

</div><!-- .entry-content -->
</div><!-- #post-## -->



<?php endwhile; // end of the loop. ?>

2012-07-05

Scott Hack answers:

I think you want something like taxonomy-bd_categories.php or bd_categories_taxonomy.php I can't remember which way it is.

2012-07-05

Jatin Soni answers:

Try in this way to create page template

<?php
/*
TEMPLATE NAME: Your templatename
*/

//for a given post type, return all
$post_type = 'wpcbd';
$tax = 'bd_categories';
$tax_terms = get_terms($tax);
if ($tax_terms) {
foreach ($tax_terms as $tax_term) {
$args=array(
'post_type' => $post_type,
"$tax" => $tax_term->slug,
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);

$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo 'List of '.$post_type . ' where the taxonomy '. $tax . ' is '. $tax_term->name;
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
<?php
endwhile;
}
wp_reset_query();
}
}
?>


You can also try with this too

<?php
/*
TEMPLATE NAME: Your templatename
*/

query_posts( array( 'wpcbd' => 'bd_categories' ) ); ?>
<?php if( is_tax() ) {
global $wp_query;
$term = $wp_query->get_queried_object();
$title = $term->name;
} ?>

<ul>
<span class="tax-title"><?php echo($title); ?></span>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

<li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a><?php the_excerpt() ?></li>

<?php endwhile; else: ?>
<?php endif; ?>
</ul>

2012-07-05

Navjot Singh answers:

Try this:

add_filter( 'single_template', 'add_albumtrack_taxslug_template', 10, 1 );
function add_albumtrack_taxslug_template( $orig_template_path )
{
$object = get_queried_object();

if ( ! ( in_array( $object->post_type, array( 'wpcbd' )) && taxonomy_exists( 'bd_categories' ) && $album_tax = wp_get_object_terms( $object->ID, 'bd_categories' ) ))
return $orig_template_path;
$template = locate_template( $template );
return ( !empty( $template ) ? $template : $orig_template_path );
}


in your theme's function.php

And then you can create template files like single-wpcbd-restaurants.php where restaurant is a taxonomy of type bd_categories and wpcbd is your custom post type.

2012-07-05

Hai Bui answers:

If you want to use a template for all taxonomy terms (under bd_categories), use taxonomy-bd_categories.php
If you want to use a template for a particular taxonomy term, for example "restaurants", use taxonomy-bd_categories-restaurants.php

For more information about WP template hierarchy, you can check this [[LINK href="http://codex.wordpress.org/Template_Hierarchy"]]http://codex.wordpress.org/Template_Hierarchy[[/LINK]]

2012-07-05

Albert Shala answers:

-> taxonomy-taxonomy_term.php

2012-07-05

enoguejona answers:

2012-07-05

hceforchtwalla answers:


hceforchtwalla comments:


hceforchtwalla comments:


hceforchtwalla comments:

2012-07-05

yehallystevi answers:


yehallystevi comments:


yehallystevi comments:


yehallystevi comments:


yehallystevi comments:


yehallystevi comments:


yehallystevi comments:


yehallystevi comments:


yehallystevi comments:


yehallystevi comments:

2012-07-05

ninenbryc answers:


ninenbryc comments:


ninenbryc comments:


ninenbryc comments:


ninenbryc comments:


ninenbryc comments:

2012-07-05

drewdelm answers:


drewdelm comments:


drewdelm comments:


drewdelm comments:

2012-07-05

luballneald answers:


luballneald comments:


luballneald comments:


luballneald comments:


luballneald comments:


luballneald comments:


luballneald comments:

2012-07-05

swigstock answers:


swigstock comments:


swigstock comments:


swigstock comments:


swigstock comments:


swigstock comments:


swigstock comments:

2012-07-05

aburnamanperry answers:


aburnamanperry comments:


aburnamanperry comments:

2012-07-05

lretaglemarge answers:


lretaglemarge comments:


lretaglemarge comments:


lretaglemarge comments:


lretaglemarge comments:


lretaglemarge comments:


lretaglemarge comments:


lretaglemarge comments:


lretaglemarge comments:


lretaglemarge comments:

2012-07-05

gspringmaange answers:


gspringmaange comments:


gspringmaange comments:


gspringmaange comments:


gspringmaange comments:


gspringmaange comments:


gspringmaange comments:

2012-07-05

lbridalbridal answers:


lbridalbridal comments:


lbridalbridal comments:


lbridalbridal comments:


lbridalbridal comments:


lbridalbridal comments:


lbridalbridal comments:


lbridalbridal comments:

2012-07-05

eelardeselli answers:


eelardeselli comments:


eelardeselli comments:


eelardeselli comments:


eelardeselli comments:


eelardeselli comments:


eelardeselli comments:

2012-07-05

wbuywatch answers:

makino / Ma Kai slavery men of genuine winter outdoor warm down - $318.00 :

2012-07-05

cmccancemarty answers:

> Fell in love with dance performances shirt presided over a per - $20.00 :

2012-07-05

apellhamdeme answers:

The Liebo original jewelry new female embroidery stitching belt - $35.00 :