【问题标题】:Wordpress, echo list of slug terms with custom linkWordpress,带有自定义链接的 slug 术语的回显列表
【发布时间】:2019-01-16 18:48:41
【问题描述】:

我正在尝试使用我的自定义链接获取术语列表(使用 CPT UI),其中 slug 是自定义链接的最后一部分。

示例: 客户:Term1、Term2、Term3

其中每个术语都是一个链接,例如:

example.com/#term1
example.com/#term2
example.com/#term3

所以,我有相同的自定义链接结构,但只有最后的 slug 发生了变化:

$servizio = get_the_terms($post->ID, 'servizio');
$servizio = array_values($servizio);
    for($cat_count=0; $cat_count<count($servizio); $cat_count++) {
        echo $servizio[$cat_count]-> slug;
        if ($cat_count<count($servizio)-1){
            echo ', ';
        }
    }

【问题讨论】:

    标签: wordpress loops slug


    【解决方案1】:

    改用foreach 循环,用术语进行迭代要容易得多。

    $categories = get_the_terms(get_the_ID(), 'servizio');
    $categories_output = [];
    
    if ($categories) { // Prevent the error #Invalid argument supplied for foreach()# incase the post has no category
        foreach ($categories as $category) {
            $categories_output[] = '<a href='. get_bloginfo('url') . '/#' . $category->slug .'>'. $category->name .'</a>';
        }
    }
    
    if ($categories_output) {
        echo implode(', ', $categories_output);
    }
    

    【讨论】:

      猜你喜欢
      • 2021-08-01
      • 2014-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-27
      • 2019-06-09
      • 2020-08-11
      • 2017-01-31
      相关资源
      最近更新 更多