douqipi9704 2011-02-25 17:33
浏览 43
已采纳

将PHP代码片段插入自定义wordpress主题的理想位置是什么,因此可以广泛访问全局变量?

I'm playing around with a custom theme for Wordpress and I have this code snippet in my header, home and footer.php files.

<?php

//Get the Wordpress page_id and language for use in template items.

global $pg_id;
global $lang;

if ($_GET["page_id"] && is_numeric($_GET["page_id"])) {
    $pg_id = $_GET["page_id"];
}

$validLang = array("en", "fr");

if ($_GET["lang"] && in_array($_GET["lang"], $validLang)) {
    $lang = $_GET["lang"];
} else {
    $lang = "en";
}

//echo ($lang);
//echo ($pg_id);

?>

I should have this code in one location. Where should I place it so the global variable $lang is available in the header, footer, home and other template files?

  • 写回答

2条回答 默认 最新

  • douyanzhou1450 2011-03-02 19:56
    关注

    I couldn't get the function working properly in functions.php (although I do think that's a possible location to place the code). I ended up using a php include: http://php.net/manual/en/function.include.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示