Summary of frequently used conditional branching that you want to remember [WordPress]

. This is Harururu from the PIPELINE WEB production team.
When building a website or developing a theme in WordPress, you can use conditional branching to perform processing according to specific conditions.
For example, you can apply a design that is different from the basic design of your site for a specific category, post, or page.
In this article, I will explain how to write conditional branching in WordPress.
conditional branching using if statements
WordPress conditional forking utilizes if statements.
If the condition specified is true, the if statement performs the corresponding action for that condition.
Here's how to write an if statement:
if (conditional) {
What to do if the conditional expression is true
}
For example, for articles belonging to a specific category, you can use if statements to branch the processing as follows:
if (in_category('news')) {
Fill in what you want to do if the category SLAG is an article belonging to "News"
} else {
Fill in the action to be performed if the category SLAG does not belong to "news"
}
function used for conditional branching
Next, let's discuss the functions you use for conditional branching in WordPress.
The following four functions are commonly used:
- is_home(): A function that determines whether it is a top page or not
- is_front_page(): A function that determines whether it is the front page of a site (top page) or not .
- is_single(): A function that determines whether it is a single post page or not
- is_page(): A function that determines whether it is a fixed page or not
- is_category(): A function that determines whether it is a specific category page or not
These functions allow you to do different things for different types of pages.
Practical example
For example, if you want to switch between what is displayed on the homepage and other pages, you can do so by writing code like this:
<?php if ( is_front_page() || is_home() ) : ?>
Fill in the actions to be performed in the case of the top page
<?php endif; ?>
Also, if you want to switch what you want to display when it is a post page or a fixed page, you can do so with the following code.
<?php if ( is_single() ) : ?>
Fill in what you want to do if it's a post page
<?php elseif ( is_page() ) : ?>
Fill in the actions you want to take if you want to do it for a fixed page
<?php else : ?>
Fill in the actions to be performed if the above conditions do not apply
<?php endif; ?>
That's all about conditional forking in WordPress.
There are various other conditional branches such as the search page and the 404 page, so I would like to introduce them.
Pleasetry it out while actually writing code.