Perhaps the most prominent feature of the WordPress content management system is that it contains many features that make it easier for developers to write and implement codes. In today’s article, I will explain one of these important features that any template developer should learn, which is conditional tags.
Conditional tags are simple, ready-to-use functions that enable us to execute code when certain conditions are met on the site. Using these functions saves developers a lot of time while developing their themes or even custom plugins.
Therefore, we will devote today’s article to explaining in detail the concept of conditional tags, and we will review a list of the most important of these tags in WordPress and their meanings. We will also explain how they work by presenting a set of practical examples that use conditional tags for different purposes.
What are Conditional Tags in WordPress?
First, before I begin explaining conditional tags in WordPress, I would like to point out that conditional logic is an important feature available in most programming languages, including PHP , which mainly uses the if statement to write conditions, through which we can set certain conditions in the code and verify that these conditions are met. Conditions before implementing any instructions.
For example, you may need to verify whether the user who is currently browsing the site has Admin or Editor privileges so that he will be exposed to a direct link to edit the article when he browses it, or you may want to verify that the visitor is viewing the site’s home page in order to display a scroll bar on this page. page, or display a personalized welcome message, or other scenarios.
Here comes the role of conditional tags in WordPress, which help you verify these and other conditions very easily through ready-made functions through which we can test any conditions you want to verify before implementing specific code on our site in a flexible and easy-to-use manner.
Conditional tags in WordPress are ready-made functions pre-written in PHP that return a logical value (either true or false) and are usually used in WordPress templates to check certain conditions (such as post type, post status , user authority , today’s date, or other conditions.. ) before executing the code for this template.
Although you can call conditional tags from anywhere in WordPress (whether themes, plugins, or anywhere you need to make conditional decisions to customize your site), in today’s article we will focus on using them in developing and designing WordPress themes.
Conditional tags are called as follows:
For example, to execute a specific code when you are on any static page on your site, the code will be as follows:
The most popular conditional tags in WordPress
The WordPress system has more than 50 conditional tags built into it, and it is worth noting that most of the conditional tags in WordPress have the prefix is_ followed by a name that expresses how they work, although there are some conditional tags that have the prefix has_ and some do not have any prefix before the name.
Of course, in this paragraph we will not display all of these tags, but we will suffice with explaining the most important of these tags and their meanings, and you can see for yourself a list of all conditional tags in the WordPress system through the following reference or the following reference .
- is_single() : This tag returns true if the page currently being displayed consists of a single post of any type (except page and attachment types).
- is_page() : This tag returns true when displaying a static page on the site.
- is_attachment(): This tag returns true when displaying any uploaded document or attached file within an article or page.
- is_singular() : Similar to the is_single() tag, but it returns the value true when displaying any post, regardless of its type. It is equivalent to using the previous three tags together as follows:
- is_search() : This tag returns true when displaying a search results page on the site.
- is_archive() : This tag returns an integer value if the current page displays an archive (whether for a tag, category, author, date…)
- is_home(): This tag returns true when your site’s home page is a blog page. As you know, you can set up a WordPress website to display either blog articles or a static page on the home page.
Therefore, in this case, if your site is set up to display blog articles on the home page, the tag will return true in the site’s blog directory (domain.com/blog) and will return false in the site’s main directory (domain.com).
Example:
- is_front_page(): This tag returns true if you are on the static home page of your site, or if you are on your blog page when you are not using a static page as the home page. That is, this tag searches for the page that is set to be the home page of your site, regardless of whether it is a static page or not. Its value is always true on the domain.com directory, regardless of the page displayed.
- is_main_query(): This tag returns true if the current query is the main query of the page.
- is_404(): This tag returns an integer value when a 404 error occurs on the site and moves to the page designated for display when this error occurs.
- is_author() : This tag returns true if we are viewing the author’s page on the site.
- is_admin (): This tag returns true when you are in the backend of your site, that is, in the admin control panel of your WordPress site. Be careful here that the user here does not necessarily have admin permission, but rather it is important that he be within the site’s control panel, even if he has the permission of another user (the tag current_user_can (‘administrator’) is the one who verifies that the user has admin permission)
- is_dynamic_sidebar(): Returns true if any of the theme’s sidebars or widget display areas have active widgets.
- has_excerpt(): returns true if the current article has an excerpt
- has_tag() : returns true if the current article has tags
- is_sticky() : Returns true if a post is defined as a sticky post.
- is_category(): Determines whether we are executing a query on an archive page that has a specific category.
You can also combine more than one condition tag by using logical operators such as the && operator , which executes the instruction when all conditions are true (its value is true), or the || Which executes the instructions when at least one of the conditions is met, or the condition is negated ! Which executes the instructions when the condition is not met.
For example: To execute a certain code when we are on the archive page, which has the WooCommerce label, we write the following code:
Using conditional tags with parameters
There are many conditional tag functions that can accept parameters through which you can customize the conditions and specify them more precisely. For example, to verify that the page we are currently displaying is a Contact Us page and customize it differently from the rest of the other pages on the site, we can write the following code:
To write a condition to verify the ID of a specific publication, we write the following code:
To write a specific code that is executed only on the archive page of the writer who has a specific name, let it be olasaleh, we write the following code:
It is also possible to pass more than one parameter for some conditional tags within an array. For example, to verify that the page we are displaying meets the condition that the page ID is 10 or the page name is About Us, we pass the parameters as follows:
Practical examples of using conditional tags in developing a WordPress theme
Example 1: Using conditional tags as an alternative to the hierarchy of template files
You can use WordPress conditional tags as an alternative to the WordPress theme file hierarchy and make decisions similar to the behavior of the theme hierarchy.
For example, the entire template display code can be written in the template’s main template file (index.php) only – although I personally do not prefer this method – but it is used in developing some WordPress templates, and the general form of the code within the file in this case is as follows:
In this way, we can rely on the basic template file within the WordPress template files list . For example, in this file, all articles and pages can be displayed identically, with only a slight difference, which is displaying the publication date if we are article pages, and not displaying the date if we are in pages. fixed, then we write the following code:
Example 2: Using conditional tags to load layout files on specific pages
One of the essential uses in which conditional tags are also useful to us is when we want to apply a specific design or effects to a specific page on the site.
For example, let’s have a page called wparpage, and we want to include custom CSS files or JavaScript files in the queue for this page only instead of loading them throughout the site and slowing down its speed unnecessarily.
To do this, we write the following code in the template functions file functions.php :
In this code we include the CSS and JavaScripr files that we want to include in the WordPress queue by defining a callback function named wpar_special_page( ) and calling the wp_enqueue_script and wp_enqueue_style functions within it.
We then linked this function to the wp_enqueue_scripts action hook, but as you notice, we preceded the commands to add files to the queue with a conditional tag that verifies that the current page on which we want to include these files is the requested page.
If it is not the requested page, the function will return and the files will not be downloaded. Otherwise, execution will continue and our formatting files and scripts will be loaded on this page only.
Read more:
Learn how to properly embed CSS and JavaScript files into a WordPress theme
Explaining the hooks in WordPress and their importance for template and plugin developers
Conclusion
In today’s article, we learned about the concept of conditional tags, which are considered one of the basic concepts for developing WordPress templates. We explained that they are special functions written in the PHP language that return a logical value (true or false) and are mainly used to customize the way site content is displayed when specific conditions are met (such as page type or User authority or whether the user is logged in or not or many other conditions..)
We also learned about the possibility of combining more than one condition and passing specific parameters to conditional tags to make the conditions more customized, and we demonstrated their relationship to the WordPress template hierarchy. We also reviewed a group of practical examples that illustrate how to use them.
You will rarely find a WordPress template that does not use conditional tags in its code, and you should also start using them in your template now and benefit from their high flexibility. If you have any questions about one of the conditional tags in WordPress or how to use it, you can write your question in the comments below the article for the WordPress team to answer .
Leave a Reply