How to make unique add_filter to the_content of specific page template files – so each template gets its own addition

I have made a couple of custom page templates files:

page-florida.php

page-texas.php etc.

Now I want to add some php after the_content so that each page template get their own php addition script just after the content.

I am thinking something along the lines of adding to the functions.php – but I am having trouble properly identifying the right page template, for instance “page-florida.php” …

function floridacontent($content) {
 global $post;
  if ($post->page_template == 'florida') {
  $content .= 'Forida added content';
  }
  return $content;
  }
add_filter('the_content', 'floridacontent');

1 Answer
1

You can get the name from the global $template. I don’t know of a way to get it with an API call instead of a global, but such is life.

function floridacontent( $content ){
    global $template;
    $name = basename( $template, '.php' );
    if( 'page-florida' == $name ){
        $content .= 'Forida added content';
    }
    return $content;
}
add_filter( 'the_content', 'floridacontent' );

Leave a Comment