Determine if page is the Posts Page

The question:

On the Reading Settings page you can set a “Front Page” and a “Posts Page”. You can check whether the current page is_front_page();

Is there a similar function for the “Posts Page”. I have noticed that is_page(); does not work for this special page.

Thanks

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

is_home() checks for the “Posts Page”, despite the somewhat confusing function name.

Method 2

WordPress comes with 7 primary template page types, which can be determined on this way

if ( is_main_query() ) {
    // Error
    if ( is_404() ) {
        ;
    }
    // Front page
    if ( is_front_page() ) {
        ;
    }
    // Archive
    if ( is_archive() ) {
        ;
    }
    // Comments popup
    if ( is_comments_popup() ) {
        ;
    }
    // Search
    if ( is_search() ) {
        ;
    }
    // Singular
    if ( is_singular() ) {
        ;
    }
    // Home - the blog page
    if ( is_home() ) {
        ;
    }
}

is_home tells to you, that you have the blog page.

Method 3

“Posts page” is usually an archive of:

  • posts of a category
  • posts of a tag
  • posts of a date ( year, month…)
  • posts of main archive

Each one of these can be checked by a one of the many conditional tags like
is_category()
is_tag()
is_date()
is_archive()

And so many more. To get a better understanding head over to the codex http://codex.wordpress.org/Conditional_Tags

Method 4

First check the blogs related things like author, tag, post type

function is_blog () {
        global  $post;
        $posttype = get_post_type($post );
        return ( ((is_archive()) || (is_author()) || (is_category()) || (is_home()) || (is_single()) || (is_tag())) && ( $posttype == 'post')  ) ? true : false ;
  }

Now check and return something which you want to have

function check_post_type(){
    $postType;
    if (is_blog())
      {
         $postType = 'I am post';
      } else
       {
          $postType = 'I am page';
       };
    return $postType;
  }

Use it like Boss
<?php echo check_post_type();?>

Thanks to Wes Bos

Method 5

https://codex.wordpress.org/Conditional_Tags in WordPress Codex sais that you can reference pages like so:

if ( is_front_page() && is_home() ) {
  // Default homepage
} elseif ( is_front_page() ) {
  // static homepage
} elseif ( is_home() ) {
  // blog page
} else {
  //everything else
}

Method 6

TL;DR

Case A. There is no need to determine it inside the main template file (index.php) because it is the default template for it[1].

Case B. To determine it inside a page template (ex: page.php), simply check it like so:

get_option( 'page_for_posts' ) == get_the_ID()

Details

I literally went digging the source-code[2] of it just to be able to know how wordpress does the checking of the value. It turns out, it is using the statement get_option( 'page_for_posts' ) to know the post ID of the selected value of the Posts page.

So yeah, for this purpose, there is no such official checker function that is similar to is_front_page().

As long as you know the ID of the page that you’ve selected then you can use it for the checking process.

References

  1. WordPress Codex, Theme Development, codex.wordpress.org/Theme_Development

  2. Source-code of SettingsReading Settings, github.com/WordPress/…/wp-admin/options-reading.php


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment