【发布时间】:2016-12-02 06:03:11
【问题描述】:
我正在开发一个网站。我正在使用 page.php 来控制所有页面,即使它们具有不同的类别。在我的本地服务器(XAMPP)上一切正常,我可以查看所有页面,但是当我在线上传时,尝试查看页面时出现解析错误。
我不想为所有页面创建单独的文件,所以我决定使用 if ( is_page( page_id ) 将所有文件放在 page.php 中
这是我得到的错误:
解析错误:语法错误,第 1 行 /----/woodclef.com/wp-content/themes/woodclef/page.php 中出现意外的 'endwhile' (T_ENDWHILE)
通过以下方式查看网站:www.woodclef.com 密码为 12345678
已编辑
我现在有一个更清晰的代码,并且错误消失了,但即使类别 ID 和页面 ID 为真,也没有显示任何帖子。任何人都可以帮助找出可能是什么问题?
<?php
//Highlights
if ( is_page( 10 ) ) {
$args = array(
'posts_per_page' => -1,
'cat' => 5
);
$sticky_query = new WP_Query( $args );
$count = 0;
while ( $sticky_query->have_posts() ) : $sticky_query->the_post();
$count++;
if ($count == 1) :
get_template_part( 'pages/common_template_a' );
elseif ($count == 2) :
get_template_part( 'pages/common_template_a' );
else :
get_template_part( 'pages/common_template' );
endif;
endwhile;
wp_reset_postdata();
}
//News
if ( is_page( 144 ) ) {
$args = array(
'posts_per_page' => -1,
'cat' => 6
);
$sticky_query = new WP_Query( $args );
$count = 0;
while ( $sticky_query->have_posts() ) : $sticky_query->the_post();
$count++;
if ($count == 1) :
get_template_part( 'pages/common_template_a' );
elseif ($count == 2) :
get_template_part( 'pages/common_template_a' );
else :
get_template_part( 'pages/common_template' );
endif;
endwhile;
wp_reset_postdata();
}
?>
【问题讨论】:
-
我推荐使用像 laravel 这样的 php famework。使用路由器
-
你为什么总是打开和关闭php标签?这是完全没有必要的,只会导致错误。
-
@CharlotteDunois 我该怎么办?你的意思是我应该删除顶部的开始标签和下面的结束标签?
-
删除中间的那些(那里有很多)。如果您在最后一个 php 结束标记之后没有其他任何内容,您也可以将其删除。通常只需要一个 php 开始标签,即顶部的那个。
-
您知道
is_page( 10 )和is_page( 144 )将是真的,并且您的所有循环都将在每个请求上执行,只要这些ID 作为数据库中的页面存在?如果我理解正确,我认为您的意思是检查当前请求是否是这些 ID 中的任何一个,然后执行您的 while 循环?
标签: php wordpress if-statement