2017-09-19 51 views
0

我可以爲帖子做這個嗎?如果在WordPress中沒有設置特色圖片,如何顯示類別圖片?

我收到了最新的最新帖子,如果它們存在,我會顯示這些帖子的縮略圖(特色圖片)。

我現在要做的是顯示分類圖像,如果特色圖像不存在。

有人可以幫助我嗎?

我都這樣了,此刻:

<?php 

    $args = array('numberposts' => '5'); 
    $recent_posts = wp_get_recent_posts($args); 
    $category = get_the_category(); 
    foreach($recent_posts as $recent){ 
     if($recent['post_status']=="publish"){ 
     if (has_post_thumbnail($recent["ID"])) { 
      echo '<a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . get_the_post_thumbnail($recent["ID"], 'thumbnail').'<div class="browse_category_name"> ' . $recent["post_title"]. '<div> ' . get_the_author_meta('display_name', $recent["post_author"]). '</div></div></a></li> '; 
     } else{ 
      echo '<a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . get_categories_with_images($post->ID,' ,') . $recent["post_title"].'</a></li> '; 
     } 
     } 
    } 
    ?> 

這是我的functions.php:

function get_categories_with_images($post_id,$separator){ 

    //first get all categories of that post 
    $post_categories = wp_get_post_categories($post_id); 
    $cats = array(); 

    foreach($post_categories as $c){ 
     $cat = get_category($c); 
     $cat_data = get_option("category_$c"); 

     //and then i just display my category image if it exists 
     $cat_image = ''; 
     if (isset($cat_data['img'])){ 
      $cat_image = '<img src="'.$cat_data['img'].'">'; 
     } 
     $cats[] = $cat_image . '<a href="'.get_category_link($c) . '">' .$cat->name .'</a>'; 
    } 
    return implode($separator , $cats); 
} 

問題:類圖像不顯示,即使縮略圖/特色圖片沒有設置。

回答

0

在你的函數中,你傳遞了錯誤的變量 - 沒有$post。只要將它改爲$recent["ID"],就像你之前所做的那樣。

+0

謝謝,但它不起作用。它只提取類別名稱,但不提取圖像...所以,這個:get_categories_with_images($ recent [「ID」],',')不起作用。 – Siyah