Conditions for displaying elements in WordPress

phan tu cua wordpress 1

Working with WordPress source code reveals interesting elements like display conditions. Understanding these conditions is important for customizing how elements are displayed on different pages. For example, the title may display differently on the home page than on a post or page. You may also need to set conditions for elements like banners to only appear on certain pages, like a specific category. One common condition is is_home(), which applies to the page set as the home page. Learning about and using display conditions in WordPress source code is essential for creating a customized website.

When diving into the WordPress source code, you’ll come across many fascinating elements. If you’re someone who loves exploring, then understanding the display conditions in WordPress is crucial. The display conditions can vary, such as having a title display differently on the home page compared to a post or page page. Or maybe you only want a banner to show up in a specific WordPress category and not on other pages. These are the conditions you’ll need to familiarize yourself with.

Some conditions for displaying elements in WordPress source code

is_home()

This condition applies to the page set as the home page.

if ( is_home() ):
echo '<h2>Cảm ơn bạn đã ghé thăm</h2>';
endif;

is_front_page()

Similar to is_home, but it applies the condition to the page whether it’s a static page or not.

if ( is_front_page() ):
echo '<h2> Cảm ơn bạn đã ghé thăm website </h2>' ;
endif ;

is_single() and is_page()

This condition will display only on the post or page page.

if ( is_single () ):
echo 'Bạn đang xem một bài đăng.' ;
endif ;

if ( is_page () ):
echo 'Đây là một trang' ;
endif ;

You can also target a specific page:

if ( is_page ( 'gioi-thieu' ) ):
<h2>Chỉ hiện ở trang giới thiệu</h2>
endif;

is_singular()

This condition displays for Custom Post Types.

See also  Optimizing Apache httpd configuration for maximum performance

is_category() and is_tag()

These conditions will display in categories and tags respectively.

is_archive()

This condition will only display the element on the archive page.

is_tax()

Display conditions for Custom Taxonomy.

is_post_type_archive()

Determines whether the query is for an existing post type archive page, usually for pages that use templates.

Negative Conditional Case in WordPress

Negative conditions exclude pages from being displayed by adding an "!" in front of the conditions. For example, excluding is_homepage and is_front_page from appearing on other pages.

if( !is_home() && !is_front_page()) :
<img class="custom_logo" src="https://wiki.minhduy.vn/<?php echo get_theme_mod("minhduy_logo")?>" alt="Wiki Minh Duy"/>

Understanding these conditions will make it easier for you to customize WordPress. I hope this article has equipped you with some valuable knowledge to enhance your work efficiently.

Rate this post

Related posts