<?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
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;
?>
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;
<?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; ?>
<?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;
?>