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 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?