我目前正在一个WordPress网站上工作,我已经使用CPT UI插件创建了一个名为'events'的自定义帖子类型。
我想在我的主页上显示事件,所以我尝试在主题文件中的主页模板中创建一个循环。 我一直将此作为指南https://www.wpbeginner.com/wp-tutorials/how-to-create-custom-post-types-in-wordpress/
但是为了我的生活,我不能得到PHP,这是在该链接中使用的为我工作。
<?PHP
$args = array( 'post_type' => 'events', 'posts_per_page' => 4 );
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<div class="entry-content">
<?php the_content(); ?>
</div>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
当我试图保存时,我得到了这个错误
syntax error, unexpected 'else' (T_ELSE)
我已经为此寻找了一段时间的答案,但什么也找不到。
我对PHP还是个新手,如果我太笨了,我很抱歉。 如有任何帮助,将不胜感激:)
您没有end while循环,将此代码也放置在
<?php
$args = array( 'post_type' => 'events', 'posts_per_page' => 4 );
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<div class="entry-content">
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>