自定义邮政类型分类单一模板

时间:2013-09-13 作者:Ben

我创建了一个名为“Ad”的自定义帖子类型,然后在该类型下有一个分类法,名为“type”,我在类型类别中添加了一些术语,名为“Radio”、“TV”、“Print”。我希望WordPress使用不同的single.php 每个术语的模板。我想有一个不同于打印模板的广播模板。我试过了taxonomy-ad-type-radio.php 我试过了single-ad-type-radio.php 以及所有其他形式的。有人能告诉我实现这一目标的最佳方法是什么吗?

我已将筛选器添加到functions.php 文件也没有用。我知道我只是错过了一些很容易的事情。非常感谢你的帮助。

<?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 个回复
SO网友:cybmeta

我认为您缺少一些概念或没有完全理解。

分类法是对事物进行分组的一种方法,即对帖子进行分组。相同的分类法可用于多个帖子类型,因此分类法不在帖子下

  • single 模板文件用于查看单个帖子。您可以为每种帖子类型创建不同的单个模板,甚至可以为每个帖子创建不同的模板taxonomy-{your_taxonomy}-{term}.php, 以你为例taxonomy-type-radio.php. 此模板将显示“类型”分类法中术语无线电范围内的所有帖子single 基于与“广告”帖子关联的分类法“类型”术语的模板。我想only one term of \'type\' taxonomy can be selected per post. 您可以使用template_include 过滤器,或更具体地说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" ) ;
    ?>
    
    将其放入函数中。主题的php文件。别忘了创建一个-*。每个术语的php(single-radio.php、single-tv.php等)。

    如果你不需要一个完整的模板文件,只需要一些小的修改,比如不同的CSS类,那么你可以检查帖子是否有这个术语,并分配一个不同的类。对于exmaple,在一个通用的单一模板中:

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

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

  • 结束

    相关推荐

    Taxonomy as checkboxes

    因此,我试图让Wordpress将类别显示为自定义元框中的一系列选择框,并使用以下示例tutorial, 但当我去保存选择时,它会以预期选择ID的形式生成其他类别。它也无法保存多个选择。 <?php // Add the Meta Box function add_custom_meta_box() { add_meta_box( \'custom_meta_box\', // $id&