ok so i have this if statement it is in the header.php
<?php
if (is_page('estate planning')) {
$estate = array ("eplnp","1","2","3","4","5");}
elseif(is_page('probate & trust administration')){
$trust = array ("eplnp","1","2","3","4","5");}
elseif(is_page('elder law')){
$elder = array ("eplnp","1","2","3","4","5");}
elseif(is_page('meet sara')){
$meet = array ("eplnp","1","2","3","4","5");}
elseif(is_page('blog')){
$blog = array ("eplnp","1","2","3","4","5");}
elseif(is_page('client forms')){
$form = array ("eplnp","1","2","3","4","5");}
elseif(is_page('request a consultation')){
$request = array ("eplnp","1","2","3","4","5");}
else {
$home = array ("homebg","hmline1","hmline","phone","home","blurb-hm");}
?>
我将如何全局使用此语句...就像我希望它应用于index.php header.php和footer.php。
我意识到我可以将它保存为bla.php并在所有页面上使用include()。 是否有一种更有效的方法,例如从一个页面全局加载它,这样代码看起来更整洁,可以在当前加载的页面上使用?
答案 0 :(得分:1)
使用include('bla.php')
包含文件是最有效的方法。这是包含块的一行代码,你不会重复自己。
还有。 Voitcus评论,你应该使用开关而不是if来简化这个块。获取页面的名称作为变量,然后执行:
switch ($page) {
case "estate planning":
$estate = array ("eplnp","1","2","3","4","5");
break;
case "probate & trust administration":
$elder = array ("eplnp","1","2","3","4","5");}
break;
...
default:
$home = array ("homebg","hmline1","hmline","phone","home","blurb-hm");
}
答案 1 :(得分:0)
您可以在wp_head()之前在文件头中包含文件;并使用任何地方 你的变量。
在所选主题文件夹中添加文件。
require_once (TEMPLATEPATH . '/bla.php');
wp_head();