Check if page opened is home page in wordpress

Sometimes we want to display certain things only on certain category of pages, for example, on this website we wanted to display list of categories only on home page and for all other pages it shouldn’t display anything. To achieve this wordpress provides certain API’s to check whether we are on home page or blog page. Details of this API’s are as below,

is_front_page() – This API returns true if we are on main blog page (Settings->Reading->Your homepage displays is set to “Your latest posts”), or when Settings->Reading->Your homepage displays is set to “A static page”

is_home() – This API returns true if we are on main blog page (Settings->Reading->Your homepage displays is set to “Your latest posts”)

using this two API’s we can check on which page we are using logic something like below,

if (is_front_page() && is_home()) {

	// This page is front page and also recent posts page

} elseif (is_front_page()) {

	// This page is static home page configured using settings

} elseif (is_home()) {

	// This is Blog page listing recent posts 

} else {

	// Some other page like single page or single post page

}
Follow Lynxbee

Leave a Reply / Ask Question