如何从Get_the_Term_List中提取URL?

时间:2013-03-29 作者:greta22

如何从中提取url:

$url = get_the_term_list($post->ID, \'nom-origin\')
我尝试了很多东西,但我就是不明白。我得到的唯一解决方案是这个,但我知道它太乱了:

$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];

2 个回复
SO网友:Manny Fleurmond

get_term_link 提供特定分类术语的链接。

$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
}

SO网友:s_ha_dum

如果不需要,请不要尝试解析完整的标记。遵循函数调用。

  1. get_the_term_list uses get_the_terms
  2. get_the_terms 返回术语数组,您可以使用该数组加get_term_link 要获取URL,请执行以下操作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>\';
    
    您可能想构建一个数组,但想法是一样的:

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

结束

相关推荐