Detecting if I'm on a single, non-post, non-homepage page?

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?

Topic page-specific-settings pages Wordpress

Category Web


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.


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; ?>

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.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.