如何更改下面的函数,使其在链接而不是字符串时输出列表?我需要的结果html是<a href="#">term</a> <a href="#">term</a> <a href="#">term</a>
等
function my_post_term_links() {
// Get an array of all taxonomies for this post
$taxonomies = get_taxonomies( \'\', \'names\' );
// Are there any taxonomies to get terms from?
if ( $taxonomies ) {
// Call the wp_get_post_terms function to retrieve all terms. It accepts an array of taxonomies as argument.
$arr_terms = wp_get_post_terms( get_the_ID(), array_values( $taxonomies ) , array( "fields" => "names" ) );
// Convert the terms array to a string
$terms_links = implode( \' \',$arr_terms );
if (!filter_var($terms_links , FILTER_VALIDATE_URL) === false) {
$terms_links ="<a href=\'#\'>".$terms_links."</a>";
}
// Get out of here
return $terms_links;
}
}
更新//
我通过添加一个for循环,设法让函数显示和列出带有链接的术语,但它复制了最后的术语,并在末尾添加了没有链接的术语。。。
foreach ( $arr_terms as $terms_links ) {
echo \'<a href="#">\' . $terms_links . \'</a>\';
}
这就是函数现在的样子。。。
function my_post_term_links() {
// Get an array of all taxonomies for this post
$taxonomies = get_taxonomies( \'\', \'names\' );
// Are there any taxonomies to get terms from?
if ( $taxonomies ) {
// Call the wp_get_post_terms function to retrieve all terms. It accepts an array of taxonomies as argument.
$arr_terms = wp_get_post_terms( get_the_ID(), array_values( $taxonomies ) , array( "fields" => "names" ) );
// Convert the terms array to a string
//$terms_links = implode( \' \',$arr_terms );
foreach ( $arr_terms as $terms_links ) {
if(!empty($terms_links)) {
echo \'<a href="#">\' . $terms_links . \'</a>\';
}
}
/*if (!filter_var($terms_links , FILTER_VALIDATE_URL) === false) {
$terms_links ="<a href=\'#\'>".$terms_links."</a>";
} */
// Get out of here
return $terms_links;
}
}