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