钩子文档

single_term_title

💡 云策文档标注

概述

single_term_title 是一个 WordPress 过滤器钩子,用于自定义分类法归档页面的标题显示。它允许开发者修改或添加前缀到术语名称中。

关键要点

  • 过滤器钩子:single_term_title,用于过滤分类法归档页面的标题。
  • 参数:$term_name(字符串),表示当前显示的术语名称。
  • 用途:常用于在标题中添加自定义前缀或基于分类法信息进行格式化。
  • 相关函数:single_term_title(),用于显示或检索分类法术语归档的页面标题。
  • 版本历史:自 WordPress 3.1.0 版本引入。

代码示例

// 返回带自定义前缀的标题
add_action('single_term_title', 'single_term_title_28072021');

function single_term_title_28072021($term_name){
    $queried_object = get_queried_object();
    $taxonomy = isset($queried_object->taxonomy) ? $queried_object->taxonomy : '';
    return sprintf(__('Product Category: %s','textdomain'), $term_name);
}

// 返回带分类法前缀的标题
add_action('single_term_title', 'single_term_title_29072021');

function single_term_title_29072021($term_name){
    $queried_object = get_queried_object();
    $taxonomy = isset($queried_object->taxonomy) ? $queried_object->taxonomy : '';
    return sprintf(__('%s: %s','textdomain'), $taxonomy, $term_name);
}

📄 原文内容

Filters the custom taxonomy archive page title.

Parameters

$term_namestring
Term name for archive being displayed.

Source

$term_name = apply_filters( 'single_term_title', $term->name );

Changelog

Version Description
3.1.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Return with custom prefix text:

    add_action('single_term_title', 'single_term_title_28072021');
    
    function single_term_title_28072021($term_name){
    
        $queried_object = get_queried_object();
        $taxonomy = isset($queried_object->taxonomy) ? $queried_object->taxonomy : '';
    
    
        return sprintf(__('Product Category: %s','textdomain'), $term_name);
    
    }

    Return with taxonomy prefix text:

    add_action('single_term_title', 'single_term_title_29072021');
    
    function single_term_title_29072021($term_name){
    
        $queried_object = get_queried_object();
        $taxonomy = isset($queried_object->taxonomy) ? $queried_object->taxonomy : '';
    
    
        return sprintf(__('%s: %s','textdomain'), $taxonomy, $term_name);
    
    }