• Resolved hungzai

    (@hungzai)


    At the moment it list AppleOrangeBanana etc.
    I would like to use separators to list like Apple | Orange | Banana

    <?php
    foreach((get_the_category()) as $childcat) {
    if (cat_is_ancestor_of(16, $childcat)) {
    echo '<a href="'.get_category_link($childcat->cat_ID).'">';
     echo $childcat->cat_name . '</a>';
    }}
    ?>

    Anyone has any idea? Thanks in advance.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hello hungzai,

    Please update your code with the below code:

    <?php
    foreach((get_the_category()) as $childcat) {
    if (cat_is_ancestor_of(16, $childcat)) {
    echo '<a href="'.get_category_link($childcat->cat_ID).'">';
     echo $childcat->cat_name . ' | </a>';
    }}
    ?>

    Thanks

    Thread Starter hungzai

    (@hungzai)

    Very good!! Thank you so much!!. However now there is a separator that comes up after the last category. Any idea how to remove that last separator? (sorry for the trouble!)

    Thread Starter hungzai

    (@hungzai)

    For those who are keen. Here is the solution.

    <?php $args = array('child_of' => 16,'hierarchical' => 0,
    'parent' => 16,);
    
    $terms = get_terms( 'category', $args );
    if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
    	$count = count( $terms );
    	$i = 0;
    	$term_list = '<p class="my-category-list">';
    	foreach ( $terms as $term ) {
    		$i++;
    		$term_list .= '<a href="' . get_term_link( $term ) . '" title="' . 
    
    $term->name . '">' . $term->name . '</a>';
    		if ( $count != $i ) {
    			$term_list .= ' | ';
    		}
    		else {
    			$term_list .= '</p>';
    		}
    	}
    	echo $term_list;
     }
     ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to use separator for listing categories?’ is closed to new replies.