Condition Based On Page and Template

Some time we need to customize the page and template based on their type.

Case 1

Condition based on checking whether current page is home or a  blog page

<?php
if( is_front_page() && is_home() ) {
  // This condition will run on if no homepage is selected
}elseif( is_front_page() ) {
  // This condition will run on static homepage is any page is selected as home
} elseif ( is_home() ) {
  // This condition will run only on blog page
} ?>

Case 2

Condition based on Current page is single or not .

<?php
// Run code only Single Page

if ( is_single()  ) {
   
}

Condition for checking single page of specific post type

<?php 

// condition for targeting single by ID , Title
if (is_single('17')){
//target single page of id **17**
}

if (is_single('demo')){
//target single page title **demo**
}



//This Condition will execute on Single **Post** Page only
if ( is_single() && 'post' == get_post_type() ) {
   
}
 
//This Condition will execute on Single of custom post type **portfolio**
if ( is_single() && 'portfolio' = get_post_type() ) {
   
}

//execute on **all** single of except any specific post type for an example i used **portfolio**
if ( is_single() && 'portfolio' != get_post_type() ) {
   
}


?>

1 Comment

  • March 10, 2021

    MilGrasp

    Nice information

Leave A Reply