Issue with if else conditionals in foreach loop in Wordpress template
<p>I'm trying to set up a taxonomy-business-categories.php template for a Wordpress theme.</p>

<p>I have the <strong>custom post type goto_listing</strong>, with <strong>custom taxonomy business-categories</strong>. There are two levels of business-categories, eg</p>

<p>Boatyards, Marinas &amp; Moorings (parent term)</p>

<ul>
<li>Boatyards</li>
<li>Marinas</li>
<li>Moorings</li>
</ul>

<p>Cleaning (parent term)</p>

<ul>
<li>Fuel Cleaning Service</li>
<li>Laundry Services</li>
</ul>

<p>etc.</p>

<p>In my taxonomy-business-categories.php template I have a loop displaying:</p>

<p>Boats (child term)</p>

<ul>
<li>Business 1</li>
<li>Business 2 (premium listing)</li>
<li>Business 3</li>
<li>Business 4 (listing with some text manually entered in post excerpt)</li>
</ul>

<p>Marinas (child term)</p>

<ul>
<li>Business 1 (listing with some text manually entered in post excerpt)</li>
<li>Business 2 (premium listing)</li>
<li>Business 3
etc.</li>
</ul>

<p>All that is working (including links to the single listing for the business)</p>

<p>But <strong>I need to get three things to happen in the foreach loop</strong>. </p>

<p>I need to check if:</p>

<ol>
<li>it is a premium listing (this is a custom field,
goto_premium_listing) and make that a link to the single listing.</li>
<li>does the post have a manually-inserted excerpt, if so show an icon by the listing and make it toggle to show the excerpt.</li>
<li>everything else should just be displayed without any icons or links.</li>
</ol>

<p>I have achieved all three, but not altogether.</p>

<p>I can either get 1 and 3 working together or 2 and 3 working together. Never all three. I get Parse error: syntax error, unexpected 'else' (T_ELSE) or various other warnings.</p>

<p>The codes is as follows: </p>

