2

我正在开发 Wordpress 主题,我正在使用 Redux 框架来获取用户选项。我想根据用户选择的类别动态创建小部件。这些小部件将显示在所选特定类别前端用户的类别页面中。

Redux Framework 正在返回具有已检查和未检查类别的数组。选中类别的值为 1,未选中的类别值为 0

以下是我时的输出print_r($redcats)

Array
(
    [14] => 1
    [13] => 1
    [7] => 0
    [1] => 1
)

header.php我运行代码仅提取包含值为 1 的索引时,代码运行完美并给了我想要使用的内容foreach() loop,以便我稍后可以获取类别名称和类别 slug,如下所示,

    $redcats = $mythemeslug['opt_extrac_widgets_selected'];
    foreach($redcats as $key=>$val)
    {
        if($val == 1)
        {
            echo "Category is ".returncatname1($key)." | and Slug is "
           .returncatslug1($key)."</br>";
        }
    }

    function returncatslug1($id)
    {
        $category = get_category( $id );
        return $category->slug;
    }

    function returncatname1($id)
    {
        $catname = get_cat_name($id);
        return $catname;
    } 

以下是输出,因为我header.php首先运行了整个代码以测试一切是否正常:

Category is History | and Slug is history
Category is My Gossipz | and Slug is my-gossipz
Category is Uncategorized | and Slug is uncategorized

但是当我把同样foreach loop的东西放进去时functions.php会给我一个错误

Warning: Invalid argument supplied for foreach() in 
C:\xampp\htdocs\wp\wp-content\themes\**themename**\functions.php on line 111

其中第 111 行包含以下代码。

$redcats = $mythemeslug['opt_extrac_widgets_selected'];
foreach($redcats as $key=>$val)
{
 //some codes comes in..
}

我想知道为什么我Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\wp\wp-content\themes\**themename**\functions.php on line 111header.php运行相同的代码时会得到正确的结果..

请注意,$mythemeslug['opt_extrac_widgets_selected']Redux 框架是否返回了结果,并且我已经在其中操作了其他 Redux 框架选项functions.php并且它们工作正常

供参考:在 Redux Framework 选项文件中,用于获取类别的代码是

array(
    'id'       => 'opt_extrac_widgets_selected',
    'type'     => 'checkbox',
    'title'    => 'Activate Category to have its own Widgets',
    'required' => array( 'opt_extrac_widgets_set', '=', '1' ),//depends on above option.
    'data'     => 'categories'
   ),
4

0 回答 0