Reading). Either way, the conditional tag is_front_page() returns TRUE when the front page is displayed. Accepted parameters This conditional tag does not accept any parameters. 15. Check if the post has a thumbnail: has_post_thum"/> Reading). Either way, the conditional tag is_front_page() returns TRUE when the front page is displayed. Accepted parameters This conditional tag does not accept any parameters. 15. Check if the post has a thumbnail: has_post_thum">

Home  >  Article  >  Web Front-end  >  Exploring Conditional Tags in WordPress: From 14 to 26 – A Comprehensive Guide

Exploring Conditional Tags in WordPress: From 14 to 26 – A Comprehensive Guide

WBOY
WBOYOriginal
2023-09-19 12:17:021269browse

探索 WordPress 中的条件标签:从 14 到 26 - 综合指南

In this series, we’ll cover one of WordPress’s essential features: conditional tags. In Part 3, we'll continue introducing and reviewing conditional tags. If you haven't seen the previous installments, be sure to check them out.

let us start!

14. Check if we are on the front page: is_front_page()

In WordPress, the "Homepage" can be set to a static WordPress page or a list of latest blog posts (Settings>Reading). Either way, the conditional tag is_front_page() returns TRUE when the front page is shown.

Accepted parameters

This conditional tag does not accept any parameters.

15. Check if the post has a thumbnail: has_post_thumbnail()

The "Featured Image" is one of the key parts of the new post/page screen. Conditional tag has_post_thumbnail() Determines whether a featured image is specified for the given post.

Accepted parameters

This conditional tag has only one parameter:

  • $post_id (integer, optional): Post ID. (Default: current post ID)

Usage examples of has_post_thumbnail()

Let's say you are developing a theme where every blog post needs to have a "featured image", so if no featured image is set, you want a "default image" to be displayed. Here's what you have to do:

<?php

function mytheme_featured_image( $class = '' ) {

    global $post;
	
	$post_title = get_the_title();
	
	if ( has_post_thumbnail( $post->ID ) ) {
	
		$featured_image = get_the_post_thumbnail( $post->ID, 'thumbnail', array(
			'alt' => esc_attr( $post_title ),
			'class' => $class
		) );
		
	} else {
	
		$featured_image = '<img src="' . get_stylesheet_directory_uri() . '/assets/default-thumb.jpg" alt="' . esc_attr( $post_title ) . '" class="default-thumb ' . $class . '" />';
		
	}
	
	return $featured_image;
	
}

/*
 * Usage (inside the Loop):
 * echo mytheme_featured_image( 'my-custom-class' );
 */

?>

16. Check if the theme uses "comments popup": is_comments_popup()

You shouldn't judge anyone who uses a theme from 10 years ago or who likes retro. If you are developing a plugin, you need to consider everything, including using a theme with a comment popup. To determine this, you can use the conditional tag is_comments_popup().

Accepted parameters

This conditional tag does not accept any parameters.

17. Check whether the page is a 404 error page: is_404()

"Not Found" error pages are usually pages we hate seeing in websites, so we don't really care how they look. However, when you use these error pages correctly, you can turn them into helpful pages that inform users or help them navigate. The conditional tag is_404() helps us determine whether to display a 404 error to the user.

Accepted parameters

This conditional tag does not accept any parameters.

is_404() Usage example

Suppose you are developing a plugin that logs broken internal links, and you want to run your function every time a 404 error page is viewed. Here's what you have to do:

<?php

add_filter( 'template_redirect', 'my_plugin_check_404_pages' );

function my_plugin_check_404_pages() {

    if ( is_404() ) {
    
        my_plugin_404_logger_function();
    
    }
    
}

?>

18. Check if the given taxonomy exists: taxonomy_exists()

If you need to check if a custom taxonomy is registered, you can use the taxonomy_exists() conditional tag to let your code determine it.

Accepted parameters

This conditional tag has only one parameter:

  • $taxonomy (String, required): The name of the taxonomy. (Default: None)

19. Check whether the page is a "search results" page: is_search()

Although somewhat underrated, the Search Results page is an important part of a WordPress website. If you are developing a plugin or theme, you can detect these pages with the help of the is_search() conditional tag.

Accepted parameters

This conditional tag does not accept any parameters.

is_search() Usage example

Suppose you want to include a Google search link with the same term. Here's what you have to do:

<?php

if ( is_search() ) {

    $search_query = get_search_query();
    
	echo '<div class="google-search"><a href="https://www.google.com.tr/search?q=' . $search_query . '">' . __( 'Search with Google', 'translation-domain' ) . '</a>';

}

?>

20. Check if the page is a "tag archive" page: is_tag()

Want to treat tag files differently? Conditional tag is_tag() can help you. Want to treat a specific tag differently? Just pass the tag name, slug or ID (or an array of them) as parameters!

