Add WordPress tags within your post

I was asked a completely random question via a blogging forum yesterday; “how do I integrate WordPress tags within my actual WordPress post“. Why anybody would want to do such a thing is quite beyond me. I can only assume they were fairly junior to WordPress because the purpose of the tags were to emulate breadcrumb style navigation… and that’s simply not how tags work. After a little bit of convincing, I talked him into an alternative presentation. Anyhow, here’s the original idea in code.

If you are after such functionality (for some crazy reason), this code will print tags within your WordPress post after a certain number of defined paragraphs.

<?php
/*
Plugin Name: Tags in Content
Description: Insert tags within a post
Version: 0.1
Author: Marty
*/

add_filter('the_content', 'my_tags');

function my_tags($post_content) {
   global $wp_query, $post;

   if (!is_single() ) return $post_content;

   // After which paragraph to insert code
   $afterParagraph = 3;

   if(get_the_tag_list()) {
   $mytags = get_the_tag_list('Tags: ',' > ','');
   }

   preg_match_all('/(?:\r\n|\r|\n)/', $post_content, $matches, PREG_OFFSET_CAPTURE );
   $insert_at = $matches[0][$afterParagraph][1];

   return substr($post_content,0,$insert_at). $mytags . substr($post_content,$insert_at,strlen($post_content));

}

Simply copy the code into notepad and rename as tag_content.php. Upload and activate as per normal.

The format of the get_the_tag_list function is as follows: (‘What comes before your tags’, ‘Seperator between your tags’, ‘What comes after your tags’). Alter as necessary.

get_the_tag_list('Tags: ',' > ','');

A nice little means of representing your tags as images is as follows:

<?php
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
echo '' . $tag->name . '';
}
}
?>

To do this, you will have to create individual images for each of your tags – in my case, 20 x 20 pixels – and upload to a images subdirectory called tags. After having read about tags I’ve actually learned quite a bit about them.

In a post I’ll write as soon as my schedule allows me, I’ll show you step-by-step how to parse the text in your post and find and replace specific words (normally those that are also tags) and replace them with links. In the case of tags, you can replace them with a link to tag archives. It’s a nice way of providing an alternate means of navigation around your site.

If you liked this article, you may also like:

  1. Adsense Code within Content
  2. Find and Replace Words in a WordPress Post
  3. WordPress Post Missed Schedule
  4. Exclude posts from WordPress blog or RSS feed based on a tag
  5. [Shortcode] Easily post an aviation TAF report into your WordPress post or page with shortcode
About Marty

is a passionate web developer from Sydney, Australia. He owns about 400 websites and makes a healthy living from working the web. As a day job, he works as a pilot for an international airline. You can follow Marty on Twitter or Google+.

Speak Your Mind

*