{"id":3373,"date":"2016-03-13T03:21:46","date_gmt":"2016-03-13T03:21:46","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=3373"},"modified":"2016-03-13T03:21:46","modified_gmt":"2016-03-13T03:21:46","slug":"wordpress-add-custom-text-posts-matching-tag","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/wordpress-add-custom-text-posts-matching-tag\/","title":{"rendered":"WordPress: Add custom text to posts matching a tag"},"content":{"rendered":"<p>The example below shows how to add text to the bottom of posts, based on specific conditions.<\/p>\n<p>To get the right value for &#8220;custom_text&#8221;, you likely just want to add a paragraph tag, but you can copy existing HTML from your site to get a good example.<\/p>\n<pre lang=\"php\">\nadd_filter('the_content', 'custom_category_text');\n\nfunction custom_category_text($content){\n  global $post;\n  $custom_text =\n    '<p>Some example text...<\/p>';\n\n  $found = false;\n  $re = \"\/\\btag\\b\/i\";\n\n  $posttags = get_the_tags();\n\n  if ($posttags) {\n    foreach($posttags as $tag) {\n      if ($found || preg_match($re, $tag->name)) {\n        $found = true;\n      }\n\n      continue;\n    }\n  }\n\n  if($found) {\n    $content =  $content . $custom_text;\n  }\n  return $content;\n}\n<\/pre>\n<p>If you want to look for specific words in the post slug, you can also add this:<\/p>\n<pre lang=\"php\">\n  $slug = $post->post_name;\n  if (preg_match($re, $slug)) {\n    $found = true;\n  }\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The example below shows how to add text to the bottom of posts, based on specific conditions. To get the right value for &#8220;custom_text&#8221;, you likely just want to add a paragraph tag, but you can copy existing HTML from your site to get a good example. add_filter(&#8216;the_content&#8217;, &#8216;custom_category_text&#8217;); function custom_category_text($content){ global $post; $custom_text = &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/wordpress-add-custom-text-posts-matching-tag\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;WordPress: Add custom text to posts matching a tag&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[432,601],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3373"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=3373"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3373\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=3373"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=3373"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=3373"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}