2012-09-22 28 views
5

我怎樣才能得到一個分類標識或名稱只有分類學slug?WordPress的獲得分類名稱與slu 012

我想我正在尋找等效的get_term_by(),但對於分類法。

編輯:我必須指定我試圖獲得WooCommerce產品屬性的稅號。

感謝

回答

1
<?php 
    $term = get_term_by('slug', $slug, 'category'); 
    $name = $term->name; 
    $id = $term->term_id; 
?> 
+3

這不回答這個問題。它只會找到一個術語的名稱,而不是一個分類法。 – David

2

作爲公認的答案沒有回答這個問題,我在這裏提供一個答案,即使問題已經很老了。

get_term_by()的第三個(必需)參數是分類本身的名稱,所以不能使用此函數。

get_taxonomies()無法使用,因爲那麼你將不得不匹配整個重寫數組,你可能無法訪問。

所以我發現的唯一方式是使用專有陣列$wp_taxonomies

function get_tax_name_from_slug($slug){ 
    foreach ($wp_taxonomies as $key => $value) { 
    if ($value->rewrite['slug'] === $slug){ 
     return $key; 
    } 
    } 
} 

我真的希望WordPress會提供一種方式來做到這一點,而無需訪問它們的內部數據結構。

7

WordPress確實提供了一個從其子彈中獲取分類信息的函數。

$taxonomy_details = get_taxonomy($slug); 

這將返回分類標準細節作爲一個對象,其中包括分類標籤的各種標籤。例如,當爲標準類別分類標準調用時,這是返回的對象。 get_taxonomy('category');

stdClass Object 
(
    [labels] => stdClass Object 
     (
      [name] => Categories 
      [singular_name] => Category 
      [search_items] => Search Categories 
      [popular_items] => 
      [all_items] => All Categories 
      [parent_item] => Parent Category 
      [parent_item_colon] => Parent Category: 
      [edit_item] => Edit Category 
      [view_item] => View Category 
      [update_item] => Update Category 
      [add_new_item] => Add New Category 
      [new_item_name] => New Category Name 
      [separate_items_with_commas] => 
      [add_or_remove_items] => 
      [choose_from_most_used] => 
      [not_found] => No categories found. 
      [menu_name] => Categories 
      [name_admin_bar] => category 
     ) 

    [description] => 
    [public] => 1 
    [hierarchical] => 1 
    [show_ui] => 1 
    [show_in_menu] => 1 
    [show_in_nav_menus] => 1 
    [show_tagcloud] => 1 
    [show_in_quick_edit] => 1 
    [show_admin_column] => 1 
    [meta_box_cb] => post_categories_meta_box 
    [rewrite] => Array 
     (
      [hierarchical] => 1 
      [slug] => category 
      [with_front] => 1 
      [ep_mask] => 512 
     ) 

    [query_var] => category_name 
    [update_count_callback] => 
    [_builtin] => 1 
    [cap] => stdClass Object 
     (
      [manage_terms] => manage_categories 
      [edit_terms] => manage_categories 
      [delete_terms] => manage_categories 
      [assign_terms] => edit_posts 
     ) 

    [name] => category 
    [object_type] => Array 
     (
      [0] => post 
     ) 

    [label] => Categories 
) 

來源:https://codex.wordpress.org/Function_Reference/get_taxonomy

+0

slu is與分類法的名稱不一樣。你的例子只能用,因爲在這種情況下它們碰巧是平等的。 get_taxonomy需要一個分類名稱,而不是一個slu子。 – Hjalmar

+0

@Hjalmar我認爲原始的海報是指分類學 - >名稱爲slu,,分類 - >標籤作爲名稱。我面臨與WC屬性一樣的問題。 – David

0
$args = array(
        'post_type' => 'awards', 
        'post_status' => 'publish', 
        'posts_per_page' => 4, 
        'orderby' => 'ID', 
        'order' => 'DESC', 
        'tax_query' => array(
         'relation' => 'AND', 
         array(
          'taxonomy' => 'awards_categories', 
          'field' => 'slug', 
          'terms' => $award_solution 
         ), 
         array(
          'taxonomy' => 'year', 
          'field' => 'slug', 
          'terms' => $yearvalue 
         ), 
        ) 
       ); 

我們如何與WP選擇查詢獲取此

相關問題