php - 如何检查当前猫是否在数组内并且 IF 是父级

标签 php wordpress categories

我有一个名为 $best_cats

的全局变量
global $best_cats;
$best_cats =  array(8,31,51,102,217,218);

现在我需要检查当前的猫是否在数组内,并且如果当前的猫是父猫。

我正在尝试这种方法,但不起作用。我做错了什么以及我该怎么做?

<?php 
    $this_category = get_category($cat);
    $cat_id = $this_category->cat_ID;
    if (in_array($cat_id, $best_cats) && ($this_category->category_parent == 0 )) { ?>

    //category is inside of the array and is a parent cat

    <?php } else { ?>

    //category is not inside the array and It's not a parent either

    <?php } ?>

最佳答案

看起来你忘记了global

 <?php 
        global $best_cats;
        $this_category = get_category($cat);
        $cat_id = $this_category->cat_ID;
        if (in_array($cat_id, $best_cats) && ($this_category->category_parent == 0 )) {
            //category is inside of the array and is a parent cat
        } else {    
            //category is not inside the array and It's not a parent either
        } 

关于php - 如何检查当前猫是否在数组内并且 IF 是父级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39851446/

相关文章:

php - Laravel MySQL 匹配不返回结果

php - 主管 "exit status 1 not expected"运行 php 脚本

php - 在本地主机上保护基于 php/mysql 的应用程序

php - 我的侧边栏小部件在 Wordpress 中不是垂直的?如何正确对齐?

php - Wordpress - 覆盖主题 CSS 样式表

iphone - 在子类上声明类别

php - 用 twig 在 html 中包裹多行

android - 如何防止 HTML 签名中的图像在 iphone 和 Android 上换行?

php - 如何在MySQL中选择所有类别并统计每个类别文章的数量

php - WooCommerce 自定义循环按名称而不是 Slug