【问题标题】:How replace string in wordpress post title using function.php如何使用function.php替换wordpress帖子标题中的字符串
【发布时间】:2023-04-07 18:22:01
【问题描述】:

我想用function.php 替换帖子标题中的破折号'-',但我做不到。

这是我的代码:

function the_breadcrumb()
{
    if (is_single()) 
    {
        str_replace(" - ", $strReplaceWith, get_the_title());
    }
}

此代码不起作用,因为wordpress 自动将 '-' 替换为 '-',我也使用了此代码(如下),但它也不起作用。

function the_breadcrumb()
{
    if (is_single()) 
    {
        str_replace(" – ", $strReplaceWith, get_the_title());
    }
}

我在另一个文档 (test.php) 上尝试了第一个代码,但我无法在 function.php 中使用此代码

对不起,我的英语说得不太好。

【问题讨论】:

  • 您可以使用正则表达式或值数组来搜索和替换。尝试在the_title() 中替换它们的棘手部分是您必须适应用户可以放置的所有排列在title 字段中。您的示例正在搜索" – ",这意味着如果连字符的任何一侧(或者如果一侧只有一个空格)都没有` `(空格),那么它将不会替换任何内容。您最好的选择是创建一个正则表达式模式以用作 str_replace 中的指针或所有各种选项的数组,或者简单地使用 array('-', '–')

标签: php wordpress string replace title


【解决方案1】:

尝试应用 wordpress 过滤器挂钩。删除 get_the_title() 对 the_title() 的更改。 https://codex.wordpress.org/Plugin_API/Filter_Reference/the_title.

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2023-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-26
  • 1970-01-01
相关资源
最近更新 更多