我在页id和分类id之间做了一个映射,这是几个地方使用的。
然而,我读过从这个岗位上,如果可以避免的话,就不应该使用全局变量。但是,我认为这是明智的,作为一个全局变量,所以它不是一个应该执行几次(每次迭代几个ID)的函数。
但我不知道该怎么做。我得到的是:
/**
* Should this be here, in order to make it global?
*/
$page_category_mapping = array();
global $page_category_mapping;
/**
* Building the mapping...
*
* Should this tage the $page_category_mapping
* as input?
*/
function build_page_category_mapping(){
// Going over all pages and maps them to a category
}
add_action( 'init', 'build_page_category_mapping' );
/**
* I assume this function should take the array as input, right?
*/
function a_funtion_that_uses_the_mapping( $page_category_mapping ){
echo '发布于 2018-04-20 07:25:33
我自己想出来的。
function generate_the_excluded_array() {
global $excluded_pages;
$excluded_pages = array('Foo', 'Bar');
}
add_action( 'after_setup_theme', 'generate_the_excluded_array' );然后可以使用global‘导入’,如下所示:
function example_shortcode_definition() {
global $excluded_pages;
echo $excluded[0];
}
add_shortcode( 'example_shortcode', 'example_shortcode_definition' );https://wordpress.stackexchange.com/questions/301159
复制相似问题