I'm trying to see if I am on a single page, this page is defined by:

  1. Is not the homepage for sure.
  2. Doesn't have any posts for sure.

Here's my code:

<?php if ( !is_home() && !have_posts() ) : ?>
    <div class="single-page">
<?php endif; ?>

Added to page.php, but it doesn't seem to do anything. I know for a fact that simply adding html code to this file adds it to every single page.

What am I missing?

3 Answers 3


The condition is_home() is right, but the condition have_post() in the template page.php will be true because at least one post of type 'page' exist. A page is already a post.

  • Makes sense! I still don't see how I could tweak my code to only cover not-homepage and not-posts pages. Commented Nov 25, 2017 at 0:33
  • Maybe you can create your custom-template.php and select it in the page that you want from WP admin panel. If you could give me more information about the question I will help you.
    – dperezac
    Commented Nov 25, 2017 at 1:03

Use is_page() to check if you're on a single page, and use ! is_front_page() to exclude the homepage, if it's set to a static page (if it isn't is_page() will be false).

<?php if ( is_page() && ! is_front_page() ) : ?>
    <div class="single-page">
<?php endif; ?>

Well the answer given by @Jacob Peattie, is fair enough. But if you only need to add some classes to the DOM for CSS styling then you may follow other procedures too. Like WordPress by default add home class to home page and single-post class to single post. As well as for other post types single-{post_type}. Have a look here. You'll have an overview. So you can follow nested CSS procedure to style your things as you like.

  • Why down vote ? Who ever down voted this please explain when you are down voting.
    – CodeMascot
    Commented Nov 25, 2017 at 9:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.