致力于为用户提供真实的
主机测评数据及优惠信息

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) ): ?> 
<div class=”separator”></div> 
<?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>’; 


?> 


你可以把所遇到过的条件标签都整合起来,这些条件标签当你制件主题的时候会很方便,你是否有不同的条件标签,可以拿出来与大家一起分享.

赞(0) 打赏
未经允许不得转载:爱主机 » WordPress自带的条件标签使用说明
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址