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

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$20
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 single-wpcbd.php to give to my custom post type his personalized template.

I need to be able to have something like this single-wpcbd-restaurants.php 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

ellenica | 07/05/12 at 1:35am Edit
Tutorial: How to assign prize money


(22) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    07/05/12
    1:50am
    Luis Abarca says:

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


    Previous versions of this answer: 07/05/12 at 1:50am

  • avatar
    Last edited:
    07/05/12
    1:59am
    Arnav Joy says:

    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. ?>

    Previous versions of this answer: 07/05/12 at 1:59am

  • avatar
    Last edited:
    07/05/12
    1:48am
    Scott Hack says:

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

  • avatar
    Last edited:
    07/05/12
    1:57am
    Jatin Soni says:

    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>

    Previous versions of this answer: 07/05/12 at 1:57am

  • avatar
    Last edited:
    07/05/12
    1:52am
    Navjot Singh says:

    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.

    Previous versions of this answer: 07/05/12 at 1:52am

  • avatar
    Last edited:
    07/05/12
    1:55am
    Hai Bui says:

    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 http://codex.wordpress.org/Template_Hierarchy

  • avatar
    Last edited:
    07/05/12
    2:07am
    Albert Shala says:

    -> taxonomy-taxonomy_term.php

  • avatar
    Last edited:
    07/05/12
    2:33am

    This answer was downvoted by the top experts.

  • avatar
    Last edited:
    07/05/12
    6:25am

    This answer was downvoted by the top experts.

  • avatar
    Last edited:
    07/05/12
    6:48am

    This answer was downvoted by the top experts.

  • avatar
    Last edited:
    07/05/12
    7:03am
    ninenbryc says:

  • avatar
    Last edited:
    07/05/12
    8:42am
    drewdelm says:

  • avatar
    Last edited:
    07/05/12
    8:49am
    luballneald says:

  • avatar
    Last edited:
    07/05/12
    9:05am
    swigstock says:

  • avatar
    Last edited:
    07/05/12
    1:26pm
    aburnamanperry says:

  • avatar
    Last edited:
    07/05/12
    1:36pm
    lretaglemarge says:

  • avatar
    Last edited:
    07/05/12
    5:35pm
    gspringmaange says:

  • avatar
    Last edited:
    07/05/12
    11:35pm
    lbridalbridal says:

  • avatar
    Last edited:
    07/06/12
    1:28am
    eelardeselli says:

  • avatar
    Last edited:
    07/07/12
    3:18pm
    wbuywatch says:

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

  • avatar
    Last edited:
    07/07/12
    8:45pm
    cmccancemarty says:

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

  • avatar
    Last edited:
    07/07/12
    10:49pm
    apellhamdeme says:

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

This question has expired.





Current status of this question: Community pot



Please log in to add additional discourse to this page.





Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.