【问题标题】:Wordpress: check whether the current post is in any of the specified termsWordpress:检查当前帖子是否在任何指定的术语中
【发布时间】:2022-01-03 12:51:50
【问题描述】:

我有一个自定义帖子类型property 和一个自定义分类features。在单个 property 页面上,我输出来自 features 的所有术语,包括空的。但是,我需要检查当前帖子出现在这些术语中的哪一个。

在出口处你需要得到类似的东西:✓ cleaningX Internet✓ home phone

所以,我明白了所有条款:

$features = get_terms([
        'taxonomy' => 'features',
        'hide_empty' => false,
    ]);

然后我尝试将它们整理出来并检查正在检查的术语中是否有当前帖子。

foreach ($features as $feature) :
    if (has_term($feature)) {
        echo '✓' . $feature->name;
    } else {
        echo 'X' . $feature->name;
    }
endforeach;

但我明白了:

X cleaningX InternetX home phone

【问题讨论】:

    标签: php wordpress foreach taxonomy term


    【解决方案1】:

    好吧,我犯了一个错误,这就是它的工作方式)

    foreach ($features as $feature) :
        if (has_term($feature, 'features')) {
            echo '✓' . $feature->name;
        } else {
            echo 'X' . $feature->name;
        }
    endforeach;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-25
      • 1970-01-01
      • 1970-01-01
      • 2011-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多