2

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');
  • I don't understant the problem you have with page_template, the template file name is really in this variable. – mmm Dec 16 '17 at 20:18
  • if you are using separate templates and each has separate content, then why not just insert the content in the template itself? – inarilo Dec 16 '17 at 21:25
  • It reaks havoc on the design, so I want to add it as part of the_content to preserve design etc. – Jens Dalsgaard Dec 17 '17 at 0:11
2

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' );

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.