WordPress has some useful functions in the core that could help any developer to change their code behaviour based on some conditions, like in example, show the author of a post in the Blog page, but not in the single post page.
These functions are called Conditional Tags and in this article you will learn how to use some of the most important and commonly used conditional tags in WordPress.
Home page
The first functions I want to view are those related to the home page.
There are two different functions to check if the page shown is the home or not, and they are based on the kind of home page you are using.
is_home()
– checks if the page shown is the blog main archive. It will returntrue
only if your home page is configured to show the latest blog posts in Settings > General.is_front_page()
– checks if the page shown is the static home page. It will returntrue
only if your home page is configure to show a static page instead of the latest blog posts, in Settings > General.
An example of use:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if ( is_front_page() && is_home() ) { | |
// Default homepage | |
} elseif ( is_front_page() ) { | |
// static homepage | |
} elseif ( is_home() ) { | |
// blog page | |
} else { | |
//everything else | |
} |
Admin
Another useful function used commonly by any developer is is_admin()
. It allows you to check if you are on a back-end page or not.
It’s particularly useful when you have to enqueue a style sheet or a script only on the front-end.
An example of use:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if ( ! is_admin() ) { | |
wp_enqueue_script( 'google-analytics', get_template_directory_uri() . '/my-ga.js' ); | |
} |
Blog & Pages
There are several functions you can use for the blog and static pages. Each of them checks a different thing:
is_single()
– checks if you are on the single post page.is_category()
– checks if you are on a Category archive page.is_tag()
– checks if you are on a Tag archive page.is_page()
– checks if you are on a static page.is_page_template()
– check if you are on a static page that uses a defined template.
For examples of use please check this page on the Codex.
Other conditional tags
Conditional tags are not used only to check if you are on a specific page or kind of pages, they also are used for other things, like in example check if the user is logged in.
The most commonly used conditional tags are:
is_user_logged_in()
– checks if the current user viewing the page is logged in or not.has_post_thumbnail( $post_id )
– checks if the$post_id
post has a featured image or not.is_customize_preview()
– checks if you are previewing the site in the Customizer.
Is that all?
Obviously no, there are many more conditional tags. If you want to learn everything about all of them, check the Codex for detailed descriptions and examples.
Leave a Reply