WordPress的最佳功能之一可能是条件标记 。 它使您可以告诉代码在特定情况下采取不同的行动 。 例如,您可以检查用户是否使用Windows或Mac,并根据系统显示不同的内容。 如果搜索查询仅返回单个结果,您还可以重定向到发布。 你说的情况; 条件标签可以识别所有标签!
尽管可以灵活地根据不同的情况确定操作,但学习起来也很简单,甚至网上都有许多教程和资源可供您掌握。 也就是说,在本文中,我们将详细介绍条件标签,它们如何工作以及何时使用它们。
在文章的最后一部分,我们还将显示十个有用的代码片段,这些代码片段可帮助您最大程度地利用条件标签,因此请充分利用它们,以使WordPress网站在特殊情况下更智能地运行!
如果(声明)
使用PHP if语句,您可以询问是true还是false或1或0 。 如果您的声明为true,则将执行您的代码;如果为false,则什么都不会发生,这取决于您决定条件标签中的操作的方式。 查看示例,我相信您会理解我在说什么。
<?php
if(10 == 10):
echo 'This is true, and will be shown.';
endif;
if(10 == 15):
echo 'This is false, and will not be shown.';
endif;
?>
您还可以使用elseif
,它可以添加另一个语句, else
,如果第一个语句为false,则将执行该语句。
<?php
if(10 == 11):
echo 'This is false, and will not be shown.';
elseif(10 == 15):
echo 'This is false, and will not be shown.';
else:
echo 'Since none of the above is true, this will be shown.';
endif;
?>
这就是您现在需要了解的if语句的全部内容,让我们进入WordPress条件标签! 但是,如果您想更深入地研究PHP if语句,请转到php.net以供参考。
条件标签如何工作?
使用is_home()
类的本机WordPress函数时,您只需询问WordPress用户当前是否在主页上。 然后,WordPress将回答0表示否,1表示是。
<?php
if( is_home() ):
echo 'User is on the homepage.';
else:
echo 'User is not on the homepage';
endif;
?>
有关WordPress条件标签的完整列表,您可以访问其法典 。
合并语句
在某些情况下,您可能要检查多个语句 。 使用AND
和OR
可以很容易地做到这一点。
<?php
if( is_home() AND is_page( 5 ) ):
echo 'User is on the homepage, and the home pages ID is 5';
endif;
if( is_home() OR is_page( 5 )):
echo 'User is on the homepage or the page where the ID is 5';
endif;
?>
何时使用条件标签?
当您想根据与您网站相关的问题的答案来更改内容时,条件标签非常有用。 用户登录了吗? 她在使用Internet Explorer吗? 有没有要显示的帖子?
要获得使用条件标签的示例,我们可以查看二十一十一(WP 3.2中的标准主题) index.php ,第20行。
<?php if ( have_posts() ) : ?>
... posts ...
<?php else : ?>
... search field ...
<?php endif; ?>
这将检查是否有要显示的帖子,如果答案为否,则会显示搜索字段。
以下是WordPress条件标签的另一个示例:
if( is_admin() ):
# User is administator
endif;
if( is_home() AND is_page('1') ):
# The user is at the home page and the home page is a page with the ID 1
endif;
if( is_single() OR is_page() ):
# The user is reading a post or a page
endif;
if( !is_home() AND is_page() ):
# The user is on a page, but not the homepage
endif;
10个有用的条件标签
WordPress Codex页面中可用的条件标签相当限于WordPress的大部分,例如帖子,页面等。 但是,如果您环顾网上,有许多小而有用的声明。
检查用户是否登录
如果您的博客注册了用户,这将是一个方便的摘录,因为它会检查您的用户是否已登录。
if ( is_user_logged_in() ):
echo 'Welcome, registered user!';
else:
echo 'Welcome, visitor!';
endif;
打开或关闭注册时显示内容
如果您的站点中具有用户注册功能,并且希望让访问者知道注册是打开还是关闭,则为一个很好的摘要。
<?php if ( get_option('users_can_register'):
echo 'Registrations are opened.';
else:
echo 'Registrations are closed.';
endif;
检查用户是否在Mac或PC上
是否要根据用户使用的操作系统提供特定的内容? 这是您的摘录。
if( stristr($_SERVER['HTTP_USER_AGENT'],"mac") ):
echo 'Hello, I'm a Mac.';
else:
echo 'And I'm a PC.';
endif;
禁用已登录用户的Google Analytics(分析)
如果您使用的是Google Analytics(分析),而您只想跟踪作者以外的访问者,则可以使用此代码段来达到目的。 确保将UA-XXXXXXX-X更改为您的Google Analytics(分析)ID 。
<?php
// function for inserting Google Analytics into the wp_head
add_action('wp_footer', 'ga');
function ga() {
if ( !is_user_logged_in() ): //If user is not logged in
?>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXX-X']); // insert your Google Analytics id here
_gaq.push(['_trackPageview']);
_gaq.push(['_trackPageLoadTime']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<?php
endif;
}
?>
检查帖子是否为自定义帖子类型
使用下面的条件标签,您可以检查当前帖子是否为特定的自定义帖子类型,例如books 。
<?php if ( is_singular( 'books' ) ):
// This post is of the custom post type books.
endif; ?>
如果搜索查询仅返回单个结果,则重定向到发布
将此片段添加到WordPress Themes的functions.php中 ,以在WordPress仅返回单个搜索结果时将搜索重定向到自动发布。
<?php
add_action('template_redirect', 'single_result');
function single_result() {
if (is_search()) {
global $wp_query;
if ($wp_query->post_count == 1) {
wp_redirect( get_permalink( $wp_query->posts['0']->ID ) );
}
}
}
?>
检查是否有最新帖子
如果您在帖子之间使用分隔符,则可能不希望将其包含在页面的最后一个帖子中。 在您的循环中包含以下条件标记,您只希望在最后一个帖子上显示某些内容。
<?php if( ($wp_query->current_post + 1) < ($wp_query->post_count) ) { ?>
<div class="separator"></div>
<?php } ?>
检查当前用户是否可以…
有时您想了解用户的角色,例如,您只希望为作者显示某些链接(编辑等)。 函数current_user_can()的工作原理与上面提到的一样,下面是代码:
<?php
if( current_user_can('editor') ):
// true if user is an editor
endif;
if( !current_user_can('administrator') ):
// true if user is not admin
endif;
if( current_user_can('edit_posts') ):
// true if user can edit posts
endif;
?>
除管理员外,对所有人禁用Tinymce HTML编辑器
是否曾经想对除管理员以外的所有人禁用Tinymce HTML编辑器 ? 这是您的摘录。
<?php
add_filter( 'wp_default_editor', create_function('', 'return "tinymce";') );
add_action( 'admin_head', 'disable_html_editor_wps' );
function disable_html_editor_wps() {
global $current_user;
get_currentuserinfo();
if ($current_user->user_level != 10) {
echo <style type="text/css">#editor-toolbar #edButtonHTML, #quicktags {display: none;}</style>;
}
}s
?>
检查用户是否来自StumbleUpon
StumbleUpon是一个很棒的社交媒体,能够吸引您网站的访问量。 这是吸引Stumblers的一个技巧:使用下面的条件标记显示一条特殊的消息来欢迎他们,以检查用户是否来自StumbleUpon。
<?php if( strpos($_SERVER[HTTP_REFERER], "stumbleupon.com" ) == true ): ?>
<div class="welcome-stumbleupon">
<p>Hello StumbleUpon user!</p>
</div>
<?php endif; ?>
最后的话
希望您发现WordPress条件标签和我一样令人印象深刻。 它为我节省了开发模板的大量时间,而不必花我的时间。
另外,请务必查看WordPress Codex,以了解它还可以做什么。 编码愉快!
编者注:这篇文章是由Filip Stefansson为Hongkiat.com撰写的。 Filip是来自瑞典的Web开发人员和WordPress爱好者。
翻译自: https://www.hongkiat.com/blog/wordpress-conditional-tags-beginners/