简体   繁体   English

WordPress中高级自定义字段的测试条件为true

[英]test condition of advance custom field is true in WordPress

i'm trying to test if a post has either of two conditions of a custom field. 我正在尝试测试帖子是否具有自定义字段的两个条件之一。 i'm using the plugin advanced custom types. 我正在使用插件高级自定义类型。 i've created a field called visibility whereby the administrator can select whether a post of page is public or private. 我创建了一个名为“可见性”的字段,管理员可以在其中选择页面帖子是公开的还是私有的。 this will then display a custom message depending on whether the user is logged in and the condition of the field. 然后将显示一条自定义消息,具体取决于用户是否已登录以及字段的条件。 However with my code below the condition is always returned as public even if the field is set to private on a post. 但是,在我的代码低于此条件的情况下,即使在帖子中将该字段设置为private,该条件也始终作为public返回。

<?
$visibility = get_field('visibility');

if($visibility='public'){ 


get_template_part( 'content', 'single' ); 


} else if ($visibility='private') { ?>

<p>You must be logged in to view this post</p>

<?php } ?>

Shorty, take a look at a difference between = and == . 矮个子,看看===之间的区别。

You are assigning, and therefore your first condition is always true. 您正在分配,因此您的第一个条件始终为true。

For comparing use always == or === , pay a great deal of attention to that. 为了始终使用=====进行比较,请特别注意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM