【问题标题】:add link to list of post terms添加链接到帖子条款列表
【发布时间】:2015-05-12 14:45:02
【问题描述】:

我需要更改下面的函数,以便它输出链接列表而不是字符串。所以我需要的结果html是<a href="#">term</a>

function my_post_terms() {

// 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 = implode( ' ',$arr_terms );

    // Get out of here
    return $terms;
}
}

【问题讨论】:

  • 一个简单的foreach 应该可以解决问题。
  • 你会在函数的什么地方写这个?

标签: php wordpress function categories


【解决方案1】:

尝试作为流动代码..

function my_post_terms() {

// 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 = implode( ' ',$arr_terms );

    // Get out of here

  if (!filter_var($terms , FILTER_VALIDATE_URL) === false) {
       echo "<a href='#'>".$terms."</a>";
    }  

    return $terms;
}
}

【讨论】:

  • 我在这一行遇到语法错误terms ="&lt;a href='#'&gt;".term."&lt;/a&gt;"
  • 我认为你仍然缺少terms开头的$
  • 我将$ 添加到termsterm,语法错误消失了,但标签没有显示。
  • 对不起.. 我被添加时没有 $ 和 ;等等。请现在就编译。
  • 对不起,但仍然不起作用。什么都没有显示
猜你喜欢
  • 1970-01-01
  • 2015-03-09
  • 2021-04-08
  • 2019-12-22
  • 1970-01-01
  • 2023-03-22
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
相关资源
最近更新 更多