资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

wordpress判断 wordpress判断产品是否更新

wordpress判断是否启用伪静态或固定链接类型

有时候需要判断wordpress站点是否设置了伪静态,即后台固定链接设置中的选择了非”默认”的结构。

创新互联专注于沙依巴克企业网站建设,响应式网站建设,商城网站建设。沙依巴克网站建设公司,为沙依巴克等地区提供建站服务。全流程按需设计网站,专业设计,全程项目跟踪,创新互联专业和态度为您提供的服务

下面看一下判断方法:

若是只写get_option(‘permalink_structure’) 判断是否是自定义结构。

wordpress多条件判断问题

is_archive只能判断当前是不是存档页,并不能判断具体是哪个存档页

如果你的news, dljz等是自定义post_type,可以参考is_post_type_archive()这个函数。

但如果是分类的,就得使用以下方法:

if ( is_category() ) {

$cid = intval( get_query_var( 'cat' ) );

$category = get_cateogry( $cid );

switch ( $category-slug ) {

case 'news':

$bannerimg = 'news.jpg';

break;

case 'dljz':

$bannerimg = 'dljz.jpg';

break;

case 'zcgs':

$bannerimg = 'zcgs.jpg';

break;

default:

$bannerimg = 'default-banner.jpg';

break;

}

}

以上代码提供参考哦。

WordPress:如何判断登录用户的角色

注:以下内容在WP

3.4+上测试通过current_user_can()的正确用法current_user_can()文档中有一句话要注意一下Do

not

pass

a

role

name

to

current_user_can(),

as

this

is

not

guaranteed

to

work

correctly.意思是说传递用户角色名称(如author、contributor)作为参数不能100%保证返回正确的结果,正确的用法是传递$capability,从这个函数的表面意思看,参数是权限比参数是角色名称更靠谱。所以,要根据不同角色拥有的权限来判断用户角色,用户权限可以在Roles

and

Capabilities中找到。判断用户是否为管理员(Administrator)if(

current_user_can(

'manage_options'

)

)

{

echo

'The

current

user

is

a

administrator';

}判断用户是否为编辑(Editor)if(

current_user_can(

'publish_pages'

)

!current_user_can(

'manage_options'

)

)

{

echo

'The

current

user

is

an

editor';

}判断用户是否为作者(Author)if(

current_user_can(

'publish_posts'

)

!current_user_can(

'publish_pages'

)

)

{

echo

'The

current

user

is

an

author';

}判断用户是否为投稿者(Contributor)if(

current_user_can(

'edit_posts'

)

!current_user_can(

'publish_posts'

)

)

{

echo

'The

current

user

is

a

contributor';

}判断用户是否为订阅者(Subscriber)if(

current_user_can(

'read'

)

!current_user_can(

'edit_posts'

)

)

{

echo

'The

current

user

is

a

subscriber';

}用$current_user判断$current_user是WordPress的一个全局变量,当用户登录后,这个里面就会有用户的角色和权限信息。当WordPress的init

action执行后,就可以安全的使用$current_user全局变量了。在模板文件中判断登录用户是否为作者(Author)global

$current_user;

if(

$current_user-roles[0]

==

'author'

)

{

echo

'The

current

user

is

an

author';

}

在functions.php中判断用户是否为作者(Author)add_action(

'init',

'check_user_role'

);

function

check_user_role()

{

global

$current_user;

if(

$current_user-roles[0]

==

'author'

)

{

echo

'The

current

user

is

an

author';

}

}

之所以要使用add_action(

'init',

'check_user_role'

);是因为$current_user这个全部变量到init

action执行时才完成赋值,既然要读它的内容,至少要等到它的内容准备好后再读取。functions.php的代码先与init

action执行,所以在functions.php中直接写global

$current_user是无法获取用户信息的。详细信息可以参考《WordPress

Actions加载顺序》。检查用户角色之前,还可以先检查一下用户是否登录

WordPress如何判断当前文章是否为自定义类型

get_post_type() == '你的自定义类型'

如果是内页的话$post_type = $post-post_type;

参考:WordPress自定义文章类型和自定义分类法


网站栏目:wordpress判断 wordpress判断产品是否更新
网页路径:http://cdkjz.cn/article/ddehehe.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220