Accepted parameters

This conditional tag has only one parameter:

  • $tag (array/string, optional): The tag's ID, name, slug, or an array of these. (Default: None)

twenty one. Check if the post has a custom excerpt: has_excerpt()

There are two kinds of "Excerpts" in WordPress posts: it's called a "Custom Excerpt" if you want to write it yourself, and an "Automatic Excerpt" is generated if you don't (by default it's the first 55 of the post characters). has_excerpt() Conditional tag checks if the user has set a custom excerpt for a given post.

Accepted parameters

This conditional tag has only one parameter:

  • $post_id (integer, optional): Post ID. (Default: current post ID)

has_excerpt() Usage example

Let's say you are making a theme and you want a custom excerpt to show in the homepage, but you don't want to show the automatic excerpt. Here's what you have to do:

<?php

if ( has_excerpt() ) {

    the_excerpt();
	
}

?>

22。检查是否是主查询:is_main_query()

WordPress 使用 WP_Query 类来列出帖子 - 无论是帖子标题列表还是存档页面中完整帖子的索引。许多函数使用 WP_Query 类,is_main_query() 就是其中之一。此条件标记检测查询是否不是“辅助查询”,而是“主查询”。

接受的参数

此条件标记不接受任何参数。

23。检查帖子是否具有给定标签:has_tag()

有时,您可能需要检查帖子是否具有某些标签,以使该帖子(或多个帖子)的行为与其他帖子不同。为此,您可以使用 has_tag() 来检查帖子是否带有您指定的标签。 (注意:它允许您指定多个要查找的标签。)

接受的参数

此条件标记有两个参数:

  • $tag (数组/字符串,可选):标签的名称、ID、slug 或这些的数组。 (默认:无)
  • $post (对象,可选):发布以进行检查。 (默认:当前帖子)

has_tag()的使用示例

假设您的博客文章有“徽章”(例如“新”、“精选”和“过时”),这些“徽章”将通过使用相应的标签来激活,并且您想要回显帖子内的图像。这是你要做的:

<?php

if ( has_tag( 'badge-new' ) ) {

    echo '<div class="post-content badge-new">';

} else if ( has_tag( 'badge-featured' ) ) {

    echo '<div class="post-content badge-featured">';

} else if ( has_tag( 'badge-obsolete' ) ) {

    echo '<div class="post-content badge-obsolete">';

} else {

    echo '<div class="post-content">';
    
}

// Post content.

echo '</div>';

?>

24。检查博客是否安装(?):is_blog_installed()

如果安装了 WordPress,此特定条件标记将返回 TRUE。我添加此条件标签仅供参考,因为从技术上讲,它对于插件或主题开发人员来说没有用处,也许可以在某些外部 WordPress 工具中使用。

接受的参数

此条件标记不接受任何参数。

25。检查用户是否为“超级管理员”:is_super_admin()

在多站点网络中,有一个“超级管理员”可以管理所有站点。要检测用户是否是“超级管理员”(或常规 WordPress 安装中的常规管理员),您可以使用 is_super_admin() 条件标签。

接受的参数

此条件标记只有一个参数:

  • $user_id(整数,可选):用户 ID。 (默认:当前用户)

is_super_admin()的使用示例

假设您不喜欢“Howdy”问候语并且想要更改它,但您的用户喜欢它并希望保留它。在这种情况下,您需要一个仅适用于您的解决方案。这是你要做的:

<?php

// Source: http://www.paulund.co.uk/change-the-wordpress-howdy-text

add_filter( 'admin_bar_menu', 'replace_howdy' );

function replace_howdy( $wp_admin_bar ) {

    $my_account = $wp_admin_bar->get_node( 'my-account' );
    
    $newtitle = __( 'Hi boss!', 'translation-domain' );
    
    $wp_admin_bar->add_node( array(
        'id' => 'my-account',
        'title'    => $newtitle
    ) );
    
    return $wp_admin_bar;

}

?>

26。检查页面是否为“Page”页面:is_page()

在 WordPress 中,“页面”是五种内置帖子类型之一,其他类型还有帖子、修订、附件和导航菜单。如果您想检测某个页面(或一般情况下的任何页面),您可以使用条件标签 is_page()

接受的参数

此条件标记只有一个参数:

  • $page (数组/字符串,可选):页面 ID、标题、slug 或其中的数组。 (默认:无)

结论

在这一部分中,我们回顾了 WordPress 中另一批记录的 65 个条件标签。在接下来的部分中,我们将讨论剩下的 39 篇文章。如果您有任何问题或意见,请在下面提出 - 如果您喜欢这篇文章,请不要忘记分享!

下一部分见!

The above is the detailed content of Exploring Conditional Tags in WordPress: From 14 to 26 – A Comprehensive Guide. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn