Php Wordpress自定义帖子类型-类别现在显示

Php Wordpress自定义帖子类型-类别现在显示,php,wordpress,Php,Wordpress,我为一个站点创建了一个自定义的帖子类型,我在管理中的帖子上显示了类别,但是当我在公共站点上调用它们时,列表总是空白的。我不确定我错过了什么 FUNCTIONS.PHP /* Custom Post Type Galleries */ function bhgallery_register_post_type() { $singular = 'Gallery'; $plural = 'Galleries'; $labels = array ( 'nam

我为一个站点创建了一个自定义的帖子类型,我在管理中的帖子上显示了类别,但是当我在公共站点上调用它们时,列表总是空白的。我不确定我错过了什么

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>
类别:

用于显示类别的代码-是否在循环中使用此代码?如果不是,那么它只会得到当前帖子的分类(如果是主页,可能没有)

否则,如果您试图获取文章类型的所有术语,使用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>';
}
$terms=get_terms('my_taxonomy');
如果(!empty($terms)&&!is_wp_error($terms)){
回声“
    ”; foreach($terms作为$term){ 回显“
  • ”.$term->name.“
  • ”; } 回声“
”; }
=========================更新============== 感谢您的澄清-请尝试使用以下方法在循环中获取自定义分类法:

<div class="portfolio-categories">Categories:<?php the_terms(get_the_ID(),'gallery_category'); ?></div>
类别:

用于显示类别的代码-是否在循环中使用此代码?如果不是,那么它只会得到当前帖子的分类(如果是主页,可能没有)

否则,如果您试图获取文章类型的所有术语,使用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>';
}
$terms=get_terms('my_taxonomy');
如果(!empty($terms)&&!is_wp_error($terms)){
回声“
    ”; foreach($terms作为$term){ 回显“
  • ”.$term->name.“
  • ”; } 回声“
”; }
=========================更新============== 感谢您的澄清-请尝试使用以下方法在循环中获取自定义分类法:

<div class="portfolio-categories">Categories:<?php the_terms(get_the_ID(),'gallery_category'); ?></div>
类别:

对不起,我应该更好地澄清一下,是的,它在循环中。我有每一篇文章的标题显示得很好,特色图片显示得很好,但由于某些原因,分类不正确。我甚至把你拥有的东西(在外观之外)放进去,它是空白的。这可能与我如何在functions.php中创建自定义分类法有关吗?感谢您的澄清-请尝试以下内容:Categories:非常好用。。不是为了给你更多的麻烦,但我也在使用它所属的
  • 类中的类别,但这会返回该类别的整个链接。。。如果我只想返回类别名称,但不想再返回了,该怎么办?再次感谢!PS您最初的out-of-a-loop函数确实有效,我只是用了一个错误的术语,我使用了自定义的post类型而不是自定义的分类法。哎呀!对不起,我应该澄清一下,是的,它在循环中。我有每一篇文章的标题显示得很好,特色图片显示得很好,但由于某些原因,分类不正确。我甚至把你拥有的东西(在外观之外)放进去,它是空白的。这可能与我如何在functions.php中创建自定义分类法有关吗?感谢您的澄清-请尝试以下内容:Categories:非常好用。。不是为了给你更多的麻烦,但我也在使用它所属的
  • 类中的类别,但这会返回该类别的整个链接。。。如果我只想返回类别名称,但不想再返回了,该怎么办?再次感谢!PS您最初的out-of-a-loop函数确实有效,我只是用了一个错误的术语,我使用了自定义的post类型而不是自定义的分类法。哎呀!