2

I'm converting an HTML template into a wordpress theme and I'm having some trouble displaying the post tags within the functions.php file. What I would like to do is add the following code into the functions file with the HTML code. I have been trying for a few days now and I have already been to the codex but nothing I try is working.

<div class="tags">
<a href="#" rel="tag">tag 1</a> <a href="#" rel="tag">tag 2</a> <a href="#" rel="tag">tag 3</a> <a href="#" rel="tag">tag 4</a>
</div>

Can anyone please help with this?

  • Sorry, but you are not providing enough informations to give an clear answer. The functions.php file is supposed to include custom theme helper functions and no direct html output. Where do you exactly want the output to happen? You want to output a list of assigned tags on a singles post view? – s1lv3r Jun 30 '14 at 20:15
  • 1
    Sorry, yes, I want the output to be in the single post. The tags you assign to each a post. – user1181153 Jun 30 '14 at 20:21
  • Through the functions file? Or through any file? – Brad Dalton Aug 7 '14 at 13:07
1

Here's one way to add post tags after the content on single posts only using the_content filter in a custom function from your functions file. Uses the_tags

function tags_after_single_post_content($content) {

if( is_singular('post') && is_main_query() ) {

$tags = the_tags('<div class="entry-meta">Tagged with: ',' • ','</div><br />'); 

$content .= $content . $tags;
    }
return $content;
}
add_filter( 'the_content', 'tags_after_single_post_content' );

Result:

enter image description here

0

Correct code:

function tags_after_single_post_content($content) {
  $posttags = get_the_tags();
  if ($posttags) {
    $array = [];
    foreach($posttags as $tag) {
      $array[] = '<a href="/tag/' . $tag->slug . '/">' . $tag->name . '</a>';
    }
    $content .= 'Tags: ' . implode(', ', $array) . '<br>';
  }

  return $content;
}
add_filter( 'the_content', 'tags_after_single_post_content' );

Reason this answer is wrong because the_tags is should be used inside The Loop and the_tags return nothing so other code is doing nothing. In this answer get_the_tags return array of tag instances so we can append them to content.

  • Hi there, welcome to WPSE. Could you please edit your post to explain why this is the correct code? Code only answers are discouraged here as they don't teach anything. Thank you! – Tim Malone Jul 15 '16 at 4:50
-1

I think you are searching for the get_tags() function. You would need to place it into the single-post.php (or single.php if your theme doesn't have a single-post.php) (to find the right template file you can always look up at Wordpress Template Hierarchy).

To echo a list of tags with the aboved linked function you would need to use something like:

<?php $tags = get_tags(); ?>
<div class="tags">
<?php foreach ( $tags as $tag ) { ?>
    <a href="<?php echo get_tag_link( $tag->term_id ); ?> " rel="tag"><?php echo $tag->name; ?></a>
<?php } ?>
</div>
  • Glad to help. Please accept the answer if you think it was helpfull. Have a nice evening. :-) – s1lv3r Jun 30 '14 at 20:54
  • Doesn't work from the functions file. – Brad Dalton Aug 7 '14 at 13:11
-1

Displays a list of tags with links to each one and a specific class for each tag:

$tags = get_tags();
$html = '<div class="post_tags">';
    foreach ( $tags as $tag ) {
       $tag_link = get_tag_link( $tag->term_id );

       $html .= "<a href='{$tag_link}' title='{$tag->name} Tag' class='{$tag->slug}'>";
       $html .= "{$tag->name}</a>";
    }
$html .= '</div>';
echo $html;
  • Link to the source of your code please. – Brad Dalton Aug 7 '14 at 13:10

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.