doushenyi9104 2018-01-31 09:58
浏览 70
已采纳

WordPress:get_tag_link()返回空字符串

In the docs it states that you need to pass get_tag_link() an argument, which is the tags ID, but I need to be able to access this during the loop.

Maybe I'm using the wrong function here. I'm trying to wrap an image with an <a> tag that is supposed to represent the tag.

And yea, only passing it a variable ($tag_id) isn't going to do the trick but I'm not sure how I should represent the $tag_id to account for the loop.

Look in the start of the HTML from the echo, article > header you will see an <a> that is wrapping the the_post_thumbnail([300, 200]).

if (have_posts()) {
          while (have_posts()) {
            the_post();

            echo '
                  <article class="blog-post col-12 col-sm-8 offset-sm-2 col-md-6 offset-md-0 col-lg-4 mb-5 mb-md-0">
                    <header class="col-12 mb-3 text-center">
                      <a href="',get_tag_link($tag_id),'">',the_post_thumbnail([300, 200]),'</a>
                    </header>

                    <div class="col-12 mb-3 text-center">
                      <small>',the_category(' '),'</small>
                    </div>
                    <div class="col-12 mx-auto mb-3">
                      <h2 class="h2">
                        <a href="',the_permalink(),'">',the_title(),'</a>
                      </h2>
                    </div>
                    <div class="col-12 mx-auto mb-3">
                      <p class="lead">',the_excerpt(),'</p>
                    </div>
                  </article>
            ';
          }
        }
  • 写回答

2条回答 默认 最新

  • dongwo1234 2018-01-31 10:17
    关注

    First you need to get the post's tags and then pick one (as it can have multiple tags) or just use the first one. This will throw an error if there are no tags or php version is 5.2 (see below for a safer code).

    $tag_id = get_the_tags()[0]->term_id;
    

    Here is a safer code:

    $tags = get_the_tags();
    if(!empty($tags)){
        $tag_id = $tags[0]->term_id
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab实现基于主成分变换的图像融合。
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