doupafu6980 2015-04-16 11:35
浏览 22
已采纳

wp中的自定义帖子类型导致语法错误意外endif [重复]

This question already has an answer here:

I'm following this tutorial for custom post types: http://www.elegantthemes.com/blog/tips-tricks/creating-custom-post-types-in-wordpress

I'm using underscore starter wp theme. http://underscores.me/

I've created a page-movie-reviews.php file with the code bellow, but I get a syntax error on the page: Parse error: syntax error, unexpected 'endif' (T_ENDIF) in C:\wamp\www\wordpress\wp-content\themes\prototype\page-movie-reviews.php on line 34

my code:

<?php
/**
 * Template Name: Movie Reviews
 * @package prototype
 */

get_header(); ?>

    <div id="primary" class="content-area">
        <main id="main" class="site-main" role="main">




    <div class="entry-content">
     <?php
        if ( has_post_thumbnail() ) {
          the_post_thumbnail();
        }
          the_content();
        ?>
    </div>



    <?php $query = new WP_Query( array('post_type' => 'movie-reviews', 'posts_per_page' => 5 ) );
     while ( $query->have_posts() ) : $query->the_post(); ?>
    <?php endif; wp_reset_postdata(); ?>
    <?php endwhile; ?>





            <?php while ( have_posts() ) : the_post(); ?>

                <?php get_template_part( 'content', 'page' ); ?>

                <?php
                    // If comments are open or we have at least one comment, load up the comment template
                    if ( comments_open() || get_comments_number() ) :
                        comments_template();
                    endif;
                ?>

            <?php endwhile; // end of the loop. ?>








        </main><!-- #main -->
    </div><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>
</div>
  • 写回答

1条回答 默认 最新

  • doubeng9407 2015-04-16 11:42
    关注

    Usually when using a loop in WP, you need to have a condition to check if there are posts that fits your criteria. I believe you copied it from somewhere else but forgot to copy the condition that comes before the loop. All you need to do is to remove the endif; from the following code or add a if($query->have_posts()): condition before the loop.

    Tip: Make sure to use indents and break-lines in your code when needed. That way it would be much easier to locate the problem.

    <?php
    $query = new WP_Query( array('post_type' => 'movie-reviews', 'posts_per_page' => 5 ) );
    while ( $query->have_posts() ) : $query->the_post(); ?>
    <?php 
    endif; //<-- you can remove this one.
    wp_reset_postdata();
    ?> 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥70 类鸟群Boids——仿真鸟群避障的相关问题
  • ¥15 CFEDEM自带算例错误,如何解决?
  • ¥15 有没有会使用flac3d软件的家人
  • ¥20 360摄像头无法解绑使用,请教解绑当前账号绑定问题,
  • ¥15 docker实践项目
  • ¥15 数电几道习题,写出作答过程,ai一律不采用
  • ¥15 利用pthon计算薄膜结构的光导纳
  • ¥15 海康hlss视频流怎么播放
  • ¥15 Paddleocr:out of memory error on GPU
  • ¥30 51单片机C语言数码管驱动单片机为AT89C52