Bricks 中文文档:bricks/get_the_title

标签TAGS: 

📄 原始资料  |  展开查看原始 JSON(bricks-get_the_title.json){ “name”: “bricks/get_the_title”, “kind”: “filter”, “url”: “https://academy.bricksbuilder.io/developer/hooks/filters/bricks-get_the_title/”, “scraped_at”: “2026-08-29”, “hook_name”: “bricks/get_the_title”, “description”: { “en”: “Filters the…

📄 原始资料  |  展开查看原始 JSON(bricks-get_the_title.json)
{
  "name": "bricks/get_the_title",
  "kind": "filter",
  "url": "https://academy.bricksbuilder.io/developer/hooks/filters/bricks-get_the_title/",
  "scraped_at": "2026-08-29",
  "hook_name": "bricks/get_the_title",
  "description": {
    "en": "Filters the page title returned by Helpers::get_the_title() . This function determines the appropriate title based on the context (single post, archive, search results, etc.).",
    "zh": "修改 Bricks 中使用的默认元数据(meta)键。"
  },
  "parameters": [
    {
      "name": "title",
      "type": "string",
      "type_zh": null,
      "description": {
        "en": "The generated page title.",
        "zh": "已生成的页面标题。"
      }
    },
    {
      "name": "post_id",
      "type": "int",
      "type_zh": null,
      "description": {
        "en": "The ID of the current post or template.",
        "zh": "当前文章或模板的 ID。"
      }
    }
  ],
  "example_usage": "add_filter( 'bricks/get_the_title', function( $title, $post_id ) {\n    // Example: Add a prefix to the title for search results\n    if ( is_search() ) {\n        return 'Searching: ' . $title;\n    }\n\n\n    return $title;\n}, 10, 2 );"
}

前言

修改 Bricks 中使用的默认元数据(meta)键。

用法

将代码加入子主题的 functions.php

add_filter( 'bricks/get_the_title', function( $title, $post_id ) {
    // Example: Add a prefix to the title for search results
    if ( is_search() ) {
        return 'Searching: ' . $title;
    }


    return $title;
}, 10, 2 );

参数

  • $title (string):已生成的页面标题。
  • $post_id (int):当前文章或模板的 ID。

示例代码

筛选返回值

add_filter( 'bricks/get_the_title', function( $title, $post_id ) {
    // Example: Add a prefix to the title for search results
    if ( is_search() ) {
        return 'Searching: ' . $title;
    }


    return $title;
}, 10, 2 );

根据条件短路返回原值

根据条件跳过或修改返回值:

add_filter( 'bricks/get_the_title', function( $value, $title ) {
    if ( is_admin() ) { return $value; }
    // TODO: custom logic here
    return $value;
} );

补充说明

更多细节可参考 官方文档