【问题标题】:display particular image associated with tag显示与标签关联的特定图像
【发布时间】:2016-03-03 02:08:30
【问题描述】:

我是 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 个帖子:project1、project2 和 project3,它们属于 Projects 类别。这三个项目中的每一个都分配了一个或多个这些标签:company1、company2、company3。对于每个公司标签,都有一个图像,该图像具有分配给图像(公司徽标)的相同公司标签。我不仅要显示标签名称,还要显示与标签关联的图像。

有没有办法做到这一点?

提前致谢。

【问题讨论】:

    标签: php wordpress taxonomy


    【解决方案1】:

    我使用了我找到的代码 sn-p 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;
      }
    }
    ?>
    

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

    【讨论】:

      猜你喜欢
      • 2015-11-02
      • 1970-01-01
      • 2015-03-16
      • 2021-10-04
      • 1970-01-01
      • 1970-01-01
      • 2021-08-02
      • 1970-01-01
      • 2017-07-23
      相关资源
      最近更新 更多