Home » Wordpress » WordPress if is_page function not working

WordPress if is_page function not working

Posted by: admin November 30, 2017 Leave a comment

Questions:

I’m trying to add a function to my WordPress theme that will add content before and after each post, but not on any pages. This does not seem to work, but not sure why.

if(!is_page()) {
  function custom_content($content) {
    $content = 'before post content' . $content . 'after post content';
    return $content;
  }
  add_filter('the_content','custom_content');
}

EDIT: Solution I ended up with that does the trick for me, using is_single to only include on single posts. If you want to include on single pages use is_singular

function custom_content($content) {
    if (is_single()) {
        $content = 'before post content' . $content . 'after post content';
 }
    return $content;
}
add_filter ('the_content', 'custom_content', 0);
Answers:

You might be using it inside the loop. is_page() works only outside the loop.

https://codex.wordpress.org/Function_Reference/is_page