2016-03-01 43 views
0

我是新的wordpress,我試圖尋找答案,但是我發現並嘗試的所有內容都無效。因此,讓我們從我加入這個代碼在我的孩子主題的functions.php開始啓動:顯示與標記相關的特定圖像

function wptp_add_tags_to_attachments() {   
    register_taxonomy_for_object_type('post_tag', 'attachment'); 
} 
add_action('init' , 'wptp_add_tags_to_attachments'); 

現在我可以在管理部分添加標籤添加到圖像。 問題是我想在帖子和類別頁面上顯示這些圖像,旁邊是分配給帖子底部的帖子,默認情況下它們出現在該帖子中。我將該類別添加到主導航欄中,因此在點擊該類別後,將顯示包含該類別中所有帖子摘錄的頁面。在底部顯示與給定帖子關聯的標籤。

一個示例原因我不確定我是否解釋過,以便於理解。

我有3個職位:項目1,項目2和項目3在類別項目。這三個項目中的每一個都分配了一個或多個這些標籤:company1,company2,company3。對於每個公司標籤,都有一個圖像具有相同的公司標籤分配給圖像(公司的標識)。我不僅要顯示標籤名稱,還要顯示與標籤相關的圖像。

有沒有辦法做到這一點?

在此先感謝。

回答

0

我用這個代碼片段,我發現here

<?php 
    $posttags = get_the_tags(); 
    if ($posttags) { 
    foreach($posttags as $tag) { 
     echo '<img src="http://example.com/images/' . $tag->term_id . '.jpg" 
      alt="' . $tag->name . '" />'; 
    } 
    } 
?> 

所以最終的產品是這樣的:

<?php 
$posttags = get_the_tags(); 
$templPath = get_stylesheet_directory_uri() .'/images/'; 
if ($posttags) { 
    foreach($posttags as $tag) { 
    $html = '<div class="projectLinkWrap">'; 
    $html .= '<a href="'. get_the_permalink() .'#projectpartner"/>'; 
    $html .= '<div class="thumbLogoWrapper">'; 
    $html .= '<img src="'.$templPath . $tag->name.'.png" 
    alt="' . $tag->name . '" /></div>'; 
    $html .= '<span class="tagName">'. $tag->name .'</span></a></div>'; 
    echo $html; 
    } 
} 
?> 

希望它可以幫助別人一天。