0

I'm in the process of building my own Wordpress theme from scratch and my knowledge of PHP is limited. What I'm trying to do is insert tags below each post. I can do this by inserting code into my single.php file but I want to do this through the functions.php file.

I have already inserted an author box below each post through the functions.php file. I got the code for that somewhere online and I would like to do the same thing with the tags.

Here is the author box code in my functions.php file.

function get_author_bio($content='') {
    global $post;
    $post_author_name=get_the_author_meta("display_name");
    $post_author_description=get_the_author_meta("description");
    $html="<div class='author-box'>\n";
    $html.="<h5 class='author'>About the author</h5>\n";
    $html.="<div class='author-entry'>\n";
    $html.="<img width='100' height='100' class='avatar' src='http://www.gravatar.com/avatar.php?gravatar_id=".md5(get_the_author_email()). "&default=".urlencode

($GLOBALS['defaultgravatar'])."&size=80&r=PG' alt='PG'/>\n";
    $html.="<h6 class='author-name'>".$post_author_name."</h6>\n";
    $html.= "<p>".$post_author_description."</p>\n";
    $html.="</div>\n";
    $html.="</div>\n";
    $content .= $html;
    return $content;
}
add_filter('the_content', 'get_author_bio');

It works great and it allows me to add HTML elements for easy CSS styling. I want something similar for the tags. Cab anyone help me?

  • You already worked out the basics. What exactly is giving you trouble with tags? – s_ha_dum May 18 '14 at 20:59
  • The problem is that I do not know how to insert tags via the functions.php file. – user1181153 May 18 '14 at 21:01
  • Well, yes, but that isn't much detail. Have you tried the Codex? – s_ha_dum May 18 '14 at 21:04
  • I have tried the codex but it's confusing. What I'm trying to do is insert tags below each of the posts. I can do it by inserting <?php the_tags(); ?> into the single.php file but I need to have it in the functions.php file. – user1181153 May 18 '14 at 21:09
  • I have written a simple case answer for you. See below. – s_ha_dum May 18 '14 at 21:18
0

If all you want to do is add tags to the post body without writing them into the theme templates, all you really need to do is:

add_filter(
  'the_content',
  function($content) {
    return $content.get_the_tag_list();
  }
);

See get_the_tag_list() for arguments. That, by the way, is basically what the the_tags() function does.

However, there are other ways to do it and those ways may be better depending upon you exact requirements. As written, your question is pretty light on detail.

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.