WordPress

推荐列表 站点导航

当前位置:首页 > 建站教程 > WordPress >

WordPress自带的条件标签使用说明

来源:网络整理  作者:  发布时间:2020-12-17 16:01
WordPress自带的条件标签可以让你依据条件显示不同的内容,比如,你可以检查用户是在首页?是否登陆?PHPif(语句) 用php的...
endif;
?>  
endif;  
if(10 == 11):
var ga = document.createElement(‘script’); ga.type = ‘text/javascript’; ga.async = true;
<?php
echo ‘Welcome, registered user!’;
endif;
<?php if ( is_singular( ‘books’ ) ):

用php的条件语句你可以判断一些事情的真假,如果是真,代码将被执行,否则什么也不发生.看下面的语句,相信你可以理解.

if( is_home() AND is_page( 5 ) ):
echo ‘<style type=”text/css”>#editor-toolbar #edButtonHTML, #quicktags {display: none;}</style>’;
function single_result() {

条件标签非常有用,它可以用来判断用户是否登陆?用户是否使用是ie浏览器?是否有文章显示等等.

elseif(10 == 15):
endif;
# The user is reading a post or a page
endif;
if (is_search()) {
add_action(‘wp_footer’, ‘ga’);
# The user is on a page, but not the homepage

当查询结果仅有一篇时直接以单页的方式显示(这个很好,我觉得)

判断当前的用户可以做什么…

if( stristr($_SERVER['HTTP_USER_AGENT'],”mac”) ):
else:

下面的例子可以让你判断当前的文章是否是特定的文章类型,比如books

自定义文章的类型

你同样可以用elseif来增加另一个条件语句,如下:

endif;
 
endif;
_gaq.push(['_trackPageview']);
}
// This post is of the custom post type books.
echo ‘And I’m a PC.’;
?>
?>
 

用户登陆时,让Google分析代码不起作用

echo ‘Registrations are open.’;
// function for inserting Google Analytics into the wp_head
}
else:
… search field …

在WordPress的条件标签一览表里面,你可以看到大部分的content或page的条件标签,如果你继续深究的话,你还会发现以下的.

echo ‘User is on the homepage or the page where the ID is 5′;
if ( !is_user_logged_in() ): // íf user is not logged in
if( is_admin() ):
})();
if( is_home() OR is_page( 5 )):
add_filter( ‘wp_default_editor’, create_function(”, ‘return “tinymce”;’) );
}
endif;
 

检查是否有文章显示,如果没有,则显示搜索框.

如果在你的网站使用了Google的分析代码,你可能希望去跟踪真正的访问者,而不是作者.不要忘记修改Google的分析id的UA-XXXXXXX-X (如下)

endif;
add_action(‘template_redirect’, ‘single_result’);
function disable_html_editor_wps() {
<?php
</script>
echo ‘This is false, and will not be shown.’;

添加以下代码片断到你的主题文件夹的functions.php文件里,它将会自动重定向到当你的搜索结果仅有一篇时以单页方式显示.

add_action( ‘admin_head’, ‘disable_html_editor_wps’ );

什么时候使用条件标签

<?php

你可以查询更多关于WordPress的codex条件标签列表.

有时你查询的条件语句不止一个,你可以使用”和”或”或”即”and”与”or”.

if( is_single() OR is_page() ):
… posts …
global $wp_query;

条件标签怎么起作用

echo ‘User is not on the homepage’;

WordPress自带的条件标签使用说明 (https://www.wpmee.com/) WordPress使用教程 第1张

除了admin,使别人禁用Tinymce HTML编辑器

if( current_user_can(‘editor’) ):
// true if user is an editor
if ($wp_query->post_count == 1) {
_gaq.push(['_setAccount', 'UA-XXXXXXX-X']); // insert your Google Analytics id here
if ($current_user->user_level != 10) {
# User is administator
<?php if ( get_option(‘users_can_register’):
var _gaq = _gaq || [];
<?php else : ?>
endif;
<?php if ( have_posts() ) : ?>
get_currentuserinfo();
<?php endif; ?>
 
<script type=”text/javascript”>

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

function ga() {
endif;
endif;
if( current_user_can(‘edit_posts’) ):
<?php
echo ‘Since none of the above is true, this will be shown.’;
if(10 == 10):
endif; ?>
 
}
if( is_home() AND is_page(’1′) ):
?>
 
endif;
echo ‘User is on the homepage.’;
?>
 

你可能会在你的文章列表里面添加些东西(比如广告或推荐),把以下代码添加到循环内,可以让你在最后一篇文章之前显示出添加的东西。

endif;
// true if user can edit posts

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

?>  
echo ‘This is false, and will not be shown.’;

如果你的博客有很多个作者,下面这个代码可以让你知道用户是否登陆.

<?php

使用WordPress自带的函数如is_home(),你可以轻松的询问WordPress当前的用户是否在首页.WordPress将会回答你是或否,即1或0.

echo ‘Registrations are closed.’;
else:
?>
 
else:
<?php
echo ‘User is on the homepage, and the home pages ID is 5′;

你的网站是否可以注册

?>
 
ga.src = (‘https:’ == document.location.protocol ? ‘https://ssl’ : ‘’) + ‘.google-analytics.com/ga.js’;
# The user is at the home page and the home page is a page with the ID 1
<?php

WordPress条件标签的用法举例:

<?php
endif;
if( !is_home() AND is_page() ):

检查用户是否登陆

<?php if( ($wp_query->current_post + 1) < ($wp_query->post_count) ): ?>
?>
 

多个条件的混合使用

}
}
var s = document.getElementsByTagName(‘script’)[0]; s.parentNode.insertBefore(ga, s);
_gaq.push(['_trackPageLoadTime']);
<?php
global $current_user;
endif;
<div class=”separator”></div>
<?php endif; ?>
 
if( !current_user_can(‘administrator’) ):
if(10 == 15):
echo ‘This is true, and will be shown.’;
// true if user is not admin

检查是否是最后一篇文章

echo ‘This is false, and will not be shown.’;
echo ‘Hello, I’m a Mac.’;
echo ‘Welcome, visitor!’;
if( is_home() ):
endif;

自定义条件标签

if ( is_user_logged_in() ):
wp_redirect( get_permalink( $wp_query->posts['0']->ID ) );

把下面的代码添加到functions.php文件里限制只有admin可以使用编辑器

判断用户使用的电脑是pc或mac
(function() {
else:

看下面的例子:

相关热词: 标签

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!

本文地址: https://v30.fanwenzhu.com/jz/wp/4869.shtml

Copyright © www.juheyunku.com      关于 | 合作 | 声明 | 联系 | 更新 | 地图 | Tags

WordPress自带的条件标签使用说明

2020-12-17 编辑:

endif;
?>  
endif;  
if(10 == 11):
var ga = document.createElement(‘script’); ga.type = ‘text/javascript’; ga.async = true;
<?php
echo ‘Welcome, registered user!’;
endif;
<?php if ( is_singular( ‘books’ ) ):

用php的条件语句你可以判断一些事情的真假,如果是真,代码将被执行,否则什么也不发生.看下面的语句,相信你可以理解.

if( is_home() AND is_page( 5 ) ):
echo ‘<style type=”text/css”>#editor-toolbar #edButtonHTML, #quicktags {display: none;}</style>’;
function single_result() {

条件标签非常有用,它可以用来判断用户是否登陆?用户是否使用是ie浏览器?是否有文章显示等等.

elseif(10 == 15):
endif;
# The user is reading a post or a page
endif;
if (is_search()) {
add_action(‘wp_footer’, ‘ga’);
# The user is on a page, but not the homepage

当查询结果仅有一篇时直接以单页的方式显示(这个很好,我觉得)

判断当前的用户可以做什么…

if( stristr($_SERVER['HTTP_USER_AGENT'],”mac”) ):
else:

下面的例子可以让你判断当前的文章是否是特定的文章类型,比如books

自定义文章的类型

你同样可以用elseif来增加另一个条件语句,如下:

endif;  
endif;
_gaq.push(['_trackPageview']);
}
// This post is of the custom post type books.
echo ‘And I’m a PC.’;
?>
?>  

用户登陆时,让Google分析代码不起作用

echo ‘Registrations are open.’;
// function for inserting Google Analytics into the wp_head
}
else:
… search field …

在WordPress的条件标签一览表里面,你可以看到大部分的content或page的条件标签,如果你继续深究的话,你还会发现以下的.

echo ‘User is on the homepage or the page where the ID is 5′;
if ( !is_user_logged_in() ): // íf user is not logged in
if( is_admin() ):
})();
if( is_home() OR is_page( 5 )):
add_filter( ‘wp_default_editor’, create_function(”, ‘return “tinymce”;’) );
}
endif;
 

检查是否有文章显示,如果没有,则显示搜索框.

如果在你的网站使用了Google的分析代码,你可能希望去跟踪真正的访问者,而不是作者.不要忘记修改Google的分析id的UA-XXXXXXX-X (如下)

endif;
add_action(‘template_redirect’, ‘single_result’);
function disable_html_editor_wps() {
<?php
</script>
echo ‘This is false, and will not be shown.’;

添加以下代码片断到你的主题文件夹的functions.php文件里,它将会自动重定向到当你的搜索结果仅有一篇时以单页方式显示.

add_action( ‘admin_head’, ‘disable_html_editor_wps’ );

什么时候使用条件标签

<?php

你可以查询更多关于WordPress的codex条件标签列表.

有时你查询的条件语句不止一个,你可以使用”和”或”或”即”and”与”or”.

if( is_single() OR is_page() ):
… posts …
global $wp_query;

条件标签怎么起作用

echo ‘User is not on the homepage’;

WordPress自带的条件标签使用说明 (https://www.wpmee.com/) WordPress使用教程 第1张

除了admin,使别人禁用Tinymce HTML编辑器

if( current_user_can(‘editor’) ):
// true if user is an editor
if ($wp_query->post_count == 1) {
_gaq.push(['_setAccount', 'UA-XXXXXXX-X']); // insert your Google Analytics id here
if ($current_user->user_level != 10) {
# User is administator
<?php if ( get_option(‘users_can_register’):
var _gaq = _gaq || [];
<?php else : ?>
endif;
<?php if ( have_posts() ) : ?>
get_currentuserinfo();
<?php endif; ?>
 
<script type=”text/javascript”>

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

function ga() {
endif;
endif;
if( current_user_can(‘edit_posts’) ):
<?php
echo ‘Since none of the above is true, this will be shown.’;
if(10 == 10):
endif; ?>
 
}
if( is_home() AND is_page(’1′) ):
?>
 
endif;
echo ‘User is on the homepage.’;
?>
 

你可能会在你的文章列表里面添加些东西(比如广告或推荐),把以下代码添加到循环内,可以让你在最后一篇文章之前显示出添加的东西。

endif;
// true if user can edit posts

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

?>  
echo ‘This is false, and will not be shown.’;

如果你的博客有很多个作者,下面这个代码可以让你知道用户是否登陆.

<?php

使用WordPress自带的函数如is_home(),你可以轻松的询问WordPress当前的用户是否在首页.WordPress将会回答你是或否,即1或0.

echo ‘Registrations are closed.’;
else:
?>
 
else:
<?php
echo ‘User is on the homepage, and the home pages ID is 5′;

你的网站是否可以注册

?>
 
ga.src = (‘https:’ == document.location.protocol ? ‘https://ssl’ : ‘’) + ‘.google-analytics.com/ga.js’;
# The user is at the home page and the home page is a page with the ID 1
<?php

WordPress条件标签的用法举例:

<?php
endif;
if( !is_home() AND is_page() ):

检查用户是否登陆

<?php if( ($wp_query->current_post + 1) < ($wp_query->post_count) ): ?>
?>
 

多个条件的混合使用

}
}
var s = document.getElementsByTagName(‘script’)[0]; s.parentNode.insertBefore(ga, s);
_gaq.push(['_trackPageLoadTime']);
<?php
global $current_user;
endif;
<div class=”separator”></div>
<?php endif; ?>
 
if( !current_user_can(‘administrator’) ):
if(10 == 15):
echo ‘This is true, and will be shown.’;
// true if user is not admin

检查是否是最后一篇文章

echo ‘This is false, and will not be shown.’;
echo ‘Hello, I’m a Mac.’;
echo ‘Welcome, visitor!’;
if( is_home() ):
endif;

自定义条件标签

if ( is_user_logged_in() ):
wp_redirect( get_permalink( $wp_query->posts['0']->ID ) );

把下面的代码添加到functions.php文件里限制只有admin可以使用编辑器

判断用户使用的电脑是pc或mac
(function() {
else:

看下面的例子:

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供学习参考!
本文地址为 https://v30.fanwenzhu.com/jz/wp/4869.shtml

相关文章

风云图片

推荐阅读

返回WordPress频道首页