1

我正在尝试获取自定义帖子类型“资源”中的所有标签。

问题是我在循环之外并且努力寻找一种方法来获得与自定义帖子类型一起使用的功能。

我的类别设置也为“resource_category”

我目前的代码是:

$tax = 'post_tag';
$terms = get_terms( $tax );
$count = count( $terms );

if ( $count > 0 ): ?>
    <div class="post-tags">
    <?php
    foreach ( $terms as $term ) {
        $term_link = get_term_link( $term, $tax );
        echo '<a href="' . $term_link . '" class="tax-filter" title="' . $term->slug . '">' . $term->name . '</a> ';
    } ?>
    </div>
<?php endif;

任何人都可以帮忙吗?

4

3 回答 3

6

您要求正确的标签,因为 2015 年的答案是列出类别而不是标签

$args = array(
        'type' => get_post_type(),
        'orderby' => 'name',
        'order' => 'ASC'
);
$tags = get_tags($args);

foreach($tags as $tag) { 
    var_dump($tag->name);
}
于 2019-01-21T11:15:15.407 回答
4

标签也是 WordPress 分类法。因此,您可以获得所有标签,就像您获得所有条款一样 阅读更多

$tags = get_terms([
          'taxonomy'  => 'YOUR_CUSTOM_TAXONOMY',
          'hide_empty'    => false
        ]);
var_dump($tags);

您还可以从标签页面 URL 复制自定义帖子分类。

http://localhost/wp-admin/edit-tags.php?taxonomy=YOUR_CUSTOM_POST_TAG_TAXONOMY_NAME&post_type=custom-post-type

于 2019-07-16T06:32:06.360 回答
-3
$args = array(
 'type' => 'resource',
 'orderby' => 'name',
 'order' => 'ASC'
);
$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> ';
}
于 2015-03-24T10:39:48.000 回答