View the complete list of a taxonomy in the navigation menu

I have a taxonomy called "country" and I need to visualize it as a list of categories can someone help me?<

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>

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>
Share Improve this question asked Jul 1, 2020 at 8:31 Vincenzo PiromalliVincenzo Piromalli 1989 bronze badges 1
  • Hi Vincenzo! So you want to create output like this from your taxonomy? Do you want to write code to do this and add it to your theme? What have you tried so far? – mozboz Commented Jul 1, 2020 at 10:02
Add a comment  | 

1 Answer 1

Reset to default 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742302375a4418268.html

相关推荐

  • View the complete list of a taxonomy in the navigation menu

    I have a taxonomy called "country" and I need to visualize it as a list of categories can someone help me?<

    16小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信