-2

can you help me to add an else statement to this loop?

<?php
$post_type = 'post'; // <-- Post Type
$tax = 'temporada'; // <-- Taxonomía
$termino = get_terms($tax);
$category = get_the_category(); 
$cat_name = $category[0]->cat_ID;
  if ($termino) {
    foreach ($termino as $temporada) {
      $args=array(
       'post_type' => $post_type,
       "$tax" => $temporada->slug,
       'cat' => $cat_name,
       'post_status' => 'publish',
       'orderby'   => 'meta_value', 
       'meta_query' => array( array('key' => 'numeroepisodio') ),
       'hide_empty' => true,
       'order' => ASC
    );
 $query_episodios = null;
 $query_episodios = new WP_Query($args);
   if( $query_episodios->have_posts() ) {
  echo '<h3>title...</h3>';
  while ($query_episodios->have_posts()) : $query_episodios->the_post();?>
      <a href="<?php the_permalink() ?>"><li><strong>[...]</strong></a>

<?php endwhile; } 
    echo '</ul>';
   wp_reset_query(); 
   } 
  }
?>

Thanks!

1 Answer 1

1

You arent clear where you want your else statement, so Ive added one for each of the if's you included.

<?php
    $post_type = 'post'; // <-- Post Type
    $tax = 'temporada'; // <-- Taxonomía
    $termino = get_terms($tax);
    $category = get_the_category(); 
    $cat_name = $category[0]->cat_ID;
    if ($termino) {
        foreach ($termino as $temporada) {
            $args=array(
               'post_type' => $post_type,
               "$tax" => $temporada->slug,
               'cat' => $cat_name,
               'post_status' => 'publish',
               'orderby'   => 'meta_value', 
               'meta_query' => array( array('key' => 'numeroepisodio') ),
               'hide_empty' => true,
               'order' => ASC
            );
            $query_episodios = null;
            $query_episodios = new WP_Query($args);
            if( $query_episodios->have_posts() ) {
                echo '<h3>title...</h3>';
                while ($query_episodios->have_posts()) : $query_episodios->the_post(); ?>
                    <a href="<?php echo the_permalink() ?>"><li><strong>[...]</strong></a>
                <?php endwhile; 
            } else {
                // do something
            }
            echo '</ul>';
            wp_reset_query(); 
        } 
    } else {
        // do something
    }
?>
3
  • i need the second else statement, i add this, but this does work :/
    – Ferrmolina
    Commented Feb 21, 2015 at 2:59
  • I used your exact code, I just formatted it in a more easy to digest fashion, and added the elses. I did find one error, missing echo on <?php the_permalink() ?>. I have corrected that. Sorry if it is not working, but the if/else are correct.
    – Brad
    Commented Feb 21, 2015 at 15:22
  • I believe part of the problem is $temporada is not defined. I was able to get this working locally by creating a custom taxonomy 'temporada', assigning a post the temporada term 'test_temporada' and changing $temporada->slug to 'test_temporada'. I can edit the code above if you would like.
    – Brad
    Commented Feb 21, 2015 at 15:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.