title: Wordpress PHP loop custom post type and display on homepage
body: <p>I'm currently working on a WordPress site and I've created a custom post type called 'events' using the CPT UI plugin.</p>
  <p>I want to display the events on my home page, so I've tried to create a loop in my homepage template in the theme files. I've been using this as a guide <a href="https://www.wpbeginner.com/wp-tutorials/how-to-create-custom-post-types-in-wordpress/" rel="nofollow noreferrer">https://www.wpbeginner.com/wp-tutorials/how-to-create-custom-post-types-in-wordpress/</a></p>
  <p>but for the life of me, I can't get the PHP that is used in that link to work for me.</p>
  <pre><code>&lt;?PHP 
    $args = array( 'post_type' =&gt; 'events', 'posts_per_page' =&gt; 4 );
    $the_query = new WP_Query( $args ); 
  ?&gt;
  &lt;?php if ( $the_query-&gt;have_posts() ) : ?&gt;
  &lt;?php while ( $the_query-&gt;have_posts() ) : $the_query-&gt;the_post(); ?&gt;
    &lt;h2&gt;&lt;?php the_title(); ?&gt;&lt;/h2&gt;
    &lt;div class=&quot;entry-content&quot;&gt;
      &lt;?php the_content(); ?&gt; 
    &lt;/div&gt;
  &lt;?php wp_reset_postdata(); ?&gt;
  &lt;?php else:  ?&gt;
    &lt;p&gt;&lt;?php _e( 'Sorry, no posts matched your criteria.' ); ?&gt;&lt;/p&gt;
  &lt;?php endif; ?&gt;
  </code></pre>
  <p>When I try to save that I get this error</p>
  <pre><code>syntax error, unexpected 'else' (T_ELSE)
  </code></pre>
  <p>I've been searching for an answer for this for a while and I can't find anything.</p>
  <p>I'm pretty new to PHP, so sorry if I'm being incredibly stupid. Any help would be appreciated :)</p>