2016-12-02 15 views
2

我正在一个网站上工作。我是usng page.php来控制所有页面,即使它们有不同的类别。在我的本地服务器(XAMPP)上一切正常,我可以查看所有页面,但是当我在线上传时,在尝试查看页面时出现解析错误。我该如何构建这个page.php文件?

我不想创建的所有网页单个文件,所以我决定所有page.php文件使用if (is_page(page_id)

这是我得到了错误的地方:

解析错误:语法错误,第一行中/----/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(); 
} 
?> 
+0

我重新使用php的famework像laravel。使用路由器 –

+3

为什么你总是打开和关闭php标签?这是绝对没有必要的,只会导致错误。 –

+0

@CharlotteDunois我该怎么办?你的意思是我应该删除顶部的开标签和下面的标签? – Sam

回答

1

我建议你写一个干净的代码,看看是否有任何错误。 尝试下面的代码在您的page.php文件

<?php 
//Highlights 
if (is_page(10)) {   

     $args = array(
      'posts_per_page' => -1, 
      'cat' => 5 
     ); 
     $sticky_query = new WP_Query($args); 
     $count = 0; 
     if($sticky_query->have_posts()): 
     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(); 
     endif;   
} 


//News 
if (is_page(144)) {   

     $args = array(
      'posts_per_page' => -1, 
      'cat' => 6 
     ); 
     $sticky_query = new WP_Query($args); 
     $count = 0; 
     if($sticky_query->have_posts()): 
     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();  
     endif; 

} 
?> 
+0

谢谢@LalKumarRai指出我的错误。代码现在比较干净,但即使类别ID和页面ID为真,也不会显示帖子。你能帮我弄清楚为什么? – Sam

+0

尝试查看从查询返回的对象... –

+0

echo“

"; print_r($sticky_query);echo "
”; die();使用上面的代码就在$ sticky_query = new WP_Query($ args);并检查帖子 –

0

在WP_Query的参数传递post_type可能会解决这个问题。

$args = array(
     'post_type' =>'post', 
     'posts_per_page' => -1, 
     'cat' => 5 
    ); 
+0

它没有工作先生。我试过这个,它显示:'if(is_page(10)){ \t \t \t echo“hello”; \t \t \t}' – Sam

+0

您是否在参数中添加了post_type并尝试过? –

+0

是的,但没有奏效 – Sam