I have a taxonomy called “country” and I need to visualize it as a list of categories can someone help me?
<li><a href="<?php echo esc_url( home_url() ); ?>/country/argentina/">Argentina</a></li> <li><a href="<?php echo esc_url( home_url() ); ?>/country/australia/">Australia </a></li> <li><a href="<?php echo esc_url( home_url() ); ?>/country/canada/">Canada</a></li> <li><a href="<?php echo esc_url( home_url() ); ?>/country/china/">China</a></li> <li><a href="<?php echo esc_url( home_url() ); ?>/country/france/">France</a></li> <li><a href="<?php echo esc_url( home_url() ); ?>/country/germany/">Germany</a></li> <li><a href="<?php echo esc_url( home_url() ); ?>/country/india/">India</a></li> <li><a href="<?php echo esc_url( home_url() ); ?>/country/italy/">Italy</a></li> <li><a href="<?php echo esc_url( home_url() ); ?>/country/spain/">Spain</a></li> <li><a href="<?php echo esc_url( home_url() ); ?>/country/south-korea/">Korea</a></li> <li><a href="<?php echo esc_url( home_url() ); ?>/country/uk/">United Kingdom</a></li> <li><a href="<?php echo esc_url( home_url() ); ?>/country/usa/">USA</a></li>
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You can achieve the result using following code:
$terms = get_terms( array(
'taxonomy' => 'country',
'hide_empty' => false,
'orderby' => 'name',
'order' => 'ASC',
) );
if ( !empty($terms) ) :
$output = '<ul>';
foreach( $terms as $country ) {
$output.= '<li><a href="'.get_term_link( $country->term_id ).'">'. esc_attr( $country->name ) .'</a></li>';
}
$output.='</ul>';
echo $output;
endif;
Here I’m using get_terms() to get all item of country taxonomy. The checking if there is any item in the list. If yes then I’m looping through them and returning as desired link. get_term_link() is being used to get the term url by using term id.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0