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

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>

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();
?>