【问题标题】:How Should I have Constructed this page.php file?我应该如何构建这个 page.php 文件?
【发布时间】: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


【解决方案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;

 } 
 ?>

【讨论】:

  • 感谢@LalKumarRai 指出我的错误。代码现在更干净了,但即使类别 ID 和页面 ID 为真,也不会显示任何帖子。你能帮我找出原因吗?
  • 回声 "
    "; print_r($sticky_query );echo "
    ";die();在 $sticky_query = new WP_Query( $args ); 下方使用上面的代码并检查帖子
  • 我这样做了,但没有返回任何帖子
  • 这意味着没有与该类别相关的帖子或有问题
【解决方案2】:

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

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

【讨论】:

  • 它不起作用,先生。我试过了,它显示:if ( is_page( 10 ) ) { echo "hello"; }
  • 你是否在你的论点中添加了 post_type 并尝试过?
猜你喜欢
  • 1970-01-01
  • 2022-12-18
  • 2021-05-25
  • 2011-10-13
  • 1970-01-01
  • 2011-03-09
  • 1970-01-01
  • 1970-01-01
  • 2012-08-16
相关资源
最近更新 更多