問題描述

預設情況下,WordPress 按字母順序排序自定義分類法 (作為標籤),而不是按照在標籤框中輸入的順序。

有人知道有什麼方法可以按照他們在帖子編輯螢幕中輸入的順序顯示自定義分類標準?

該網址是:http://granadatheater.com/

GGW(Goes Good With) 藝術家目前是按字母順序排列的,他們希望改變它們,這樣它們的輸入方式與輸入方式相同。

所以如果輸入它的 Artist1,Artist3,Artist2 這是如何應該出現在網站的前端。

最佳解決方案

這是不可能的 「開箱即用」…

預設的’orderby’ 選項是 (升序或降序)

  • ID 名稱

  • 預設

  • 金屬塊

  • 計數

  • term_group

這些都在 codex 中詳細說明。

那就是說有一些聰明的女士們紳士在這裡如果有人能解決這個問題,我可以確定這些人中的一個

次佳解決方案

經過相當多的搜尋和廣泛的測試,我找到了答案。

將此程式碼新增到主題的 functions.php 中:

function set_the_terms_in_order ( $terms, $id, $taxonomy ) {
    $terms = wp_cache_get( $id, "{$taxonomy}_relationships_sorted" );
    if ( false === $terms ) {
        $terms = wp_get_object_terms( $id, $taxonomy, array( 'orderby' => 'term_order' ) );
        wp_cache_add($id, $terms, $taxonomy . '_relationships_sorted');
    }
    return $terms;
}
add_filter( 'get_the_terms', 'set_the_terms_in_order' , 10, 4 );

function do_the_terms_in_order () {
    global $wp_taxonomies;  //fixed missing semicolon
    // the following relates to tags, but you can add more lines like this for any taxonomy
    $wp_taxonomies['post_tag']->sort = true;
    $wp_taxonomies['post_tag']->args = array( 'orderby' => 'term_order' );    
}
add_action( 'init', 'do_the_terms_in_order');

(信用:這是基於 – 但改進 – http://wordpress.kdari.net/2011/07/listing-tags-in-custom-order.html)

第三種解決方案

我一直在努力尋找一個自定義分類法的字母子句的答案… 我不建議改變核心 WP 檔案,所以這裡是我新增到我的 taxonomy.php 檔案列出自定義分類法描述,連結以字母順序排列為小孩。修改以滿足您的需要,我希望這有助於某人在那裡。

// Get Main Taxonomy for use in template file
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$termTaxonomy = $term->taxonomy;

<h1><?php echo apply_filters( 'the_title', $term->name ); ?></h1>

<?php // test for description before unleashing a div 
if ( !empty( $term->description ) ): 
  echo '<div class="description">';
  echo $term->description;
  echo '</div>;
endif; ?>

// Now get children terms, using get_term & 'child_of' get's us alphabetical order
$termchildren = get_terms( $termTaxonomy, array(
  'child_of'     => $term->term_id,
  'hierarchical' => 0,
  'fields'       => 'ids',
  'hide_empty'   => 0
) );

// Make an alphabetical linked list
echo '<ul>';
foreach ($termchildren as $child) {
  $term = get_term_by( 'id', $child, $termTaxonomy );

  // Modify this echo to customize the output for each child term
  echo '<li><a href="http://'%20.%20get_term_link(%20$term->name,%20$termTaxonomy%20)%20.%20'" alt="' .$term->description. '">' . $term->name . '</a></li>';
}
echo '</ul>';

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。