How to check whether it is POST or PAGE in WordPress


In WordPress, is_page() function return true if the current article is a “PAGE” and similarly is_single() function return true if the current article is “POST”.

We can also use get_post_type() function to identify whether a current article is a “PAGE” or “POST”.

1. Page

1.1 is_page() function return true if the current article type is “PAGE” otherwise return false.

<?php
    
  if(is_page()) {
       
    echo "You are viewing a PAGE.";
  }    
  
?>

1.2 get_post_type() function return 'page' (as string) if the current article type is “PAGE”.

<?php
    
  if(get_post_type() === 'page') {
       
    echo "You are still viewing a PAGE.";
  }    
  
?>

1.3 We can also use get_post() function to check whether it is a “PAGE” or “POST”.

<?php
    
  if(get_post()->post_type === 'page') {
       
    echo "Ahh, you are still viewing a PAGE.";
  }    
  
?>

2. Post

2.1 Similarly is_single() function return true if the current article type is “POST” else return false.

<?php
    
  if(is_single()) {
       
    echo "You are viewing a POST.";
  }    
  
?>

2.2 get_post_type() function return 'post' (as string) if the current article type is “POST”.

<?php
    
  if(get_post_type() === 'post') {
       
    echo "You are still viewing a POST.";
  }    
  
?>

2.3 And get_post() function to check whether it is a “PAGE” or “POST”.

<?php
    
  if(get_post()->post_type === 'post') {
       
    echo "Ahh, you are still viewing a POST.";
  }    
  
?>

References

  1. Top 10 Latest Posts in WordPress
  2. How to display related Posts in WordPress
  3. is_page() – WordPress
  4. is_single() – WordPress
  5. get_post_type() – WordPress

Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.