2

I have created a Custom Post type called "Ad" I then have a taxonomy under that called "type" I have added some terms in the Type category called "Radio", "TV", "Print". I would like WordPress to use a different single.php template for each term. I would like to have a Radio template that is different than the Print template. I have tried taxonomy-ad-type-radio.php I have tried single-ad-type-radio.php and every other form of those. Can someone tell me what's the best way to accomplish this?

I have added filters to my functions.php file to no avail also. I know I am just missing something very easy. Thanks so much for your help.

<?php 

/** Register the post type for the ads **/

add_action( 'init', 'register_cpt_ad' );

function register_cpt_ad() {

    $labels = array( 
        'name' => _x( 'Ads', 'ad' ),
        'singular_name' => _x( 'ad', 'ad' ),
        'add_new' => _x( 'Add New', 'ad' ),
        'add_new_item' => _x( 'Add New ad', 'ad' ),
        'edit_item' => _x( 'Edit ad', 'ad' ),
        'new_item' => _x( 'New ad', 'ad' ),
        'view_item' => _x( 'View ad', 'ad' ),
        'search_items' => _x( 'Search Ads', 'ad' ),
        'not_found' => _x( 'No ads found', 'ad' ),
        'not_found_in_trash' => _x( 'No ads found in Trash', 'ad' ),
        'parent_item_colon' => _x( 'Parent ad:', 'ad' ),
        'menu_name' => _x( 'Ads', 'ad' ),
    );

    $args = array( 
        'labels' => $labels,
        'hierarchical' => false,
        'description' => 'Adbank system',
        'supports' => array( 'title', 'excerpt', 'editor', 'thumbnail', 'custom-fields' ),
        'taxonomies' => array( 'category', 'type', 'campaign', 'post_tag' ),
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,     
        'show_in_nav_menus' => true,
        'publicly_queryable' => true,
        'exclude_from_search' => false,
        'has_archive' => true,
        'query_var' => true,
    'menu_position' => 3,
        'can_export' => true,
        'rewrite' => true,
        'capability_type' => 'post'
    );

    register_post_type( 'ad', $args );
}

/**Register the taxonomy for the ad types**/
add_action( 'init', 'register_taxonomy_type' );

function register_taxonomy_type() {

    $labels = array( 
        'name' => _x( 'Ad Type', 'type' ),
        'singular_name' => _x( 'Ad Types', 'type' ),
        'search_items' => _x( 'Search Ad Type', 'type' ),
        'popular_items' => _x( 'Popular Ad Type', 'type' ),
        'all_items' => _x( 'All Ad Type', 'type' ),
        'parent_item' => _x( 'Parent Ad Types', 'type' ),
        'parent_item_colon' => _x( 'Parent Ad Types:', 'type' ),
        'edit_item' => _x( 'Edit Ad Types', 'type' ),
        'update_item' => _x( 'Update Ad Types', 'type' ),
        'add_new_item' => _x( 'Add New Ad Types', 'type' ),
        'new_item_name' => _x( 'New Ad Types', 'type' ),
        'separate_items_with_commas' => _x( 'Separate ad type with commas', 'type' ),
        'add_or_remove_items' => _x( 'Add or remove ad type', 'type' ),
        'choose_from_most_used' => _x( 'Choose from the most used ad type', 'type' ),
        'menu_name' => _x( 'Ad Type', 'type' ),
    );

    $args = array( 
        'labels' => $labels,
        'public' => true,
        'show_in_nav_menus' => true,
        'show_ui' => true,
        'show_tagcloud' => true,
        'hierarchical' => true,
        'rewrite' => true,
        'query_var' => true
    );

    register_taxonomy( 'type', array('ad'), $args );
}

/**Register the taxonomy for the campaigns**/
add_action( 'init', 'register_taxonomy_campaign' );

function register_taxonomy_campaign() {

    $labels = array( 
        'name' => _x( 'Campaigns', 'campaign' ),
        'singular_name' => _x( 'Campaign', 'campaign' ),
        'search_items' => _x( 'Search campaigns', 'campaign' ),
        'popular_items' => _x( 'Popular campaigns', 'campaign' ),
        'all_items' => _x( 'All campaigns', 'campaign' ),
        'parent_item' => _x( 'Parent Campaign', 'campaign' ),
        'parent_item_colon' => _x( 'Parent Campaign:', 'campaign' ),
        'edit_item' => _x( 'Edit Campaign', 'campaign' ),
        'update_item' => _x( 'Update Campaign', 'campaign' ),
        'add_new_item' => _x( 'Add New Campaign', 'campaign' ),
        'new_item_name' => _x( 'New Campaign', 'campaign' ),
        'separate_items_with_commas' => _x( 'Separate campaigns with commas', 'campaign' ),
        'add_or_remove_items' => _x( 'Add or remove campaigns', 'campaign' ),
        'choose_from_most_used' => _x( 'Choose from the most used campaigns', 'campaign' ),
        'menu_name' => _x( 'Campaigns', 'campaign' ),
    );

    $args = array( 
        'labels' => $labels,
        'public' => true,
        'show_in_nav_menus' => true,
        'show_ui' => true,
        'show_tagcloud' => true,
        'hierarchical' => true,

        'rewrite' => true,
        'query_var' => true
    );

    register_taxonomy( 'campaign', array('ad'), $args );
}

function query_post_type($query) {
    $post_types = get_post_types();

    if ( is_category() || is_tag()) {

        $post_type = get_query_var('ad');

        if ( $post_type ) {
            $post_type = $post_type;
        } else {
            $post_type = $post_types;
        }

        $query->set('post_type', $post_type);

        return $query;
    }
}

add_filter('pre_get_posts', 'query_post_type');

/* NEW STUFF */

// Register the column
function price_column_register( $columns ) {
$columns['level'] = __( 'User Level', 'my-plugin' );

return $columns;
}
add_filter( 'manage_edit-ad_columns', 'price_column_register' );

// Display the column content
function price_column_display( $column_name, $post_id ) {
if ( 'level' != $column_name )
    return;
$roles = get_post_meta( $post->ID, '_members_access_role', true );
if ($roles == 'level1') echo 'level1';
echo 'hello';
}
add_action( 'manage_posts_custom_column', 'price_column_display', 10, 2 );

/**
 * Append the terms of multiple taxonomies to the list
 * of classes generated by post_class().
 *
 * @since 2010-07-10
 * @alter 2012-01-06
 */
function mysite_post_class( $classes, $class, $ID ) {

    $taxonomies = array(
        'type',
        'campaign',

    );

    $terms = get_the_terms( (int) $ID, $taxonomies );

    if ( is_wp_error( $terms ) || empty( $terms ) )
        return $classes;

    foreach ( (array) $terms as $term ) {
        if ( ! in_array( $term->slug, $classes ) )
            $classes[] = $term->slug;
    }

    return $classes;
}

add_filter( 'post_class', 'mysite_post_class', 10, 3 );
add_filter( 'tpicker_taxonomies', 'my_tpicker_taxonomies' );
function my_tpicker_taxonomies( $old_taxies) { // Filter taxonomy order

    foreach( array("Categories ", "Campaigns ", "Ad Type " ) as $tax_label) {
        $new_taxies[$tax_label] = $old_taxies[$tax_label];
    }

    return $new_taxies;
}

1 Answer 1

5

I think your are missing some concepts or not fully understanding.

  1. A taxonomy is a way to group things, ie, posts. The same taxonomy can be used for several post types, so a taxonomy is not under a post.
  2. The single template files are used for the view of a single post. You can make a different single template for each post type, even for each individual post.
  3. The specific template for a term of a taxonomy is taxonomy-{your_taxonomy}-{term}.php, in your case, it would be for example taxonomy-type-radio.php. This template will show all posts within the term radio of the 'type' taxonomy.

I think what you really want is to use a different single template based on the term of the taxonomy "type" associated to a 'Ad' post. I assume only one term of 'type' taxonomy can be selected per post. You can use template_include filter, or more specifically, the single_template filter:

<?php
function get_custom_single_template($single_template) {
    global $post;

    if ($post->post_type == 'ad') {
        $terms = get_the_terms($post->ID, 'type');
        if($terms && !is_wp_error( $terms )) {
            //Make a foreach because $terms is an array but it supposed to be only one term
            foreach($terms as $term){
                $single_template = dirname( __FILE__ ) . '/single-'.$term->slug.'.php';
            }
        }
     }
     return $single_template;
}

add_filter( "single_template", "get_custom_single_template" ) ;
?>

Put this in the functions.php file of your theme. Don't forget to create the single-*.php for each term (single-radio.php, single-tv.php, etc).

If you don't need a full template file and only need small modifications, like differents CSS classes, you can check if the post has the term and assign a diferrent class. For exmaple, in a common single template:

 <div class="<?php echo class="<?php echo has_term( 'radio', 'type' ) ? 'radio' : ''; ?>">
   <?php the_cotent();?>
</div>

Or

<?php
$terms = get_the_terms($post->ID, 'type');
$class = '';
//Make a foreach because $terms is an array but it supposed to be only one term
foreach($terms as $term){
    $class = $term->slug;
}
?>
<div class="<?php echo $class; ?>">
   <?php the_cotent();?>
</div>
10
  • Yes! I want to have a different template for each "type" So if the single post is in the radio category then it pulls the single radio template. Forgive my ignorance but where do I put your code? functions file?
    – Ben
    Commented Sep 13, 2013 at 6:20
  • Yes, in the functions.php file. Also you have to create single-radio.php, single-tv.php and so on ... in your theme folder.
    – cybmeta
    Commented Sep 13, 2013 at 10:01
  • That is what I thought and I did all this and for some reason it is only pulling from the single-ad.php it refuses to look at any other single-template. Even when I get rid of the single-ad.php it wont look to any other single file. Maybe the theme developer put something in here somewhere to say to only pull from that one. I just cant find it ;-(
    – Ben
    Commented Sep 13, 2013 at 14:04
  • In order to help you further I need to see the code you are using to register the custom post type and the custom taxonomy. Can you edit your question and add the code?
    – cybmeta
    Commented Sep 13, 2013 at 15:13
  • Can you also try again the code of my answer? I've edited it.
    – cybmeta
    Commented Sep 13, 2013 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.