Check page in WordPress
As we all are familiar with the use of pages in wordpress. Here we are discussing about the detection of pages or a specific page in wordpress. We have a default function is wordpress, that is :
<?php is_page(); ?>
It is a boolean function which returns only TRUE or FALSE. If we are working in a file and wordpress like in our header.php of our current theme. If we want show different text for only pages then we will use this function in below mentioned way :
<?php if( is_page() ){ echo "content to show in pages only"; }else{ echo "content to show other than pages"; } ?>
In this way we can put any code snippet in place of above text.
Check Specific Page :
If in case we want to add a condition for a specific page, like we are having so many pages in our website and we want to add a code for a specific page then we need to follow below mentioned tricks using if else statement like above :
<?php is_page( 18 ); // where 18 is page id is_page( 'About Us' ); // where "About Us" is page title is_page( 'about-us' ); // where "about-us" is page slug ?>
Check Multiple Pages :
In this reference if we need to check more than one page in same condition or in other words we want to add a condition for multiple pages of our choice, then use below mentioned code :
<?php is_page( array( 18, 30, 112 ) ); // where 18, 30, 112 are page id's of multiple pages is_page( array( 'About Us', 'Contact Us', 'Privacy Policy' ) ); // where About Us, Contact Us, Privacy Policy are page titles of multiple pages is_page( array( 'about-us', 'contact-us', 'privacy-policy' ) ); // where about-us, contact-us, privacy-policy are page slug of multiple pages ?>
In this way working with wordpress pages becomes much easy as we are able to detect any page by just using a default wordpress function.
Leave a Reply