使用in_array看起来是正确的,但是会抛出错误

时间:2017-04-04 04:15:22

标签: php wordpress wordpress-theming

我在使用in_array搜索时遇到问题,但代码看起来很好。我究竟错过了什么。

os.path

显示的错误消息:

  

警告:in_array()期望参数2为数组,给定整数   C:\ XAMPP \ htdocs中\ testwp2 \可湿性粉剂内容\主题\我的主题\模板frontpage.php   在第119行

     

警告:array_push()期望参数1为数组,给定整数   在   C:\ XAMPP \ htdocs中\ testwp2 \可湿性粉剂内容\主题\我的主题\模板frontpage.php   在第122行

2 个答案:

答案 0 :(得分:2)

您需要更改此行

$prevCategoryName = array_push($prevCategoryName, $categoryName);

array_push($prevCategoryName, $categoryName);

答案 1 :(得分:0)

以下是更正后的代码:

$postCats = wp_get_post_categories($postId, array('orderby' => 'term_order', 'order' => 'ASC')); 

$prevCategoryName = array();
$most_latest_post = "";
foreach($postCats as $cat) {
    $categoryName = get_cat_name($cat);
    if (!in_array($categoryName, $prevCategoryName)) {
        $most_latest_post .= '<div class="postCatTags">' . $categoryName . '</div>';
    }
    array_push($prevCategoryName, $categoryName);
}
相关问题