WordPress自帶的條件標(biāo)簽可以讓你依據(jù)條件顯示不同的內(nèi)容,比如,你可以檢查用戶是在首頁?是否登陸?
PHP if(語句)
用php的條件語句你可以判斷一些事情的真假,如果是真,代碼將被執(zhí)行,否則什么也不發(fā)生.看下面的語句,相信你可以理解.
<?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的條件語句,好了,下面我們進(jìn)入WordPress的條件標(biāo)簽.
條件標(biāo)簽怎么起作用
使用WordPress自帶的函數(shù)如is_home(),你可以輕松的詢問WordPress當(dāng)前的用戶是否在首頁.WordPress將會回答你是或否,即1或0.
<?php
if( is_home() ):
echo ‘User is on the homepage.’;
else:
echo ‘User is not on the homepage’;
endif;
?>
你可以查詢更多關(guān)于WordPress的codex條件標(biāo)簽列表.
多個條件的混合使用
有時你查詢的條件語句不止一個,你可以使用”和”或”或”即”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;
?>
什么時候使用條件標(biāo)簽
條件標(biāo)簽非常有用,它可以用來判斷用戶是否登陸?用戶是否使用是ie瀏覽器?是否有文章顯示等等.
看下面的例子:
<?php if ( have_posts() ) : ?>
… posts …
<?php else : ?>
… search field …
<?php endif; ?>
檢查是否有文章顯示,如果沒有,則顯示搜索框.
WordPress條件標(biāo)簽的用法舉例:
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;
自定義條件標(biāo)簽
在WordPress的條件標(biāo)簽一覽表里面,你可以看到大部分的content或page的條件標(biāo)簽,如果你繼續(xù)深究的話,你還會發(fā)現(xiàn)以下的.
檢查用戶是否登陸
如果你的博客有很多個作者,下面這個代碼可以讓你知道用戶是否登陸.
if ( is_user_logged_in() ):
echo ‘Welcome, registered user!’;
else:
echo ‘Welcome, visitor!’;
endif;
你的網(wǎng)站是否可以注冊
<?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分析代碼不起作用
如果在你的網(wǎng)站使用了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;
}
?>
自定義文章的類型
下面的例子可以讓你判斷當(dāng)前的文章是否是特定的文章類型,比如books
<?php if ( is_singular( ‘books’ ) ):
// This post is of the custom post type books.
endif; ?>
當(dāng)查詢結(jié)果僅有一篇時直接以單頁的方式顯示(這個很好,我覺得)
添加以下代碼片斷到你的主題文件夾的functions.php文件里,它將會自動重定向到當(dāng)你的搜索結(jié)果僅有一篇時以單頁方式顯示.
<?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 ) );
}
}
}
?>
檢查是否是最后一篇文章
你可能會在你的文章列表里面添加些東西(比如廣告或推薦),把以下代碼添加到循環(huán)內(nèi),可以讓你在最后一篇文章之前顯示出添加的東西。
<?php if( ($wp_query->current_post + 1) < ($wp_query->post_count) ): ?>
<div class=”separator”></div>
<?php endif; ?>
判斷當(dāng)前的用戶可以做什么…
<?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>’;
}
}
?>
你可以把所遇到過的條件標(biāo)簽都整合起來,這些條件標(biāo)簽當(dāng)你制件主題的時候會很方便,你是否有不同的條件標(biāo)簽,可以拿出來與大家一起分享.