douhao2856 2015-03-06 12:17
浏览 9
已采纳

将类别名称类添加到类别链接

Our WordPress template displays the categories that a post is within. For example if a post is in the category Dogs and also the category Cats then those category links are displayed on the post's page.

Is it possible to output the links like this...

<a href="/category/cats" class="cats">Cats</a>
<a href="/category/cats" class="dogs">Dogs</a>

The reason is we'd like to style each category's link with a different color.

If so, how can I accomplish this?

  • 写回答

1条回答 默认 最新

  • dtv11049 2015-03-06 12:30
    关注

    Assuming this is on the single template, you can generate the links using something like this:

    $categories = wp_get_post_terms( get_the_id(), 'category' );
    
    if ( $categories ):
        foreach ( $categories as $category ): ?>
            <a href="<?php echo get_term_link( $category->term_id, 'category' ); ?>" class="<?php echo $category->slug; ?>"><?php echo $category->name; ?></a>
        <?php endforeach;
    endif;
    

    The class will be generated from the category slug, so you can be sure that it will never have a space in it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部