如何显示我的自定义帖子类型的类别?

时间:2013-08-01 作者:Laniakea

我有一个自定义的帖子类型。我想做的是在项目顶部显示项目类别,以便访问者可以相应地筛选项目。

在我的functions.php 我有:

<?php

require_once(\'portfolio-type.php\');
add_filter(\'excerpt_length\', \'my_excerpt_length\');

function my_excerpt_length($length) {
return 25; 
}

add_filter(\'excerpt_more\', \'new_excerpt_more\');  
function new_excerpt_more($text){  

return \' \';  
}  

function portfolio_thumbnail_url($pid){
$image_id = get_post_thumbnail_id($pid);  
$image_url = wp_get_attachment_image_src($image_id,\'screen-shot\');  
return  $image_url[0];  
}
?>
在中portfolio-type.php:

 <?php

 if ( function_exists( \'add_theme_support\' ) ) { 
add_theme_support( \'post-thumbnails\' );
set_post_thumbnail_size( 270, 170, true ); // Normal post thumbnails
add_image_size( \'screen-shot\', 720, 540 ); // Full size screen
 }

 add_action(\'init\', \'portfolio_register\');  

 function portfolio_register() {  
 $args = array(  
    \'label\' => __(\'Portfolio\'),  
    \'singular_label\' => __(\'Project\'),  
    \'public\' => true,  
    \'show_ui\' => true,  
    \'capability_type\' => \'post\',  
    \'hierarchical\' => false,  
    \'rewrite\' => true,  
    \'supports\' => array(\'title\', \'editor\', \'thumbnail\')  
   );  

register_post_type( \'portfolio\' , $args );  
 }  

register_taxonomy("project-type", array("portfolio"), array("hierarchical"  =>  true, "label" => "Project Types", "singular_label" => "Project Type",   "rewrite" => true));
 ?>
最后在我的索引中。php我有:

<!-- Start Projects -->

                    <div id="posts" class="row isotope">

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

                          <?php  
                              $title= str_ireplace(\'"\', \'\', trim(get_the_title()));  
                              $desc= str_ireplace(\'"\', \'\', trim(get_the_content()));  
                          ?>     

                          <div class="item post item span4 isotope-item">

                            <a class="project-wrp fancybox" title="<?=$title?>" rel="lightbox[work]" href="<?php print portfolio_thumbnail_url($post->ID) ?>"><div class="profile-photo"><div class="profile-icon">&#0102;</div><?php the_post_thumbnail(array(\'230\',\'170\'),array(\'alt\' => \'\')); ?> </div>  
                            <div class="project-name"><?php echo $title; ?></div>
                            <div class="project-client"><?php echo $desc; ?></div>
                            </a>
                          </div>  
                      <?php endwhile; endif; ?>  


                    </div>

3 个回复
最合适的回答,由SO网友:Rahul Verma 整理而成

从公文包类型中删除您的代码。注册post类型和分类法的php(第9行之后)。

使用以下代码(在portfolio type.php中)注册post类型“portfolio”

function portfolio_register() {
    $labels = array(
        \'name\' => _x(\'Portfolio\', \'post type general name\'),
        \'singular_name\' => _x(\'Portfolio Item\', \'post type singular name\'),
        \'add_new\' => _x(\'Add New\', \'portfolio item\'),
        \'add_new_item\' => __(\'Add New Portfolio Item\'),
        \'edit_item\' => __(\'Edit Portfolio Item\'),
        \'new_item\' => __(\'New Portfolio Item\'),
        \'view_item\' => __(\'View Portfolio Item\'),
        \'search_items\' => __(\'Search Portfolio Items\'),
        \'not_found\' =>  __(\'Nothing found\'),
        \'not_found_in_trash\' => __(\'Nothing found in Trash\'),
        \'parent_item_colon\' => \'\'
    );
    $args = array(
        \'labels\' => $labels,
        \'public\' => true,
        \'publicly_queryable\' => true,
        \'show_ui\' => true,
        \'query_var\' => true,
        \'rewrite\' => true,
        \'capability_type\' => \'post\',
        \'hierarchical\' => false,
        \'menu_position\' => 8,
        \'supports\' => array(\'title\',\'editor\',\'thumbnail\')
    ); 
    register_post_type( \'portfolio\' , $args );
}
add_action(\'init\', \'portfolio_register\');
使用以下代码(在portfolio type.php中)为post类型“portfolio”注册分类法“portfolio\\u categories”,使其具有层次结构(如类别)

function create_portfolio_taxonomies() {
    $labels = array(
        \'name\'              => _x( \'Categories\', \'taxonomy general name\' ),
        \'singular_name\'     => _x( \'Category\', \'taxonomy singular name\' ),
        \'search_items\'      => __( \'Search Categories\' ),
        \'all_items\'         => __( \'All Categories\' ),
        \'parent_item\'       => __( \'Parent Category\' ),
        \'parent_item_colon\' => __( \'Parent Category:\' ),
        \'edit_item\'         => __( \'Edit Category\' ),
        \'update_item\'       => __( \'Update Category\' ),
        \'add_new_item\'      => __( \'Add New Category\' ),
        \'new_item_name\'     => __( \'New Category Name\' ),
        \'menu_name\'         => __( \'Categories\' ),
    );

    $args = array(
        \'hierarchical\'      => true, // Set this to \'false\' for non-hierarchical taxonomy (like tags)
        \'labels\'            => $labels,
        \'show_ui\'           => true,
        \'show_admin_column\' => true,
        \'query_var\'         => true,
        \'rewrite\'           => array( \'slug\' => \'categories\' ),
    );

    register_taxonomy( \'portfolio_categories\', array( \'portfolio\' ), $args );
}
add_action( \'init\', \'create_portfolio_taxonomies\', 0 );
然后使用以下代码检索模板文件(如index.php)中的分类术语

<?php

$taxonomy = \'portfolio_categories\';
$terms = get_terms($taxonomy); // Get all terms of a taxonomy

if ( $terms && !is_wp_error( $terms ) ) :
?>
    <ul>
        <?php foreach ( $terms as $term ) { ?>
            <li><a href="<?php echo get_term_link($term->slug, $taxonomy); ?>"><?php echo $term->name; ?></a></li>
        <?php } ?>
    </ul>
<?php endif;?>

?>
如果您需要任何澄清,请告诉我。

SO网友:Adam Pery

对于此公文包帖子类别列表,请使用:

<?php echo get_the_term_list(get_the_ID(), \'portfolio_category\', \'\', \', \', \'\'); ?>

SO网友:Scott Shubham

This code works.

$term_args = array(
        \'post_type\'              => \'portfolio\',
         \'taxonomy\'               => \'project-type\',
        \'hide_empty\'             => true,
        \'fields\'                 => \'all\'
      );
    $term_query = new WP_Term_Query( $term_args );
结束

相关推荐

JQuery在WordPress php文件中不起作用

我正在尝试使用jQuery垂直滚动div内容。我在中使用了以下代码行function.phpadd_action( \'wp_enquque_scripts\', \'my_script_enqueue\' ); function my_script_enqueue() { wp_enqueue_script( \'my-jquery\', \'/wordpress/wp-content/themes/MyTheme/js/jquery-1.9.0.js\', array