首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >创建(全局)-mapping

创建(全局)-mapping
EN

WordPress Development用户
提问于 2018-04-18 14:42:29
回答 1查看 26关注 0票数 0

我在页id和分类id之间做了一个映射,这是几个地方使用的。

然而,我读过从这个岗位上,如果可以避免的话,就不应该使用全局变量。但是,我认为这是明智的,作为一个全局变量,所以它不是一个应该执行几次(每次迭代几个ID)的函数。

但我不知道该怎么做。我得到的是:

代码语言:javascript
复制
/**
 * 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 '
EN

回答 1

WordPress Development用户

回答已采纳

发布于 2018-04-20 07:25:33

我自己想出来的。

代码语言:javascript
复制
function generate_the_excluded_array() {

  global $excluded_pages;
  $excluded_pages = array('Foo', 'Bar');
}
add_action( 'after_setup_theme', 'generate_the_excluded_array' );

然后可以使用global‘导入’,如下所示:

代码语言:javascript
复制
function example_shortcode_definition() {

  global $excluded_pages;
  echo $excluded[0];
}
add_shortcode( 'example_shortcode', 'example_shortcode_definition' );
票数 0
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/301159

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档