The following very simple snippet of code is not working right: the tags are being displayed in the page, but outside of the span with class "tags".

echo '<span class="tags">' . the_tags('See also: ', ' &middot; ') . '</span>'; 

Here's the output:

See also: 
<a href="#" rel="tag">animals</a> · 
<a href="#" rel="tag">communities</a> · 
<a href="#" rel="tag">cultural differences</a> · 
<a href="#" rel="tag">projects</a>
<span class="tags"></span>

the_tags() doesn't return the links but immediately echoes them. As that happens before the echo they appear before the span.

What you want is this:

echo '<span class="tags">';
the_tags('See also: ', ' &middot; ');
echo '</span>';

or even simpler

the_tags( '<span class="tags"> See also: ',' &middot; ', '</span>' );

As a general rule of thumb you can remember that function that start with the_ directly echo things, while functions starting with get_ return stuff.

So here you could also be using this:

echo '<span class="tags">' . get_the_tag_list('See also: ', ' &middot; ') . '</span>';
  • Fantastic, @kraftner! Thank you for the detailed answer.
    – Bankitalia
    Sep 30 '16 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.