Wordpress - 如果复选框输出与页面标题匹配则显示文本

发布于 2024-11-08 12:53:10 字数 971 浏览 0 评论 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 解决了我的问题,对问题进行了小更新:

我怎样才能整齐地编码 - 使用您的答案,品牌只是复选框的一个示例,还有其他几个,例如网络、打印、社交等我怎样才能将它们与页面标题相匹配?

因此,如果选中的字段等于页面标题“branding”,则将遵循以下方式 OR 选中的字段等于页面标题“web” OR 选中的字段等于页面标题“print ”。

I want to show the projects that has had it's checkbox ticked as Branding, if it's on the Branding page (i.e the page title is Branding).

To explain the code a bit:

This line show's all the checkboxes that have been ticked for each project so it will output "Branding", "Web", "Print" if they have been ticked.

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

This next line is just checking the page title is "Branding":

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

I'm trying to put these both in an if statement where it would just output the checked boxes and if they match the title then output them.

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

The code above shows what I want to do but it doesn't quite work.

IMPORTANT: I'm using this plugin to create the custom checkboxes so please bear that in mind.

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

UPDATE:
Thanks very much to Adam Kiss for solving what I asked, small update to question:

How could I code this neatly - using your answer, Branding was just one example of the check boxes, there's also several other one's like Web, Print, Social so how could I match those to the page title as well?

So it will be along the lines of if checked field equals the page title "branding" do OR checked field equals page title "web" OR checked field equals page title "print".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

謸气贵蔟 2024-11-15 12:53:10

您要查找的函数是 in_array

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

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

编辑:因为我们正在使用implode() ,我假设 get_field 返回类型 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
}

the function you're looking for is in_array:

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

Note: this assumes that result of that implode is array with strings like "Branding", "Web", etc.

Edit: Since we're using implode(), I assume get_field returns type array, so we put the implode away (I got confused for a while)

Edit: Sorry, was away :]

you could use array_intersect

Usage:

$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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文