0

我想显示已将复选框勾选为品牌的项目,如果它在品牌页面上(即页面标题是品牌)。

稍微解释一下代码:

此行显示了每个项目已勾选的所有复选框,因此如果已勾选,它将输出“Branding”、“Web”、“Print”。

implode(', ',get_field('categories')

下一行只是检查页面标题是“Branding”:

implode(', ',get_field('categories')

我试图将它们都放在一个 if 语句中,它只会输出选中的框,如果它们与标题匹配,则输出它们。

<?php if(implode(', ',get_field('categories')) && $grid_title == "Branding"); {
echo "testing"; 
}
?>

上面的代码显示了我想要做的,但它并不完全有效。

重要提示:我正在使用这个插件来创建自定义复选框,所以请记住这一点。

==============================

更新: 非常感谢 Adam Kiss 解决了我的问题,对问题的小更新:

我怎么能巧妙地编码这个 - 使用你的答案,品牌只是复选框的一个例子,还有其他几个,比如 Web、打印、社交,那么我怎么能将它们与页面标题匹配呢?

因此,如果选中的字段等于页面标题“branding”,或者选中的字段等于页面标题“web”选中的字段等于页面标题“print”,它将是这样的。

4

1 回答 1

0

您正在寻找的功能是in_array

<?php
   if(
       in_array("Branding", get_field('categories')) 
       && $grid_title == "Branding"
   ){
     echo "testing";
   }

注意:这假设内爆的结果是带有“Branding”、“Web”等字符串的数组。

编辑:由于我们正在使用implode(),我假设 get_field 返回 type array,所以我们把内爆放在一边(我有一段时间感到困惑)

编辑:对不起,不在了:]

你可以使用array_intersect

用法:

$categories = get_field('categories');
$cats_iwant = array("Branding", "Print", "Design");

$inarray = array_intersect($categories, $cats_iwant);
//this '$inarray' now has values like 'Branding', 'Design' which are in both arrays

if (count($inarray) > 0) {
  //we have at least one common word ('Branding', ...)
}

//short version
if (count(array_intersect(get_field('categories'),array(
    'Branding', 'Design', 'Print'
   ))) > 0)
{
 //do stuff
}
于 2011-05-19T08:43:48.603 回答