2016-11-08 91 views
1

我正在使用SF分類縮略圖插件將類別圖像添加到我的類別。獲取類別圖像的src(使用SF分類縮略圖)

我想在一個標題元素的樣式中的背景圖像中使用類別圖像的src。

我可以使用下面顯示的各個類別的相關類別的圖像:

echo get_term_thumbnail($wp_query->get_queried_object_id()); 

這將返回正確的img標籤,但是我需要在src而不是元素。有任何想法嗎?

感謝

回答

1

您需要使用wp_get_attachment_image_src(),它返回一個數組(URL,寬度,高度,is_intermediate),還是假的,如果沒有可用的圖像。

$term_thumbnail = get_term_thumbnail($wp_query->get_queried_object_id()); 

$size = 'medium';// choose the image size to get 

$attachment_img_atts = wp_get_attachment_image_src($term_thumbnail, $size); 

if ($attachment_img_atts) { 
?> 
<img src="<?php echo $attachment_img_atts[0]; ?>" width="<?php echo $attachment_img_atts[1]; ?>" height="<?php echo $attachment_img_atts[2]; ?>" /> 
<?php 
} 

更多細節wp_get_attachment_image_src()

編輯:

wp_get_attachment_image_url()返回的URL,寬度和在參數的附接ID的高度的陣列(見其他是可選的,並且它們的默認值) ,你只需要$ attachment_img_atts [0];在你的情況,並把它放在後臺網址。

function se_40495669($post_id){ 
     $term_thumbnail = get_term_thumbnail($post_id); 
     $size = 'large';// choose the image size to get 
     $attachment_img_atts = wp_get_attachment_image_src($term_thumbnail, $size); 

     if ($attachment_img_atts) { 
       $url = $attachment_img_atts[0]; 
     } 
     return $url; 
    } 

而在你的代碼中使用它:

style="background-image: url(<?php echo se_40495669($wp_query->get_queried_object_id()); ?>)"; 

希望它能幫助!

+1

我希望能夠使用類圖像的,使得式=鏈接 「背景圖像:網址(<??PHP的回聲some_function($ wp_query-> get_queried_object_id())>)」;你的方法似乎沒有返回的網址 – DB1500

0
function expats_term_get_attachment(){ 
    $args = array( 
     'orderby'=> 'name', 
     'order' => 'ASC', 
     'hide_empty' => true, 
     'exclude'=> array(), 
     'exclude_tree' => array(), 
     'include'=> array(), 
     'number' => '', 
     'fields' => 'all', 
     'slug' => '', 
     'parent' => '', 
     'hierarchical' => true, 'child_of' => 0, 
     'childless' => false, 
     'get' => '', 
     'name__like' => '', 
     'description__like' => '', 
     'pad_counts' => false, 
     'offset' => '', 
     'search' => '', 
     'cache_domain' => 'core',); 
} 

    $taxonomy = 'category'; 
    expats_term_get_attachment(); 
    $terms = get_terms($taxonomy, $args); 

    foreach ($terms as $term) { 
     $cat_url = wp_get_attachment_image_url(get_term_thumbnail_id($term->term_taxonomy_id), ''); 
     $cat_slug = $term->slug; 
     $cat_name = $term->name; 
?> 
     <div class="col-md-3"> 
      <div class="cat-image" style="background-url('<?php echo $cat_url; ?>');"> 
       <p><?php echo $cat_name; ?></p> 
      </div> 
     </div> 
<?php } ?>