php - Getting no more than 2 categories shown and last one to remove ending slash -


i trying show @ 2 categories per post in wordpress , managed so, don't know how detect second one.

<?php while ( have_posts() ) : the_post();     $terms  = get_terms( 'directory_categories', 'orderby=name&hide_empty=1&hierarchical=0' );  // getting categories     $i = 0;     $len = count($terms); // counting categories     foreach($terms $term) { // terms array of objects (http://codex.wordpress.org/function_reference/get_terms)       $i++;       if ($i < 3) {         //if reached second loop displays '/'         $array[] = $term->name;         $limit = count($array);       ?>       <a href="<?php echo get_term_link( $term->slug, 'directory_categories' ); ?>"><?php echo $term->name; ?></a>/     <?php       // else if reached second loop , second loop 2 should omit slash     } elseif($i < 3 && == 2) { ?>     <a href="<?php echo get_term_link( $term->slug, 'directory_categories' ); ?>"><?php echo $term->name; ?></a>     <?php } else { } ?> <?php } ?> <!-- end foreach --> endwhile; ?> 

current output

category 1 / category 2/ 

expected output without ending slash

category 1 / category 2 

i sure logic wrong, please let me know mistaking.

use code instead (i've put comments can maintain it):

<?php  while ( have_posts() ) : the_post();     $tax = 'directory_categories'; // taxonomy     $total = 2; // number of categories show each post     $sep = ' / '; // separator want use      $terms = get_the_terms(get_the_id(), $tax);     if ($terms && !is_wp_error($terms)) {         $terms = array_values($terms);         foreach ($terms $key => $term) {             echo '<a href="' . get_term_link($term->slug, $tax) . '">' . $term->name . '</a>';             if ($key < $total - 1 && count($terms) >= $total) {                 echo $sep;             }             if ($key == $total - 1) {                 break;             }         }     } endwhile; ?> 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -