使用自定义帖子类型显示自定义分类

时间:2011-03-28 作者:don de lion

我们在custom post type的标题下设置了一个自定义post类型,以及两个自定义分类法。(以下代码已添加到我们的functions.php页面:)add\\u action(\'init\',\'create\\u seminar\\u post\\u type\');

函数create\\u seminar\\u post\\u type(){register\\u post\\u type(\'研讨会\',

        array(
                \'labels\'=> array(
                        \'name\' => \'Seminars\',
                        \'singular_name\' => \'Seminar\',
                        \'add_new\' => \'Add New Seminar\',
                        \'add_new_item\' => \'Add New Seminar\',
                        \'edit\' => \'Edit Seminars\',
                        \'edit_item\' => \'Edit Seminar\',
                        \'new_item\' => \'New Seminar\',
                        \'view\' => \'View Seminar\',
                        \'view_item\' => \'View Seminar\',
                        \'search_items\' => \'Search Seminars\',
                        \'not_found\' => \'No Seminars found\',
                        \'not_found_in_trash\' => \'No Seminars found in Trash\',
                        \'parent\' => \'Parent Seminar\',
                ),
                \'supports\' => array(\'title\', \'editor\', \'excerpt\', \'trackbacks\', \'custom-fields\', \'comments\', \'revisions\', \'thumbnail\', \'author\', \'page-attributes\'),
                \'public\' =>true,
                \'taxonomies\' => array(\'post_tag\', \'category\')
                )
    );
    register_taxonomy (\'trt\', \'seminars\', array(
    \'labels\' =>array(
                \'name\' =>\'Total Running Time\',
                \'singular_name\' => \'Total Running Time\',
                \'search_items\' => \'Search Total Running Time\',
                \'popular_items\' => \'Popular Total Running Times\',
                \'all_items\' => \'All Running Times\',
                \'parent_item\' => \'Parent Running Time\',
                \'edit_item\' => \'Edit Running Time\',
                \'update_item\' => \'Update Running Time\',
                \'add_new_item\' => \'Add New Running Time\',
                \'new_item_name\' => \'New Running Time Name\'
    ),
                \'hierarchical\' => true, \'label\' => \'Total Running Time\'));
    register_taxonomy (\'discs-in-set\', \'seminars\', array(
    \'labels\' =>array(
                \'name\' =>\'# of Discs in Set\',
                \'singular_name\' => \'Total Discs in Set\',
                \'search_items\' => \'Search Discs in Set\',
                \'popular_items\' => \'Popular Discs in Set\',
                \'all_items\' => \'All Discs in Set\',
                \'parent_item\' => \'Parent Discs in Set\',
                \'edit_item\' => \'Edit Discs in Set\',
                \'update_item\' => \'Update Discs in Set\',
                \'add_new_item\' => \'Add New Discs in Set\',
                \'new_item_name\' => \'New Discs in Set\'
    ),
    \'hierarchical\' => false,
    \'public\' => true,
    \'show_ui\' => true,
    \'query_var\' => \'discs-in-set\',
    \'show_tagcloud\' => true,
    ));
}

下面是在我们的单个研讨会页面(源自single.php页面)中添加的循环代码:

<?php $loop = new WP_Query( array( \'post_type\' => \'seminars\', \'posts_per_page\' => 10 ) ); ?>
        <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
        <?php the_title( \'<h2 class="entry-title"><a href="\' . get_permalink() . \'" title="\' . the_title_attribute( \'echo=0\' ) . \'" rel="bookmark">\', \'</a></h2>\' ); ?>
我们想为每个研讨会展示的是研讨会信息,以及运行时间和光盘集的自定义分类信息。但所显示的只是研讨会帖子及其附带的类别&;标签。我应该怎么做才能让它工作?

同样,在仪表板中查看研讨会列表时,它会显示所有列表,其中包含标题、作者、类别、标记、评论和日期列。为了简单起见,最好将自定义分类法合并到此列表中。这可以通过自定义分类法来完成吗?还是有更直接的方式?非常感谢您的帮助/见解。。

1 个回复
最合适的回答,由SO网友:Rev. Voodoo 整理而成

对于仪表板后列表位。。。。

http://codex.wordpress.org/Plugin_API/Action_Reference/manage_posts_custom_column

    // ADDS EXTRA INFO TO ADMIN MENU FOR PRODUCT POST TYPE
add_filter("manage_edit-ve_products_columns", "voodoo_prod_edit_columns");
add_action("manage_posts_custom_column", "voodoo_prod_custom_columns");

function voodoo_prod_edit_columns( $columns ) {

    // Add the extra column for product categories instead of rebuilding the whole array
    $columns[\'prod_cat\'] = "Product Category";
    $columns[\'description\'] = "Excerpt";

    return $columns;
}

function voodoo_prod_custom_columns( $column ) {
    global $post;
    switch( $column ) {
        case "description":
            the_excerpt();
        break;
        case "prod_cat":
            echo get_the_term_list( $post->ID, \'product_category\', \'\', \', \', \'\' );
        break;
    }
}
我使用它将两列添加到我的ve\\U产品自定义帖子类型中。描述列显示摘录,prod\\u cat显示我的自定义分类法(product\\u类别)。摆脱:

$columns[\'description\'] = "Excerpt";
将删除摘录部分,只引入分类法。你只需要用你自己的税名交换。在第一个过滤器中,您自己的CPT(根据我提供的链接)


至于你的另一个问题,你只是想说出分类法吗?我不确定,所以我会尽量回答你的问题。

            <?php   
            // Let\'s find out if we have taxonomy information to display   
            // Something to build our output in   
            $taxo_text = \'\';   

            // Variables to store each of our possible taxonomy lists   
            // This one checks for a Product Category classification   
            $prodcat_list = get_the_term_list( $post->ID, \'product_category\', \'\', \', \', \'\' );

            // Add Product Category list if this post was so tagged   
            if ( \'\' != $prodcat_list )   
            $taxo_text .= $prodcat_list;
            ?>
我使用它来存储分类法(product\\u类别)。我把它放在循环的顶部,上面是我需要显示实际税额的地方。然后我可以使用以下方法将其吐出:

<?php echo $taxo_text; ?>
因此,这一大块代码加载product\\u category的术语,然后回显taxo文本并吐出术语,其行为与\\u category类似。您只需在我有product\\u类别的地方以您的税务名称进行交换

结束

相关推荐