我在header.php中有以下代码来回显正文的id: body id =“?php echo $body;?”它取自index.php中的变量:$body =“home”;输出结果为:body id =“”有什么建议来解决这个问题(我创建了var_dump($body),值为...
我在header.php中有以下代码来回显正文的id:< body id =“<?php echo $body;?>”>它取自index.php中的变量:$body =“home”;
输出结果为:body id =“”
有什么建议来解决这个问题
(我创建了var_dump($body),值为“home”,因此变量正常工作)
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 -->
index.php文件:
<?php
/**
* The main template file.
*
* 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的包含是由get_header()函数完成的,所以header.php文件中的$body变量包含在该函数的范围内(即它是函数中的局部变量).
您需要在header.php文件中将变量声明为全局变量.在对变量执行任何其他操作之前,请将此行添加到header.php:
global $body;
我相信这应该有助于实现您正在寻找的行为.
请参阅以下参考:
PHP Variable Scope
WordPress – Stepping Into Templates
本文标题为:PHP变量在WordPress标题和索引文件中不起作用?
基础教程推荐
- pbootcms二次开发必须要了解的后台目录结构 2023-07-09
- 织梦dedecms如何在dede:sql中使用[field:global.autoindex/] 2022-08-27
- dedecms织梦无需登录注册可下单购买的修改 2022-11-04
- 织梦dedecms调用当前栏目文章数的方法 2022-11-08
- pbootcms网站自动清理runtime缓存方法 2023-07-09
- 织梦DedeCMS搜索指定多个栏目文档的办法 2022-11-11
- dedecms根据来访IP区域自动跳转对应页面的方法 2022-07-21
- dedecms织梦全局变量调用方法总结 2023-07-08
- PbootCMS网站标题描述等标签限制字数的办法 2023-07-08
- dedecms织梦cms常用判断语句汇总 2022-06-24