0

I am creating a WordPress options panel based on http://en.bainternet.info/2012/my-options-panel

In the article it states to call the stored options to use

//get the data to an array
$data = get_option('demo_options');
//access each field by its id
echo $data['text_field_id'];

Which works, the only problem I have is that if I want to call some information for say the header.php and the footer.php or any other page I have to include the line

$data = get_option('demo_options');

at the top of every page otherwise I can not call the data, which seems repetitive.

I have tried to create a global variable in the functions.php file like;

global $data
$data = get_option('demo_options');

but that doesn't work.

Does anyone know how I can resolve this so I don't need to add the line to the top of every page?

Thanks

4

1 回答 1

1

functions.php

$data = get_option('demo_options');

在任何其他主题模板文件 ( header.php, single.php, page.php) 中:

global $data; 
var_dump( $data );

要在其他模板文件中定义变量header.php并且不必global $var在其他模板文件中使用,必须执行以下操作:

  • 定义变量header.php
  • 而不是get_header();在其他主题模板文件中,使用include 'header.php';
  • 这样,您可以直接引用变量而无需声明global
于 2013-08-18T15:16:07.657 回答