显示所有自定义分类和分类法描述的列表(甚至是空的分类)

时间:2013-10-15 16:09:50

标签: wordpress

我正在尝试使用以下代码显示自定义分类术语及其说明的列表。

我遇到的问题是,与自定义帖子类型无关的分类术语(即分类术语为空)将不会显示在列表中。知道如何让它们显示出来吗?

    $institutions = get_terms('institutions');

    foreach($institutions as $institution) {

        // variables
        $link = get_term_link(intval($institution->term_id),'institutions');
        $description = term_description(intval($institution->term_id),'institutions');

        // output
        echo '<article>';
        echo '<h3><a href="' . $link . '">' . $institution->name . '</a></h3>' . strip_tags(substr($description,0,350)) . '...';
        echo '</article>';

        };

以下是我用来注册分类法的代码:

add_action( 'init', 'inst_taxonomies', 0 );

function inst_taxonomies() {

    $htinstutitions_labels = array(
        'name'              => _x( 'Institutions', 'taxonomy general name' ),
        'singular_name'     => _x( 'Institution', 'taxonomy singular name' ),
        'search_items'      =>  __( 'Search in institutions' ),
        'all_items'         => __( 'All institutions' ),
        'most_used_items'   => null,
        'parent_item'       => null,
        'parent_item_colon' => null,
        'edit_item'         => __( 'Edit institution' ), 
        'update_item'       => __( 'Update institution' ),
        'add_new_item'      => __( 'Add new institution' ),
        'new_item_name'     => __( 'New institution' ),
        'menu_name'         => __( 'Institutions' ),
    );  
        register_taxonomy('institutions',array('jobs'),array(
        'hierarchical'      => true,
        'labels'            => $htinstutitions_labels,
        'show_admin_column' => true,
        'has_archive'       => true,
        'show_ui'           => true,
        'query_var'         => true,
        'hide_empty'        => 0,
        'rewrite'           => array('slug' => 'institutions' )
    ));
}

1 个答案:

答案 0 :(得分:0)

$args = array(
    'hide_empty'    => false, 
);
$institutions = get_terms('institutions',$args);

请参阅此处

<强> Get Terms