How to extract url from get_the_term_list?

How do you extract the url from :

$url = get_the_term_list($post->ID, 'nom-origin')

I have tried many things but i just don’t get it. The only solution i got was this but i know it’s just too messy :

$url = get_the_term_list($post->ID, 'nom-origin');//,'<h3>Job Category:</h3> ', ', ', '' );
$url_tmp1 = explode("href="",$url);
$url_tmp2 = explode("" rel="tag">",$url_tmp1[1]);
$url_simple = $url_tmp2[0];

Related posts

Leave a Reply

2 comments

  1. get_term_link gives you the link of a particular taxonomy term.

    $terms = get_object_terms( $post->ID, 'nom-origin' );
    $urls = array();
    foreach( $terms as $term )
    {
        $url[] = get_term_link( $term->slug, 'nom-origin' );
        //Or do whatever you want here with the url
    }
    
  2. Never try to parse complete markup when you don’t have to. Follow the function calls.

    1. get_the_term_list uses get_the_terms
    2. get_the_terms returns an array of terms
    3. You can use that array plus get_term_link to get your URLs

    And the code for that is in the Codex.

    $terms = get_terms('species');
    echo '<ul>';
      foreach ($terms as $term) {
        echo '<li><a href="'.get_term_link($term->slug,species').'">'.$term->name.'</a></li>';
      }
    echo '</ul>';
    

    You may want to build an array instead, but its the same idea:

    $terms = get_terms('species');
    $turls = array();
    foreach ($terms as $term) {
        $turl[] = get_term_link($term->slug,'species');
    }