php - 无法在 WordPress 中动态修改页面内容
问题描述
我需要opengraph
根据查询字符串更改特定页面上的一些属性。
我尝试使用一些基于已安装插件的过滤器functions.php
:
function update_the_og_title($content) {
if(is_page('the-page')) {
// Modify the tags
} else{
// Do nothing
}
}
但是,我很快发现functions.php
无法检测到使用is_page()
. 我应该怎么办?
我还能如何opengraph
在 WordPress 中动态修改页面的标签?
谢谢。
解决方案
您可以使用 is_page 但不能在最初包含 functions.php 时使用。您要做的是使用钩子在 WP 进程中的特定点运行您的代码:
function update_the_og_title() {
$id = get_the_id(); //
if(is_page($id)) {
// Get your content here, maybe from metadata?
// Modify the tags
} else{
// Do nothing
}
}
add_action('wp', 'update_the_og_title');
您可以通过多种方式获取页面 ID,包括根据您的要求使用$post 变量。
推荐阅读
- c - 将二进制数据编译成 C 程序并像文件一样使用它们
- javascript - 使两个过滤表下拉列表同时工作
- c# - .NET 中使用 PBKDF2 和 nonce 的良好实践 Web 身份验证
- php - 如何永远在后台运行 PHP 脚本文件
- azure-data-explorer - ADX 的内置导出命令与 ADFv2 复制活动性能
- android - 如何使 EditText 只允许表情符号输入?
- python - 如何通过Python将null(00 00等字节)附加到文件直到达到一定大小
- pandas - 需要使用多个类别和日期时间在各种表中划分数据框
- css - 重叠背景- 文本和背景颜色不一致
- excel - 当某些单元格不再为空白时,如何锁定 Excel 行?