Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
111 views
in Technique[技术] by (71.8m points)

php wordpress processing inside the hook and synchronizing the value to the variable outside the hook

what I want is that I have a special taxonomy and get_terms does not work without it being loaded, naturally the only way I can get it is to hook up to "init". But when this is the case, I will have to repeat this. I do not want this.

As you can see in the code below, I am doing the operation in init and trying to transfer it to "$ new_array". How can I do it?

    protected function get_reactions()
    {
        $new_array = array();
        
        add_action( 'init', function() use ( &$new_array ) {
            $reactions = get_terms( array(
                'taxonomy' => 'bp_reaction',
                'hide_empty' => false
            ));
    
            foreach ( $reactions as $value ) {
                $priority = get_option( 'taxonomy_'.$value->term_id.'_priority' );
                $image    = get_option( 'taxonomy_'.$value->term_id.'_image' );
                $new_array[$priority] = (object) array(
                    'id'       => $value->term_id,
                    'priority' => $priority,
                    'slug'     => $value->slug,
                    'name'     => $value->name,
                    'image'    => $image
                );
            }
        }, 9 );

        // Sort from largest to small
        krsort( $new_array );

        return $new_array;
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...