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();
    ?> 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料