我为网站创建了自定义帖子类型,并且我在管理员的帖子中显示了类别,但是当我在公共网站上调用它们时,列表始终为空白。我不确定我错过了什么。
的functions.php
/* Custom Post Type Galleries */
function bhgallery_register_post_type() {
$singular = 'Gallery';
$plural = 'Galleries';
$labels = array (
'name' => $plural,
'singular' => $singular,
'add_name' => 'Create New',
'add_new_item' => 'Create New ' . $singular,
'edit' => 'Edit',
'edit_item' => 'Edit ' . $singular,
'new_item' => 'New' . $singular,
'view' => 'View' . $singular,
'view_item' => 'View' . $singular,
'search_term' => 'Search ' . $plural,
'parent' => 'Parent ' . $singular,
'not_found' => 'No ' . $plural . ' Found',
'not_found_in_trash' => 'No ' . $plural . ' in Trash'
);
$args = array (
'labels' => $labels,
'public' => true,
'public_queryable' => true,
'exclude_from_search' => false,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 10,
'menu_icon' => 'dashicons-camera',
'can_export' => true,
'delete_with_user' => false,
'hierarchical' => false,
'has_archive' => true,
'query_var' => true,
'capability_type' => 'post',
'map_meta_cap' => true,
// 'capabilities' => array();
'rewrite' => array(
'slug' => 'gallery',
'with_front' => true,
'pages' => true,
'feeds' => false,
),
'supports' => array(
'title',
'thumbnail',
'editor'
)
);
register_post_type( 'bh_gallery', $args );
}
add_action ( 'init', 'bhgallery_register_post_type');
/** Custom Categories for Gallery **/
function bhgallery_register_taxonomy() {
$plural = 'Categories';
$singular = 'Category';
$labels = array (
'name' => $plural,
'singular_name' => $singular,
'search_items' => 'Search ' . $plural,
'popular_items' => 'Popular ' . $plural,
'all_items' => 'All ' . $plural,
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => 'Edit ' . $singular,
'update_item' => 'Update ' . $singular,
'add_new_item' => 'Add New ' . $singular,
'new_item_name' => 'New ' . $singular . ' Name',
'separate_items_with_comas' => 'Seperate ' . $singular . ' with commas',
'add_or_remove_items' => 'Add or remove ' . $plural,
'choose_from_most_used' => 'Choose from the most used ' . $plural,
'not_found' => 'No ' . $plural . 'fount',
'menu_name' => $plural,
);
$args = array (
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array( 'slug' => 'categories'),
);
register_taxonomy( 'gallery category', 'bh_gallery', $args );
}
add_action ( 'init', 'bhgallery_register_taxonomy');
主页
<div class="portfolio-categories">Categories:<?php the_category(', '); ?></div>
答案 0 :(得分:0)
您用来显示类别的代码 - 您是在The Loop中使用它吗?如果没有,那么它只会获得当前帖子的类别(如果它是主页,则可能没有)。
否则,如果您尝试获取帖子类型的所有条款,则使用get_terms()http://codex.wordpress.org/Function_Reference/get_terms将允许您按帖子类型获取条款,然后您可以循环显示它们:
$terms = get_terms( 'my_taxonomy' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
echo '<ul>';
foreach ( $terms as $term ) {
echo '<li>' . $term->name . '</li>';
}
echo '</ul>';
}
=================== UPDATE ============== 感谢您的澄清 - 尝试使用以下内容代替在循环中获取自定义分类:
<div class="portfolio-categories">Categories:<?php the_terms(get_the_ID(),'gallery_category'); ?></div>