显示与标签关联的特定图像


display particular image associated with tag

我是wordpress的新手,我试图寻找答案,但我发现和尝试的所有方法都没有奏效。因此,让我们从头开始,我将这段代码添加到函数中.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,属于项目类别。这三个项目中的每一个都分配了以下一个或多个标签:公司 1、公司 2、公司 3。对于每个公司标签,都有一个图像,其中的相同公司标签分配给图像(公司的徽标)。我不仅要显示标签名称,还要显示与标签关联的图像。

有没有办法做到这一点?

提前谢谢。

我使用了我在这里找到的这个代码片段:

<?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;
  }
}
?>

希望有一天能帮助某人。