添加foreach功能

时间:2015-03-07 03:27:30

标签: php wordpress

我正在尝试使用以下函数编辑wordpress php文件:

add_action('userpro_after_profile_head','userpro_sc_bar', 99);
    function userpro_sc_bar( $args ) {
        global $userpro, $userpro_social;
        extract($args);

        ( display content buttons here )
}

我正在尝试从另一个插件向此函数添加一个按钮,我需要添加以下全局规则:

foreach ( $sellers['users'] as $seller ) {
                    $store_info = get_store_info( $seller->ID );
                    $store_url  = get_store_url( $seller->ID );
}

当我将两行$store_info$store_url添加到全局规则时,它有效,但我没有获得使用foreach定义的$卖家ID参数

当我将整个事物添加到这样的全局规则中时:

add_action('userpro_after_profile_head','userpro_sc_bar', 99);
    function userpro_sc_bar( $args ) {
        global $userpro, $userpro_social;
        extract($args);
           foreach ( $sellers['users'] as $seller ) {
                    $store_info = get_store_info( $seller->ID );
                    $store_url  = get_store_url( $seller->ID );
           }
                   ( display content buttons here )
    }

它不再有效了。

将这两个结合在一起是否合适?

2 个答案:

答案 0 :(得分:0)

我对wordpress不太熟悉...... 但这可能是$sellers变量的问题吗?

我认为无法从您的函数中访问$sellers变量。

假设$sellers是全局变量,您可以尝试:

add_action('userpro_after_profile_head','userpro_sc_bar', 99);
    function userpro_sc_bar( $args ) {
        global $userpro, $userpro_social, $sellers; // <-- Added $sellers
        extract($args);
           foreach ( $sellers['users'] as $seller ) {
                    $store_info = get_store_info( $seller->ID );
                    $store_url  = get_store_url( $seller->ID );
           }
                   ( display content buttons here )
    }

答案 1 :(得分:0)

好的,得到的是,这对我有用:

add_action('userpro_after_profile_head','userpro_sc_bar', 99);
    function userpro_sc_bar( $args ) {
        global $userpro, $userpro_social, $sellers; // <-- Added $sellers
        extract($args);
        $sellers = get_sellers(); // <-- Get $sellers
           foreach ( $sellers['users'] as $seller ) {
                    $store_info = get_store_info( $seller->ID );
                    $store_url  = get_store_url( $seller->ID );
           }
                   ( display content buttons here )
    }
相关问题