How to remove separator after last category
-
Hi, I have been trying to create a custom list that will display the 5 latest posts with the categories listing above each post (sometimes multiple categories) with an associated id). I have it working right now but am stuck with an extra separator at the end of the last category which I’d like to remove. I’d appreciate suggestions on how to get rid of the last separator. If anyone has a better way to accomplish the same category list for each post I would also be interested to hear that as I’m trying to learn and be as efficient with code as possible. Thanks in advance.
function recent_posts_function($atts){ extract(shortcode_atts(array( 'posts' => 5, ), $atts)); $return_string = '<ul>'; query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts)); if (have_posts()) : while (have_posts()) : the_post(); global $post; $categories = get_the_category($post->ID); $separator = ' | '; $output = ''; $cat_link = get_category_link($categories[0]->cat_ID); if($categories){ foreach($categories as $category) { $output .= '<a href="'.get_category_link( $category->term_id ).'" id="'.$category->slug.'">'.$category->cat_name.'</a>'.$separator; } } $return_string .= '<li>'.$output.' <br/><a href="'.get_permalink().'">'.get_the_title().'</a><br/><img src="/wp-content/uploads/2015/01/post-date.jpg" border="0" style="float: left; padding-right:5px;">'.get_the_date().'<br/><br/> </li>'; endwhile; endif; $return_string .= '</ul>'; wp_reset_query(); return $return_string; } function register_shortcodes(){ add_shortcode('kapitall-recent-posts', 'recent_posts_function'); } add_action( 'init', 'register_shortcodes');
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘How to remove separator after last category’ is closed to new replies.