<pre><code>&lt;?php 
$term_slug = get_query_var( 'term' );
$taxonomyName = get_query_var( 'taxonomy' );
$current_term = get_term_by( 'slug', $term_slug, $taxonomyName );
$termchildren = get_term_children( $current_term-&gt;term_id, $taxonomyName );
foreach ($termchildren as $child) {
$term = get_term_by( 'id', $child, $taxonomyName );
    $wpq = array (
    'post_type' =&gt; 'goto_listing',
    'taxonomy'=&gt;$taxonomyName,
    'term'=&gt;$term-&gt;slug,
    'order'=&gt;'asc'
    );
    $query = new WP_Query ($wpq);
    echo "&lt;h3&gt;$term-&gt;name:&lt;/h3&gt;"; 
    ?&gt;
    &lt;ul class="goto-leaders"&gt;
    &lt;?php if ($query-&gt;have_posts() ) : while ($query-&gt;have_posts() ) : $query-&gt;the_post(); ?&gt;

// BELOW IS THE IF ELSE PART THAT'S NOT WORKING ALL TOGETHER
     &lt;?php
      if (get_post_meta($post-&gt;ID, 'goto_premium_listing ', true));  {  ?&gt;
            &lt;li&gt;&lt;span&gt;&lt;a href="&lt;?php the_permalink(); ?&gt;"&gt;&lt;?php the_title();?&gt;&lt;/a&gt;&lt;/span&gt;&lt;span&gt;&lt;?php echo CFS()-&gt;get( 'goto_telephone' ); ?&gt;&lt;/span&gt;&lt;/li&gt;
    &lt;?php } 

      else { 
         if ($post-&gt;post_excerpt ) { ?&gt;
            &lt;li&gt;&lt;span&gt;&lt;a href="#" data-toggle="panel-&lt;?php echo $post-&gt;ID ; ?&gt;"&gt;&lt;i class="fa fa-arrow-circle-o-down"&gt;&lt;/i&gt; &lt;?php the_title();?&gt;&lt;/a&gt;&lt;/span&gt;&lt;span&gt;&lt;?php echo CFS()-&gt;get( 'goto_telephone' ); ?&gt;&lt;/span&gt;&lt;/li&gt;
            &lt;div id="panel-&lt;?php echo $post-&gt;ID ; ?&gt;" style="margin-top: 1rem;" class="goto-toggle hide" data-toggler=".hide"&gt;&lt;?php echo $post-&gt;post_excerpt; ?&gt;&lt;/div&gt;
    &lt;?php  } 

    else { ?&gt;
            &lt;li&gt;&lt;span&gt;&lt;?php the_title();?&gt;&lt;/span&gt;&lt;span&gt;&lt;?php echo CFS()-&gt;get( 'goto_telephone' ); ?&gt;&lt;/li&gt;
    &lt;?php } ?&gt;

    &lt;?php endwhile; endif; wp_reset_postdata(); ?&gt;
   &lt;/ul&gt;   

&lt;?php } ?&gt; &lt;!-- end foreach --&gt;
</code></pre>

<p>I think the problem is with the if else statement within the foreach loop</p>

<p>How do I fix this? </p>

<p>Tracy</p>

<p>UPDATE: Using the following code results in no error warnings but only the first if statement is acted upon, ie I get the icon and toggle text on the business with an excerpt. The rest take the final else statement, ie a plain listing, no icons, not links, nada. </p>

<pre><code> &lt;?php
        if($post-&gt;post_excerpt ){ ?&gt;
            &lt;li&gt;&lt;span&gt;&lt;a href="#" data-toggle="panel-&lt;?php echo $post-&gt;ID ; ?&gt;"&gt;&lt;i class="fa fa-arrow-circle-o-down"&gt;&lt;/i&gt; &lt;?php the_title();?&gt;&lt;/a&gt;&lt;/span&gt;&lt;span&gt;&lt;?php echo CFS()-&gt;get( 'goto_telephone' ); ?&gt;&lt;/span&gt;&lt;/li&gt;
            &lt;div id="panel-&lt;?php echo $post-&gt;ID ; ?&gt;" style="margin-top: 1rem;" class="goto-toggle hide" data-toggler=".hide"&gt;&lt;?php echo $post-&gt;post_excerpt; ?&gt;&lt;/div&gt;
    &lt;?php  } 
    else {        
        if (get_post_meta($post-&gt;ID, 'goto_premium_listing ', true)) {  ?&gt;
            &lt;li&gt;&lt;span&gt;&lt;a href="&lt;?php the_permalink(); ?&gt;"&gt;&lt;?php the_title();?&gt;&lt;/a&gt;&lt;/span&gt;&lt;span&gt;&lt;?php echo CFS()-&gt;get( 'goto_telephone' ); ?&gt;&lt;/span&gt;&lt;/li&gt;
    &lt;?php }
    else { ?&gt;
            &lt;li&gt;&lt;span&gt;&lt;?php the_title();?&gt;&lt;/span&gt;&lt;span&gt;&lt;?php echo CFS()-&gt;get( 'goto_telephone' ); ?&gt;&lt;/li&gt;
   &lt;?php }}?&gt;
</code></pre>

<p>The above codes produces this:</p>

<p><a href="https://i.stack.imgur.com/MvVea.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MvVea.jpg" alt="enter image description here"></a></p>

<p>Taking the first child cat Boatyards, you can see Clarke's Court Boatyard, which is a premium listing (with customfield of goto-premium-listing), Grenada Marina is listing with an excerpt, Spice Island Marine Services is a plain old listing.</p>

<p>Grenada Marina and Spice Island Marine Services are working correctly. Clarke's Court Boatyard is a premium listing and should be appearing the same as Spice Island Marine but with a link to the single listing page. </p>

<p>So, it's working correctly apart from putting a link on the premium listing.</p>

<p>UPDATE 2: Here is the output in html format:</p>

<pre><code>&lt;ul class="goto-leaders"&gt;
&lt;li&gt;&lt;span&gt;**Clarkes Court Boatyard**&lt;/span&gt;&lt;span&gt;439 3939&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span&gt;&lt;a href="#" data-toggle="panel-61"&gt;&lt;i class="fa fa-arrow-circle-o-down"&gt;&lt;/i&gt; Grenada Marine&lt;/a&gt;&lt;/span&gt;&lt;span&gt;443 1667&lt;/span&gt;&lt;/li&gt;
&lt;div id="panel-61" style="margin-top: 1rem;" class="goto-toggle hide" data-toggler=".hide"&gt;One-stop shop boatyard providing storage, repair and maintenance services.&lt;/div&gt;        
&lt;li&gt;&lt;span&gt;Spice Island Marine Services&lt;/span&gt;&lt;span&gt;444 4257&lt;/li&gt;
&lt;/ul&gt;  
etc
</code></pre>

<p>As you can see, it's only the permalink to the premium listing (marked in bold) that has not been actioned.     </p>