-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!

closed as unclear what you're asking by Michael, Pieter Goosen, cybmeta, TheDeadMedic, fuxia Feb 22 '15 at 11:14

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

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
    }
?>
  • i need the second else statement, i add this, but this does work :/ – Ferrrmolina Feb 21 '15 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 Feb 21 '15 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 Feb 21 '15 at 15:49

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