javascript - 尝试通过 function.php 将一些自定义文本添加到 Wordpress 帖子标题中
问题描述
我需要在 wordpress 帖子标题中动态添加自定义文本,我试图通过 function.php 中的此代码添加
以下代码不起作用
add_filter('the_title', 'new_title', 10, 2);
function new_title($title, $id) {
if('babysitters' == get_post_type($id)){
$exclusive = get_field('exclusive');
$newtitle = $title .', ' .$exclusive->y;
}
else{
$newtitle = $title;
}
return $newtitle;
}
解决方案
您正在使用get_field
但它不在 Wordpress“循环”中,因此您需要将帖子 ID 传递给函数,以便它知道要使用的帖子。
假设 get_field 将一个对象返回到$exclusive
变量中,您只需将函数更改为以下内容:
add_filter('the_title', 'new_title', 10, 2);
function new_title($title, $id) {
if('babysitters' == get_post_type($id)){
$exclusive = get_field('exclusive', $id); // pass the id into get_field
$title = $title .', ' .$exclusive->y;
}
return $title;
}
推荐阅读
- mybatis - 尝试从 ibatis 版本 2 升级到 mybatis 3.3,卡在下面的代码片段中
- node.js - Firebase onCreate 方法未触发
- reactjs - 如何在反应表上打开表行?
- extjs - ExtJS 在 Safari 14 上冻结
- sql - 创建不重复同一列的 SQL 表
- vue.js - Vue:测试 utils vue 组合 - root.$route
- c - C - 我应该使用引号还是方括号将标题包含在单独的目录中
- docker - Docker 日志 - 将时间从主机同步到容器
- php - 有没有办法让 PHP 在表格中向下获取第二行的值?
- reactjs - 登录成功时重定向页面