在一个数组中设置多个选项,并使它们在Wordpress中可访问


Set multiple options in an array and make them accessible in Wordpress

我想创建一些变量/选项来自定义我的WordPress主题。

目前我已经在functions.php文件中设置了 5 个变量:

$isBoxedLayout = true;
$postCounter = 0;
$postDate = false;
$socialButtons;
$showCatLink = false;

我通过其他文件(如header.phpcontent.php等)访问它们:

global $isBoxedLayout;
global $postCounter;

在文件的顶部。我认为最好只有一个数组来存储所有内容!?但是我该怎么做呢?

另外,有人知道如何访问template-tags.php文件中functions.php中设置的变量吗?

我已经试过了:

include '../functions.php'

但是我收到以下错误:

include(../functions.php): failed to open stream: No such file or directory

当我想访问像content.php这样的文件中的变量时,它运行良好。区别在哪里?

也许不完全是你正在寻找的答案,但我会看看选项 API。它将您的选项保存到数据库中,并且非常易于使用。

// Functions.php
add_option( 'isBoxedLayout', true, '', 'yes' );

// template-tags.php
$isBoxedLayout = get_option( 'isBoxedLayout' );