Customize WordPress footers by tag

Using the WordPress APIs, you can add custom footers to posts that key off tags. For instance, you could add links to related books on Amazon based on the topic of a post.

This example will map tags to custom footers, as well as optionally add a random footer for posts without one of the selected tags.

add_filter('the_content', 'custom_category_text');

function custom_category_text($content){
  global $post;

  if(!is_single()) {
    return $content;
  }

  $regexes = array(
    "tag1" => "/\b(tag1|tag_1|tag-1)\b/i",
    "tag2" => "/\b(tag2)\b/i"
  );

  $ads = array(
    "tag1" => '

Content 1

', "tag2" => '

Content 2

', ); $posttags = get_the_tags(); $found = ''; foreach ($regexes as $type => $re) { if ($posttags) { foreach($posttags as $tag) { if ($found or preg_match($re, strtolower($tag->name))) { return $content . $ads[$type]; } } } $slug = $post->post_name; if (preg_match($re, $slug)) { return $content . '' . $ads[$type] . ''; } } $ads = array( '

This is a test 1

', '

This is a test 2

' ); return $content . '' . $ads[array_rand($ads, 1)] . ''; }