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

在文档中,它声明您需要传递 get_tag_link() code>一个参数, 是标签ID,但我需要能够在循环 code>期间访问它。 p>

也许我在这里使用了错误的功能。 我正在尝试使用&lt; a&gt; code>标记来包装图像,该标记应该代表标记。 p>

然后,只传递一个变量( $ tag_id code>)不会做的伎俩,但我不确定如何代表 $ tag_id code>来解释循环。 p>

echo code>, article&gt;中查看 HTML code>的开头。 标题 code>您将看到包含 the_post_thumbnail([300,200]) code>的&lt; a&gt; code>。 p>

  if(have_posts()){
 while(have_posts()){
 the_post(); 
 
 echo'
&lt; 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“&gt; 
&lt; header class =”col-12 mb-3 text-center“  &gt; 
&lt; a href =“',get_tag_link($ tag_id),'”&gt;',the_post_thumbnail([300,200]),'&lt; / a&gt; 
&lt; / header&gt; 
 
  &lt; div class =“col-12 mb-3 text-center”&gt; 
&lt; small&gt;',the_category(''),'&lt; / small&gt; 
&lt; / div&gt; 
&lt; div  class =“col-12 mx-auto mb-3”&gt; 
&lt; h2 class =“h2”&gt; 
&lt; a href =“',the_permalink(),'”&gt;',the_title()  ,'&lt; / a&gt; 
&lt; / h2&gt; 
&  lt; / div&gt; 
&lt; div class =“col-12 mx-auto mb-3”&gt; 
&lt; p class =“lead”&gt;',the_excerpt(),'&lt; / p&gt; \  n&lt; / div&gt; 
&lt; / article&gt; 
'; 
} 
} 
  code>  pre> 
  div>

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
}

There are a number of errors in your code.

  1. the_post_thumbnail() will print directly to the page. Since you are echoing you should use get_the_post_thumbnail().
  2. Likewise with the_excerpt() and the_permalink()
  3. the image dimensions in (get_)the_post_thumbnail() should not be in brackets - ie, just (300,200)
  4. to join strings together in PHP use a . not a ,

Finally, to answer your question, use get_the_tags(). If you use it in the loop you don't need to pass $tag_id to it. It will return an array, so you will then need to get the id from the first key and pass that to get_tag_link() to get it's URL, eg:

$theTags = get_the_tags(); $firstTagURL = get_tag_link( $theTags[0]->term_id );

Hope that helps