簡體   English   中英

WordPress:從自定義帖子類型獲取所有標簽

[英]WordPress: Get all tags from custom post type

我正在嘗試獲取我的自定義帖子類型“資源”內的所有標簽。 問題是我不在循環中,努力尋找一種方法來使功能與自定義帖子類型一起使用。 我將類別設置也設置為“ 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;

有人可以幫忙嗎?

您要求使用正確的標簽,因為2015年的答案是列出類別而不是標簽

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

foreach($tags as $tag) { 
    var_dump($tag->name);
}
$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> ';
}

標簽也是WordPress分類法。 因此,您可以像獲取所有術語一樣獲得所有標簽。 閱讀更多

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

您還可以從“標簽”頁面URL復制自定義帖子分類法。

HTTP://localhost/wp-admin/edit-tags.php分類= YOUR_CUSTOM_POST_TAG_TAXONOMY_NAME&post_type =定制后型

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM