wordpress header.php not accepting php


wordpress header.php not accepting php

在创建wordpress自定义主题时遇到了一个奇怪的问题。出于某种原因

hello <?php echo 'world'; ?>

显示为html

hello <?php echo 'world'; ?>
不是

hello world

我以前制作过很多wordpress网站,但这是第一个在亚马逊服务器上任何帮助都将非常感激。马克。

网址为lmof.uk/demo

header.php(就像stackoverflow允许我发布的那样)

<html xmlns="http://www.w3.org/1999/xhtml">
<? global $woocommerce; ?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" / >
<title><?php wp_title(''); ?></title>
<link rel="shortcut icon" href="<?=get_template_directory_uri()?>/images/favicon.png" />
<link href='http://fonts.googleapis.com/css?family=Droid+Serif' rel='stylesheet' type='text/css'>
<!--[if IE 7]><link rel="stylesheet" href="ie7.css" type="text/css" media="screen"> <![endif]-->
<? wp_enqueue_script('jquery'); ?>
<? wp_enqueue_script('bootstrapcss'); ?>
<? wp_head(); ?>
<link href="<? bloginfo('stylesheet_url')?>" media="screen" rel="stylesheet" type="text/css" />
<script type="text/javascript">
  $(document).ready(function(){
    var page = $("body").attr("id");
    $(".page-"+page).addClass("active");
  });
</script>
<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-51095016-1', 'sample4u.co.uk');
  ga('send', 'pageview');

</script>
</head>
<?php if(is_page()) { $page_slug = 'page-'.$post->post_name; } ?>

<body id="<?
  $exp = explode("?",$_SERVER['REQUEST_URI']);
  echo end(array_filter(explode("/",$exp[0]))) ?>" <?php body_class($page_slug); ?>
  data-spy="scroll" data-target="#navbar-category" data-offset="60"> 

我以为我检查过了

如果其他人有这个问题,你需要在PHP .ini文件中添加short_open_tags = on。

考虑到您后来的注释和后续代码,我假设您原来的示例实际上已经工作而不是失败了。

你的问题是你使用了短的开始标签。只有在设置了PHP的short_open_tag选项后,它们才会起作用,这就是手册中不鼓励使用它们的原因。如果你正在为别人开发代码——比如WordPress的插件或主题——你应该避免使用短标记,因为不能保证服务器设置了short_open_tag,而且最终用户可能无法打开它们。

只是为了增加信息的目的。你不需要滥用php标签。只有当你在html和php之间切换时才使用它们。当你在

上使用纯php时,你不需要在每一行上打开和关闭php标签

这一行

<? wp_enqueue_script('jquery'); ?>
<? wp_enqueue_script('bootstrapcss'); ?>
<? wp_head(); ?>

可以简化为

 <? 
   wp_enqueue_script('jquery');
   wp_enqueue_script('bootstrapcss');
   wp_head(); 
 ?>