mercredi 23 mars 2016

Issue with if else conditionals in foreach loop in Wordpress template

I'm trying to set up a taxonomy-business-categories.php template for a Wordpress theme.

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

Boatyards, Marinas & Moorings (parent term)

  • Boatyards
  • Marinas
  • Moorings

Cleaning (parent term)

  • Fuel Cleaning Service
  • Laundry Services

etc.

In my taxonomy-business-categories.php template I have a loop displaying:

Boats (child term)

  • Business 1
  • Business 2 (premium listing)
  • Business 3
  • Business 4 (listing with some text manually entered in post excerpt)

Marinas (child term)

  • Business 1 (listing with some text manually entered in post excerpt)
  • Business 2 (premium listing)
  • Business 3 etc.

All that is working (including links to the single listing for the business)

But I need to get three things to happen in the foreach loop.

I need to check if:

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

I have achieved all three, but not altogether.

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.

The codes is as follows:

<?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->term_id, $taxonomyName );
foreach ($termchildren as $child) {
$term = get_term_by( 'id', $child, $taxonomyName );
    $wpq = array (
    'post_type' => 'goto_listing',
    'taxonomy'=>$taxonomyName,
    'term'=>$term->slug,
    'order'=>'asc'
    );
    $query = new WP_Query ($wpq);
    echo "<h3>$term->name:</h3>"; 
    ?>
    <ul class="goto-leaders">
    <?php if ($query->have_posts() ) : while ($query->have_posts() ) : $query->the_post(); ?>

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

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

    else { ?>
            <li><span><?php the_title();?></span><span><?php echo CFS()->get( 'goto_telephone' ); ?></li>
    <?php } ?>

    <?php endwhile; endif; wp_reset_postdata(); ?>
   </ul>   

<?php } ?> <!-- end foreach -->

I think the problem is with the if else statement within the foreach loop

How do I fix this?

Tracy

Aucun commentaire:

Enregistrer un commentaire