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

display particular image associated with tag

我是 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' );

现在我可以在管理部分给图片添加标签了。 问题是我想在分配给 post 的标签旁边的 post 和类别页面上显示这些图像,它们默认显示在底部。我将类别添加到主导航中,因此在我单击它后会显示一个页面,其中包含该类别中的所有 post 摘录。底部显示与给定 post.

关联的标签

一个例子,因为我不确定我是否解释过它以便于理解。

我有 3 个 post:项目 1、项目 2 和项目 3,它们都属于项目类别。这三个项目中的每一个都分配了一个或多个这些标签:company1、company2、company3。对于每个公司标签,都有一个图像,该图像具有分配给图像(公司徽标)的相同公司标签。我不仅要显示标签名称,还要显示与标签关联的图像。

有什么办法吗?

提前致谢。

我使用了我发现的这个代码片段 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;
  }
}
?>

希望有一天能对某人有所帮助。