WordPress自带的条件标签应用阐明

WordPress自带的条件标签能够让你根据条件显示不同的内容,比方,你能够反省用户是在首页?能否登陆?

PHP if(语句)
用php的条件语句你能够判别一些事件的虚实,假如是真,代码将被执行,否则什么也不发作.看上面的语句,置信你能够了解.
 
<?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来添加另一个条件语句,如下:
 
<?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;
?>

以上就是php的条件语句,好了,上面咱们进入WordPress的条件标签.

条件标签怎样起作用
应用WordPress自带的函数如is_home(),你能够轻松的讯问WordPress以后的用户能否在首页.WordPress将会答复你是或否,即1或0.
 
<?php
if( is_home() ):
echo ‘User is on the homepage.’;
else:
echo ‘User is not on the homepage’;
endif;
?>

你能够查问更多对于WordPress的codex条件标签列表.

多个条件的混合应用
有时你查问的条件语句不止一个,你能够应用”和”或”或”即”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;
?>

什么时分应用条件标签
条件标签十分有用,它能够用来判别用户能否登陆?用户能否应用是ie阅读器?能否有文章显示等等.
看上面的例子:
 
<?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;

自定义条件标签
在WordPress的条件标签一览表外面,你能够看到大局部的content或page的条件标签,假如你持续深究的话,你还会发现以下的.

反省用户能否登陆
假如你的博客有很多个作者,上面这个代码能够让你晓得用户能否登陆.
 
if ( is_user_logged_in() ):
echo ‘Welcome, registered user!’;
else:
echo ‘Welcome, visitor!’;
endif;

你的网站能否能够注册
 
<?php if ( get_option(‘users_can_register’):
echo ‘Registrations are open.’;
else:
echo ‘Registrations are closed.’;
endif;
判别用户应用的电脑是pc或mac
if( stristr($_SERVER['HTTP_USER_AGENT'],”mac”) ):
echo ‘Hello, I’m a Mac.’;
else:
echo ‘And I’m a PC.’;
endif;

用户登陆时,让Google剖析代码不起作用
假如在你的网站应用了Google的剖析代码,你可能心愿去跟踪真正的拜访者,而不是作者.不要遗记修正Google的剖析id的UA-XXXXXXX-X (如下)
 
<?php
// function for inserting Google Analytics into the wp_head
add_action(‘wp_footer’, ‘ga’);
function ga() {
if ( !is_user_logged_in() ): // íf 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; ?>

当查问后果仅有一篇时间接以单页的形式显示(这个很好,我感觉)
增加以下代码片断到你的主题文件夹的functions.php文件里,它将会主动重定向到当你的搜寻后果仅有一篇时以单页形式显示.
 
<?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) ): ?>
<p class=”separator”></p>
<?php endif; ?>

判别以后的用户能够做什么…
 
<?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;
?>

除了admin,使他人禁用Tinymce HTML编辑器
把上面的代码增加到functions.php文件里限度只有admin能够应用编辑器
 
<?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>’;
}
}
?>

你能够把所遇到过的条件标签都整合起来,这些条件标签当你制件主题的时分会很不便,你能否有不同的条件标签,能够拿进去与大家一同分享.

以上就是安达网络工作室关于《WordPress自带的条件标签使用说明》的一些看法。更多内容请查看本栏目更多内容!

本文相关话题: 条件标签 标签说明 WordPress
版权声明:本文为 安达网络工作室 转载文章,如有侵权请联系我们及时删除。
相关文章
WordPress中Gravatar头像缓存到本地及相干优化的技巧

将Gravatar寰球通用头像缓存的目的在于放慢网站的关上速度,由于Gravatar官网的效劳器在国外,加上伟大的GF...

详解WordPress开发中用于获取分类及子页面的函数用法

get_category get_category 可能咱们平常接触的不多,但却是很有用,网上这个函数引见的貌似不多,所以明天...

WordPress中设置Post Type自定义文章类型的实例教程

什么是自定义post&#63; 不要想当然的以为这里的post就是就是指博客中的文章,它只不过是一个文章类的代理词...

wordpress获取自定义字段get_post_meta函数应用引见

wordpress能够设置自定义字段,不便扩大性能,wordpress利用巧妙的数据库表设计达到这一目的,posts表寄存文...

自定义wordpress登录页的一些技巧办法

下文提到的一切代码,都是增加到主题的funshions.php 文件的最初一个 ?> 的后面。 制造一个名为 login_logo...

wordpress编辑器中增加链接性能主动退出nofollow的办法

由于偶然加一些站外链接,普通都是nofollow,独自写入很费事,所以揣摩从编辑器动手,经过这个插件(http:/...

需求提交

客服服务