标签: 输出结构

  • wordpress自定the_category的输出结构

    通过WordPress的过滤器the_category来自定义输出内容。方法很简单,但是很实用。以下是一个示例代码:

    function custom_the_category($thelist, $separator = '', $parents = '') {
        // 获取当前文章的所有分类
        $categories = get_the_category();
    
        if (empty($categories)) {
            return $thelist;
        }
    
        $thelist = '';
        foreach ($categories as $category) {
            $cat_name = esc_html($category->name);
            $cat_link = esc_url(get_category_link($category->term_id));
    
            // 自定义每个分类的HTML结构
            $thelist .= '<div class="custom-category-item">';
            $thelist .= '<a href="' . $cat_link . '" class="custom-category-link">';
            $thelist .= '<span class="custom-category-name">' . $cat_name . '</span>';
            $thelist .= '</a>';
            $thelist .= '</div>';
        }
    
        return $thelist;
    }
    add_filter('the_category', 'custom_the_category', 10, 3);

    然后在模板文件中,你可以像平常一样调用the_category()函数:

    <?php the_category(', '); ?>