如何按自定义类别列出自定义帖子?

时间:2014-01-29 作者:Let\'s Code

根据一些参考资料,我编写了创建自定义帖子类型的代码(journal) 以及使用以下代码进行分类。它工作得很好。接下来,我将从管理端为我的自定义帖子类型添加一个新类别(journal) 调用featured. 我可以在管理端按类别对此帖子类型进行排序Admin part. 但在用户端,当我列出仅显示的类别时,我无法列出它们post categories 如下图所示。下图显示我的自定义帖子类型列表(journal) . 如何在用户端按类别名称显示此日志列表?enter image description here

function post_type_journal()
    {

        $labels = array(
        \'name\' => _x(\'Journals\', \'post type general name\'),
        \'singular_name\' => _x(\'Journal\', \'post type singular name\'),
        \'add_new\' => _x(\'Add New\', \'journal\'),
        \'add_new_item\' => __(\'Add New Journal\'),
        \'edit_item\' => __(\'Edit Journal\'),
        \'new_item\' => __(\'New Journal\'),
        \'view_item\' => __(\'View Journal\'),
        \'search_items\' => __(\'Search Journal\'),
        \'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,
        \'menu_icon\' => null,
        \'rewrite\' => true,
        \'capability_type\' => \'post\',
        \'hierarchical\' => false,
        \'menu_position\' => null,
        \'supports\' => array(\'title\',\'editor\',\'thumbnail\'),

      ); 
        register_post_type(\'journal\', $args);
    }
    add_action(\'init\',\'post_type_journal\');
function themes_taxonomy() {

        register_taxonomy(\'article\', \'journal\', array(
        // Hierarchical taxonomy (like categories)
        \'hierarchical\' => true,
        \'query_var\' => true,        
        // This array of options controls the labels displayed in the WordPress Admin UI
        \'labels\' => array(
            \'name\' => _x( \'Journal Category\', \'taxonomy general name\' ),
            \'singular_name\' => _x( \'Journal-Category\', \'taxonomy singular name\' ),
            \'search_items\' =>  __( \'Search Journal-Categories\' ),
            \'all_items\' => __( \'All Journal-Categories\' ),
            \'parent_item\' => __( \'Parent Journal-Category\' ),
            \'parent_item_colon\' => __( \'Parent Journal-Category:\' ),
            \'edit_item\' => __( \'Edit Journal-Category\' ),
            \'update_item\' => __( \'Update Journal-Category\' ),
            \'add_new_item\' => __( \'Add New Journal-Category\' ),
            \'new_item_name\' => __( \'New Journal-Category Name\' ),
            \'menu_name\' => __( \'Journal Categories\' ),
        ),

        \'rewrite\' => array(
            \'slug\' => \'article\', // This controls the base slug that will display before each term
            \'with_front\' => false, // Don\'t display the category base before "/locations/"            
            \'hierarchical\' => true // This will allow URL\'s like "/locations/boston/cambridge/"
        ),
    ));
}
add_action( \'init\', \'themes_taxonomy\',0);  
以下是输出的用户端代码,如第二幅图所示

$categories = get_categories($args);
  foreach($categories as $category) { 
    echo \'<p>Category: <a href="\' . get_category_link( $category->term_id ) . \'" title="\' . sprintf( __( "View all posts in %s" ), $category->name ) . \'" \' . \'>\' . $category->name.\'</a> </p> \';
    echo \'<p> Description:\'. $category->description . \'</p>\';
    echo \'<p> Post Count: \'. $category->count . \'</p>\';  } 


        $type = \'journal\';
                echo  $cat_id=get_cat_ID(\'featured\');
        $args=array(
                 \'cat\'=>$cat_id,
         \'post_type\' => $type,
         \'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() ) {
         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();  // Restore global post data stomped by the_post().
        ?>

1 个回复
SO网友:Thiago Macedo

使用类别与使用自定义分类法不同。您应该调整代码以使用get_terms 并查询custom tax:

$categories = get_terms(\'article\');
  foreach($categories as $category) { 
    $args=array(
     \'article\'=>$category->slug,
     \'post_type\' => \'journal\',
     \'post_status\' => \'publish\',
     \'posts_per_page\' => -1,
     \'caller_get_posts\'=> 1);
  }

结束

相关推荐

具有自定义分类的自定义帖子类型中的WP_DROPDOWN_CATEGORIES

我有一个自定义的帖子类型,它有自己的分类法,基本上“show Vinces”是帖子类型,Vincement regions是分类法。看到一个场馆无法在多个地区存在,我删除了默认的metta框,并使用wp_dropdown_categories(). 分类法项目正在输出并按我所希望的方式显示,但它们不会被提交,并且下拉列表在提交后不会保留所选内容。我已经尽我所能地查看原始metabox的各种属性,并尝试将这些属性应用到下拉列表中,但到目前为止,我没有任何乐趣。我看过一些various WPSE上的帖子和ha