douqipi9704
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
    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

    点赞 评论
  • doukoumi3389
    doukoumi3389 2011-02-25 19:31

    Place it in the functions.php file of your theme, that is for all the shared code and function of the theme.

    This file is loaded during wordpress installation and the functions defined here are visible both to the admin and the theme.

    See here for a deeper explaination.

    点赞 评论

相关推荐