如何更改<?php the_ategory()>模板?

时间:2017-01-15 作者:Nelya Sanotska

此函数生成此html

<ul class="post-categories">
   <li><a href="#" rel="category tag" class="text">test</a></li>
   <li><a href="#" rel="category tag" class="text">test</a></li>
</ul>
但我需要这个html

<ul class="post-categories">
<li>
    <a href="#" rel="category tag" class="text">            
        <span class="hover-toggle">Farm to Cup</span>
        <span class="hover-toggle">Farm to Cup</span>            
    </a>
</li>
<li>
    <a href="#" rel="category tag" class="text">            
        <span class="hover-toggle">Farm to Cup</span>
        <span class="hover-toggle">Farm to Cup</span>            
    </a>
</li>

有什么解决办法吗?

1 个回复
SO网友:funkysoul

您可以使用此代码段,并相应地修改输出字符串。

$categories = get_the_category();
$separator = \' \';
$output = \'\';
if ( ! empty( $categories ) ) {
    foreach( $categories as $category ) {
        $output .= \'<a href="\' . esc_url( get_category_link( $category->term_id ) ) . \'" alt="\' . esc_attr( sprintf( __( \'View all posts in %s\', \'textdomain\' ), $category->name ) ) . \'">\' . esc_html( $category->name ) . \'</a>\' . $separator;
    }
    echo trim( $output, $separator );
}