2010-08-31 78 views
1

我想包括localization.phpheader.php但它只適用於當我將它包含在index.php(見下文)。我認爲包括它在header.php將使localization.php在每個文件中工作。 (localization.php具有包含lang文件以顯示語言的功能)。PHP包括在Wordpress的index.php文件而不是在header.php文件中的作品?

有什麼建議?

它的工作原理是這樣的:

的index.php

* 
* This is the most generic template file in a WordPress theme 
* and one of the two required files for a theme (the other being style.css). 
* It is used to display a page when nothing more specific matches a query. 
* E.g., it puts together the home page when no home.php file exists. 
* Learn more: http://codex.wordpress.org/Template_Hierarchy 
* 
* @package WordPress 
* @subpackage Starkers 
* @since Starkers 3.0 
*/ 
$body = "home"; 
include_once 'localization.php'; 
get_header(); ?> 
<div id="content"> 
    <div class="container"> 
     <div id="mainbar"> 
      <?php 
      /* Run the loop to output the posts. 
      * If you want to overload this in a child theme then include a file 
      * called loop-index.php and that will be used instead. 
      */ 
      get_template_part('loop', 'index'); 
      ?> 
      <p><?php echo l('test'); ?></p> 
     </div> 
     <?php get_sidebar(); ?> 
    </div><!-- .container --> 
</div><!-- #main-content --> 
<?php get_footer(); ?> 

的header.php

<?php 
/** 
* The Header for our theme. 
* 
* Displays all of the <head> section and everything up till <div id="main"> 
* 
* @package WordPress 
* @subpackage Starkers 
* @since Starkers 3.0 
*/ 
?><!DOCTYPE html> 
<html <?php language_attributes(); ?>> 
<head> 
<meta charset="<?php bloginfo('charset'); ?>" /> 
<title><?php 
    /* 
    * Print the <title> tag based on what is being viewed. 
    * We filter the output of wp_title() a bit -- see 
    * twentyten_filter_wp_title() in functions.php. 
    */ 
    wp_title('|', true, 'right'); 

    ?></title> 
<link rel="profile" href="http://gmpg.org/xfn/11" /> 
<link rel="stylesheet" type="text/css" media="all" href="<?php bloginfo('stylesheet_url'); ?>" /> 
<link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" /> 
<?php 
    /* We add some JavaScript to pages with the comment form 
    * to support sites with threaded comments (when in use). 
    */ 
    if (is_singular() && get_option('thread_comments')) 
     wp_enqueue_script('comment-reply'); 

    /* Always have wp_head() just before the closing </head> 
    * tag of your theme, or you will break many plugins, which 
    * generally use this hook to add elements to <head> such 
    * as styles, scripts, and meta tags. 
    */ 
    wp_head(); 
?> 
</head> 

<!--<body <?php body_class(); ?>>--> 
<body id="<?php echo $body; ?>"> 
<div id="header"> 
    <div class="container"> 
     <div id="header-top"> 
      <h1> 
       <a href="<?php echo home_url('/'); ?>" title="<?php echo esc_attr(get_bloginfo('name', 'display')); ?>" rel="home"><?php bloginfo('name'); ?></a> 
      </h1> 
      <!--<p><?php bloginfo('description'); ?></p>--> 

      <div id="access" role="navigation"> 
       <?php /* Allow screen readers/text browsers to skip the navigation menu and get right to the good stuff */ ?> 
       <!--<a href="#content" title="<?php esc_attr_e('Skip to content', 'twentyten'); ?>"><?php _e('Skip to content', 'twentyten'); ?></a>--> 
       <?php /* Our navigation menu. If one isn't filled out, wp_nav_menu falls back to wp_page_menu. The menu assiged to the primary position is the one used. If none is assigned, the menu with the lowest ID is used. */ ?> 
       <?php wp_nav_menu(array('container_class' => 'menu-header', 'theme_location' => 'primary')); ?> 
      </div><!-- #access --> 
      <ul id="lang"> 
       <li <?php if($lang_file=='lang.en.php') {echo 'class="current"';} ?>><a href="index.php?lang=en">ENGLISH</a></li> 
       <li <?php if($lang_file=='lang.zh-tw.php') {echo 'class="current"';} ?>><a href="index.php?lang=zh-tw">CHINESE</a></li> 
      </ul> 
     </div> 
    </div><!-- .container --> 
</div><!-- #header --> 

localization.php

<?php 
session_start(); 
header('Cache-control: private'); // IE 6 FIX 

if(isSet($_GET['lang'])) { 
    $lang = $_GET['lang']; 

    // register the session and set the cookie 
    $_SESSION['lang'] = $lang; 
    setcookie("lang", $lang, time() + (3600 * 24 * 30)); 
} 
else if(isSet($_SESSION['lang'])) { 
    $lang = $_SESSION['lang']; 
} 
else if(isSet($_COOKIE['lang'])) { 
    $lang = $_COOKIE['lang']; 
} 
else { 
    $lang = 'en'; 
} 

