我已经花了几个小时来查看这段代码,找不到丢失的逗号,括号或引号等等。我是一个PHP初学者,但我正在尝试 - 任何帮助找到它将是惊人。我得到的错误是:
PHP Parse错误:语法错误,第235行的意外$文件结尾...
这是我的代码:
<?php
$page = get_page_by_title( 'CSA' );
$page1 = get_page_by_title( 'Co-op Members' );
$page2 = get_page_by_title( 'Recipes' );
$page3 = get_page_by_title( 'FAQs' );
$page4 = get_page_by_title( 'Contact Us' );
$page5 = get_page_by_title( 'Home' );
if ( is_page($page->ID) )
{?>
<div id="page-bg" style="background: url('<?php bloginfo('template_directory'); ?>/images/green/page-top5.jpg') no-repeat scroll center bottom #244714 !important;">
<?php
}elseif ( is_page($page1->ID) )
{?>
<div id="page-bg" style="background: url('<?php bloginfo('template_directory'); ?>/images/green/page-top2.jpg') no-repeat scroll center bottom #244714 !important;">
<?php
}elseif ( is_page($page2->ID) )
{?>
<div id="page-bg" style="background: url('<?php bloginfo('template_directory'); ?>/images/green/page-top3.jpg') no-repeat scroll center bottom #244714 !important;">
<?php
}elseif ( is_page($page3->ID) )
{?>
<div id="page-bg" style="background: url('<?php bloginfo('template_directory'); ?>/images/green/page-top4.jpg') no-repeat scroll center bottom #244714 !important;">
<?php
}elseif ( is_page($page4->ID) )
{?>
<div id="page-bg" style="background: url('<?php bloginfo('template_directory'); ?>/images/green/page-top5.jpg') no-repeat scroll center bottom #244714 !important;">
<?php
}elseif ( is_page($page5->ID) )
{?>
<div id="page-bg" style="background: url('http://ycgrown.com/wp-content/uploads/2013/03/Cherry-Tomatoes.jpg') no-repeat scroll center bottom #244714 !important;">
<?php
}else{
?>
<div id="page-bg" style="background: url('http://ycgrown.com/wp-content/uploads/2013/03/Cherry-Tomatoes.jpg') no-repeat scroll center bottom #244714 !important;">
编辑:我添加了PHP标记但仍然收到错误 - 还有其他想法?
答案 0 :(得分:1)
你缺少PHP标签:
{?>
<div id="page-bg" style="background: url('http://xyz.com/wp-content/uploads/2013/03/Cherry-Tomatoes.jpg') no-repeat scroll center bottom #244714 !important;">
<?php
}else{
?>
<div id="page-bg" style="background: url('http://xyz.com/wp-content/uploads/2013/03/Cherry-Tomatoes.jpg') no-repeat scroll center bottom #244714 !important;">
答案 1 :(得分:1)
您还缺少其他条件的右括号
}else{
?>
<div id="page-bg" style="background: url('http://ycgrown.com/wp-content/uploads/2013/03/Cherry-Tomatoes.jpg') no-repeat scroll center bottom #244714 !important;">
<?php
}
?>
答案 2 :(得分:0)
您可以采取一些措施来整理此代码。理想情况下,开始时的代码将放在控制器中,然后渲染将主要是带有一些PHP代码段的HTML。
<?php
// This would be best in a controller class, but for now we'll enclose
// it in its own little section
$page = get_page_by_title( 'CSA' );
$page1 = get_page_by_title( 'Co-op Members' );
$page2 = get_page_by_title( 'Recipes' );
$page3 = get_page_by_title( 'FAQs' );
$page4 = get_page_by_title( 'Contact Us' );
$page5 = get_page_by_title( 'Home' );
?>
<!-- This is your 'view layer', so keep your PHP small and concise -->
<?php if ( is_page($page->ID) ): ?>
<div
id="page-bg"
class="bgnd"
style="background-image: url('<?php bloginfo('template_directory') ?>/images/green/page-top5.jpg');"
>
<?php elseif ( is_page($page1->ID) ): ?>
<div
id="page-bg"
class="bgnd"
style="background-image: url('<?php bloginfo('template_directory') ?>/images/green/page-top2.jpg');"
>
<?php elseif ... ?>
因此,每个PHP if
或elseif
(或实际上任何语句)只占用一行,这样可以更容易地查看缩进是否匹配。请注意,虽然循环/构造的括号形式是“适当的”PHP的首选,其中PHP用于视图层,我更喜欢冒号形式,因为它更紧凑,并且最终描述比扫描读取更容易闭幕式。
此外,我已将您的内联background
规则更改为background-image
,并建议您设置“bgnd”类:
.bgnd { background: no-repeat scroll center bottom #244714 !important; }
这将使事情更加干燥(不要重复自己),从而节省您的时间和精力。
最后我缩进了div
s,所以他们的属性是每行一个。这是一个品味问题,但它更具可读性,并且仍然完全有效。它还具有以下优点:您需要在IDE中执行更少/不需要水平滚动,这可以使调试时的生活更轻松。