获取分类术语并将其插入到数组中

时间:2017-06-06 作者:Ryan Coolwebs

我正在使用shortcake UI插件,并努力将一系列短代码注册到UI界面中。

我正在尝试获取所有分类术语(通过get\\u terms()),然后填充接口的数组。现在,我遇到了这样一个分析错误:

Parse error: syntax error, unexpected \';\', expecting \')\' in...
代码如下:

// Register the Featured Pages shortcode with \'Shortcake UI\' plugin
add_action( \'register_shortcode_ui\', \'shortcode_ui_feat_pages\' );

function shortcode_ui_feat_pages()
{
    shortcode_ui_register_for_shortcode(
        \'featured-pages\',
        array(
            \'label\' => \'Featured Pages Shortcode\',
            \'listItemImage\' => \'dashicons-editor-table\',
            \'attrs\' => array(
                array(
                    \'label\' => \'Category\',
                    \'attr\' => \'category\',
                    \'type\' => \'select\',
                    \'options\' => array(
                    $feat_pages_terms = get_terms(array(\'taxonomy\' => \'featured_page_category\', \'hide_empty\' => false));
                    foreach($feat_pages_terms as $feat_page_term) :
                        echo \'array(\\\'value\\\' =>\\\'\' . $feat_page_term->name . \'\\\', \\\'label\\\' =>\\\'\' . $feat_page_term->name . \'\\\'), \';
                    endforeach;
                    ),
                    \'description\' => \'Insert the category full name (i.e. Home page)\',
                ),
                array(
                    \'label\' => \'Columns\',
                    \'attr\' => \'cols\',
                    \'type\' => \'select\',
                    \'options\' => array(
                        array(\'value\' => \'\', \'label\' => esc_html__(\'\')),
                        array(\'value\' => 2, \'label\' => esc_html__(\'Two\')),
                        array(\'value\' => 3, \'label\' => esc_html__(\'Three\')),
                        array(\'value\' => 4, \'label\' => esc_html__(\'Four\')),
                    ),
                    \'description\' => \'The number of columns you want to display within a row\',
                ),
                array(
                    \'label\' => \'Order By\',
                    \'attr\' => \'orderby\',
                    \'type\' => \'select\',
                    \'options\' => array(
                        array(\'value\' => \'\', \'label\' => esc_html__(\'None\')),
                        array(\'value\' => \'ID\', \'label\' => esc_html__(\'ID\')),
                        array(\'value\' => \'author\', \'label\' => esc_html__(\'Author\')),
                        array(\'value\' => \'title\', \'label\' => esc_html__(\'Title\')),
                        array(\'value\' => \'name\', \'label\' => esc_html__(\'Name\')),
                        array(\'value\' => \'date\', \'label\' => esc_html__(\'Date\')),
                        array(\'value\' => \'modified\', \'label\' => esc_html__(\'Modified\')),
                        array(\'value\' => \'rand\', \'label\' => esc_html__(\'Random\')),
                        array(\'value\' => \'menu_order\', \'label\' => esc_html__(\'Menu order\')),
                    ),
                    \'description\' => \'Choose how you want to order the Featured Pages\',
                ),
                array(
                    \'label\' => \'Order\',
                    \'attr\' => \'order\',
                    \'type\' => \'select\',
                    \'options\' => array(
                        array(\'value\' => \'\', \'label\' => esc_html__(\'\')),
                        array(\'value\' => \'ASC\', \'label\' => esc_html__(\'Ascending\')),
                        array(\'value\' => \'DESC\', \'label\' => esc_html__(\'Descending\')),
                    ),
                    \'description\' => \'You can order the pages in ascending or descending order (A-Z or Z-A)\',
                )
            ),
        )
    );
}
我在注册特色页面数组的代码外测试了foreach循环,它的输出很好。我只是有点难以从自定义帖子类型分类法“featured\\u page\\u category”中填充用于选择的选项。

在创建变量$feat\\u pages\\u terms时,我不喜欢声明获取\\u terms()。

我是否必须在shortcode ui注册之外运行此逻辑,然后将值分解为它?

1 个回复
最合适的回答,由SO网友:inarilo 整理而成

这将提前创建多维数组,然后将其分配给选项。多维数组是通过向主数组的每个元素分配一个数组来创建的。我已经重用了$feat\\u pages\\u terms变量,但您也可以使用array()创建一个单独的变量,并为其分配数组元素,我将这些行作为注释行包括在内。使用此选项时,不需要$key,因为只需添加新元素。

function shortcode_ui_feat_pages()
{
    $feat_pages_terms = get_terms(array(\'taxonomy\' => \'featured_page_category\', \'hide_empty\' => false));
    //$multiarr = array();
    foreach($feat_pages_terms as $key => $feat_page_term) :
        $feat_pages_terms[$key] = array(\'value\' => $feat_page_term->name, \'label\' => $feat_page_term->name);
        //$multiarr[] = array(\'value\' => $feat_page_term->name, \'label\' => $feat_page_term->name);
    endforeach;
    shortcode_ui_register_for_shortcode(
        \'featured-pages\',
        array(
            \'label\' => \'Featured Pages Shortcode\',
            \'listItemImage\' => \'dashicons-editor-table\',
            \'attrs\' => array(
                array(
                    \'label\' => \'Category\',
                    \'attr\' => \'category\',
                    \'type\' => \'select\',
                    \'options\' =>$feat_pages_terms/*$multiarr*/,
                    \'description\' => \'Insert the category full name (i.e. Home page)\',
                ),
...

结束

相关推荐

get_terms custom order

我正在使用get_terms 函数按字母顺序排列自定义分类法的所有术语。然而,我的客户现在要求将其中一个术语放在最后(因此不按字母顺序排列)。有人对如何实现这一目标的最佳方式有什么建议吗?