// use appropiate lang.xx.php file according to the value of the $lang 
switch ($lang) { 
case 'en': 
    $lang_file = 'lang.en.php'; 
    break; 

case 'es': 
    $lang_file = 'lang.es.php'; 
    break; 

case 'zh-tw': 
    $lang_file = 'lang.zh-tw.php'; 
    break; 

case 'zh-cn': 
    $lang_file = 'lang.zh-cn.php'; 
    break; 

default: 
    $lang_file = 'lang.en.php'; 
} 

//translation helper function 
function l($localization) { 
    global $lang; 
    return $lang[$localization]; 
} 

    include_once 'languages/'.$lang_file; 
?> 

(我認爲localization.php是不言自明)

它不會像這樣工作:

的header.php:

<?php 
/** 
* The Header for our theme. 
* 
* Displays all of the <head> section and everything up till <div id="main"> 
* 
* @package WordPress 
* @subpackage Starkers 
* @since Starkers 3.0 
*/ 
include_once 'localization.php'; 
?><!DOCTYPE html> 
<html <?php language_attributes(); ?>> 
<head> 
<meta charset="<?php bloginfo('charset'); ?>" /> 
<title><?php 
    /* 
    * Print the <title> tag based on what is being viewed. 
    * We filter the output of wp_title() a bit -- see 
    * twentyten_filter_wp_title() in functions.php. 
    */ 
    wp_title('|', true, 'right'); 

    ?></title> 
<link rel="profile" href="http://gmpg.org/xfn/11" /> 
<link rel="stylesheet" type="text/css" media="all" href="<?php bloginfo('stylesheet_url'); ?>" /> 
<link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" /> 
<?php 
    /* We add some JavaScript to pages with the comment form 
    * to support sites with threaded comments (when in use). 
    */ 
    if (is_singular() && get_option('thread_comments')) 
     wp_enqueue_script('comment-reply'); 

    /* Always have wp_head() just before the closing </head> 
    * tag of your theme, or you will break many plugins, which 
    * generally use this hook to add elements to <head> such 
    * as styles, scripts, and meta tags. 
    */ 
    wp_head(); 
?> 
</head> 

<!--<body <?php body_class(); ?>>--> 
<body id="<?php echo $body; ?>"> 
<div id="header"> 
    <div class="container"> 
     <div id="header-top"> 
      <h1> 
       <a href="<?php echo home_url('/'); ?>" title="<?php echo esc_attr(get_bloginfo('name', 'display')); ?>" rel="home"><?php bloginfo('name'); ?></a> 
      </h1> 
      <!--<p><?php bloginfo('description'); ?></p>--> 

      <div id="access" role="navigation"> 
       <?php /* Allow screen readers/text browsers to skip the navigation menu and get right to the good stuff */ ?> 
       <!--<a href="#content" title="<?php esc_attr_e('Skip to content', 'twentyten'); ?>"><?php _e('Skip to content', 'twentyten'); ?></a>--> 
       <?php /* Our navigation menu. If one isn't filled out, wp_nav_menu falls back to wp_page_menu. The menu assiged to the primary position is the one used. If none is assigned, the menu with the lowest ID is used. */ ?> 
       <?php wp_nav_menu(array('container_class' => 'menu-header', 'theme_location' => 'primary')); ?> 
      </div><!-- #access --> 
      <ul id="lang"> 
       <li <?php if($lang_file=='lang.en.php') {echo 'class="current"';} ?>><a href="index.php?lang=en">ENGLISH</a></li> 
       <li <?php if($lang_file=='lang.zh-tw.php') {echo 'class="current"';} ?>><a href="index.php?lang=zh-tw">CHINESE</a></li> 
      </ul> 
     </div> 
    </div><!-- .container --> 
</div><!-- #header --> 

回答

0

關閉頂部我頭,我敢打賭,這是一個路徑問題。您的包含與執行腳本相關,而不是包含包含,如果這是有意義的。

+0

我意識到localization.php正在工作,但lang.xx.php文件不起作用。 Wierd,index.php和header.php都在同一個文件夾中有localization.php。我怎樣才能解決這個問題? – alexchenco 2010-08-31 02:23:45

+0

語言文件在哪裏?他們在一個名爲'語言'的文件夾中?然後你需要將它包含在你的路徑定義中。 – kevtrout 2010-08-31 02:26:47

+0

該文件夾只是索引本地化和頭文件php文件(主題/ starkers/localization.php)下的一個級別。但爲什麼該文件在index.php上工作?如果將它包含在header.php中,我需要路徑定義? – alexchenco 2010-08-31 02:29:48

0

我試着在我的服務器上重複你的問題,但是本地化文件包含在header.php中時。

+0

so localizatoin.php在header.php上工作?你究竟在哪個部分包含了它?我的工作原理只是在/ language文件夾內的lang.xx.php文件不起作用(低一級)。 – alexchenco 2010-08-31 02:27:23

+0

我把它包含在你所顯示的文檔類型的正上方。我想也許它必須遵循doctype,但它工作得很好。嘗試圍繞文件名包括括號:'include_once('localization.php');'。它在我的工作沒有他們,但也許你的Web服務器喜歡括號。 – kevtrout 2010-08-31 11:10:15