2017-09-13 79 views
0

的WordPress的get_the_category()archive.php文件同時顯示類別塞網址傳回兩大類,數組一個不同,不是以「aktuality-博客」類我用散熱片實現相關。WordPress的get_the_category()將返回的東西,我不想

這裏是var_dump(get_the_category());

array(2) { 
    [0]=> 
    object(WP_Term)#4190 (16) { 
    ["term_id"]=> 
    int(2) 
    ["name"]=> 
    string(9) "Aktuality" 
    ["slug"]=> 
    string(9) "aktuality" 
    ["term_group"]=> 
    int(0) 
    ["term_taxonomy_id"]=> 
    int(2) 
    ["taxonomy"]=> 
    string(8) "category" 
    ["description"]=> 
    string(0) "" 
    ["parent"]=> 
    int(0) 
    ["count"]=> 
    int(18) 
    ["filter"]=> 
    string(3) "raw" 
    ["cat_ID"]=> 
    int(2) 
    ["category_count"]=> 
    int(18) 
    ["category_description"]=> 
    string(0) "" 
    ["cat_name"]=> 
    string(9) "Aktuality" 
    ["category_nicename"]=> 
    string(9) "aktuality" 
    ["category_parent"]=> 
    int(0) 
    } 
    [1]=> 
    object(WP_Term)#4188 (16) { 
    ["term_id"]=> 
    int(30) 
    ["name"]=> 
    string(4) "Blog" 
    ["slug"]=> 
    string(14) "aktuality-blog" 
    ["term_group"]=> 
    int(0) 
    ["term_taxonomy_id"]=> 
    int(30) 
    ["taxonomy"]=> 
    string(8) "category" 
    ["description"]=> 
    string(0) "" 
    ["parent"]=> 
    int(0) 
    ["count"]=> 
    int(1) 
    ["filter"]=> 
    string(3) "raw" 
    ["cat_ID"]=> 
    int(30) 
    ["category_count"]=> 
    int(1) 
    ["category_description"]=> 
    string(0) "" 
    ["cat_name"]=> 
    string(4) "Blog" 
    ["category_nicename"]=> 
    string(14) "aktuality-blog" 
    ["category_parent"]=> 
    int(0) 
    } 
} 

我想到剛陣列1是陣列0,沒有分類的現狀存在。我可以通過插入數組[1] category int get_posts的$ arg來解決問題。

我不想要解決方法,但堅實的解決方案,如果他們創建的產品也可以在不同的類別上工作,只需正常存檔,我可以在類別slu get上獲得,我可以直接瀏覽它,獲得本地分頁等等。 ,而不是解決方法,我最終必須重寫整個cms。

+0

你在默認WP主題的工作? –

+0

不,編寫自定義...根本沒有功能變化。 – azurinko

回答

1

所以在Wordpress中,您可以有多個與給定帖子關聯的類別。

因此,您所遇到的是一種正常行爲,您只需爲所需帖子選擇一個類別即可避免這種情況。

因此,這裏是幾件事情可以做

排除從帖主查詢的類別。

function exclude_category($query) { 
    if ($query->is_home() && $query->is_main_query()) { 
     $query->set('cat', '-1,-1347'); 
    } 
} 
add_action('pre_get_posts', 'exclude_category'); 

https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

遍歷類,並選擇當前上下文

foreach(get_the_category() as $term){ 
    if($term->slug == 'aktuality'){ 
     // do something 
    } 
} 

也許這重要嗎?

安裝Yoast插件允許使用主類別或獨立於此處的插件使用分叉代碼。

https://joshuawinn.com/using-yoasts-primary-category-in-wordpress-theme/

歧路代碼(didnt親自測試)

<?php 
// SHOW YOAST PRIMARY CATEGORY, OR FIRST CATEGORY 
$category = get_the_category(); 
$useCatLink = true; 
// If post has a category assigned. 
if ($category){ 
    $category_display = ''; 
    $category_link = ''; 
    if (class_exists('WPSEO_Primary_Term')) 
    { 
     // Show the post's 'Primary' category, if this Yoast feature is available, & one is set 
     $wpseo_primary_term = new WPSEO_Primary_Term('category', get_the_id()); 
     $wpseo_primary_term = $wpseo_primary_term->get_primary_term(); 
     $term = get_term($wpseo_primary_term); 
     if (is_wp_error($term)) { 
      // Default to first category (not Yoast) if an error is returned 
      $category_display = $category[0]->name; 
      $category_link = get_category_link($category[0]->term_id); 
     } else { 
      // Yoast Primary category 
      $category_display = $term->name; 
      $category_link = get_category_link($term->term_id); 
     } 
    } 
    else { 
     // Default, display the first category in WP's list of assigned categories 
     $category_display = $category[0]->name; 
     $category_link = get_category_link($category[0]->term_id); 
    } 
    // Display category 
    if (!empty($category_display)){ 
     if ($useCatLink == true && !empty($category_link)){ 
     echo '<span class="post-category">'; 
     echo '<a href="'.$category_link.'">'.htmlspecialchars($category_display).'</a>'; 
     echo '</span>'; 
     } else { 
     echo '<span class="post-category">'.htmlspecialchars($category_display).'</span>'; 
     } 
    } 

} 
?>