2017-04-25 86 views
2

我使用高級自定義字段返回類別圖像的URL位置。這將允許我在帖子標題之前添加類別圖片。我在我的functions.php文件中有一個函數insert_image_before_title,它被標題過濾器引用(見下文)。所以這似乎適用於我的主要博客頁面或帖子的詳細信息頁面上的帖子。由於該類別的正確圖像出現在帖子的標題前面。然而,它不適用於小部件。例如,「最新評論」或「最近發佈的帖子」小部件將在標題前顯示相同的類別圖像,即使類別圖像可能不是與該帖子關聯的正確圖像(它看起來使用它遇到的第一個類別圖像對於特定的部件並顯示在前端的部件相同的圖像,如果任何類別的圖像是聯繫在一起的文章。Wordpress - 高級自定義字段 - 類別圖像錯誤的小工具

function insert_image_before_title($title, $id = null) 
{ 
    // load all 'category' terms for the post 

    $terms = get_the_terms(get_the_ID(), 'category'); 

    // we will use the first term to load ACF data from 

    if(!empty($terms)) 
    {   
     $term = array_pop($terms); 

     $category_icon_url = get_field('category_icon', $term); 

     // do something with $custom_field 

     if($category_icon_url) 
     {   
      $title = '<img src="'. $category_icon_url .'" />' . $title; 
     } 

    } 

    return $title; 
} 
add_filter('the_title', 'insert_image_before_title', 10, 2); 

回答

1
Try this: 
<?php 
$args = array(
    'taxonomy' => 'post_tag', 
    'hide_empty' => true, 
); 
$terms = get_terms($args); 
foreach ($terms as $term): 
$thumbnail = get_field('field_name', $term->taxonomy . '_' . $term->term_id); 
?> 
<!--If an array is returned in ACF--> 
<img src="<?php echo $thumbnail['url'];?>" alt="<?php echo $thumbnail['title'];?>"> 
<!--If an url is returned in ACF--> 
<img src="<?php echo $thumbnail;?>" alt="#"> 
<?php endforeach; ?> 
+0

感謝您的答覆。我是新來的WordPress所以我不t知道這件事很多,所以你能告訴我哪裏(哪個文件)我會把你的代碼放在上面嗎? – truk

+0

給我一個鏈接,你想這樣做 –

+0

如果你拿這個頁面http:// bassandbrands .COM /博客/聲音 - 檢查 - jamaica- chronixx /例如,如果您轉到右側側邊欄並向下滾動到「最新評論」窗口小部件。所有標題在標題名稱前都有一個耳機圖標作爲類別圖像。當只有第一個應該有耳機作爲分類圖像圖標,其餘的應該有一個錄製圖標。所以有些東西被搞砸了 – truk

相關問題