无法在 WordPress 中动态修改页面内容
Unable to dynamically modify content of a page in 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()
的页面。我该怎么办?
我还能如何在 WordPress 中动态修改页面的 opengraph
标签?
谢谢。
您可以使用 is_page,但如果最初包含 functions.php,则不能。你想要做的是 运行 你的代码在 WP 过程中的特定点,使用 hook:
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 variable。
我需要根据查询字符串更改特定页面上的某些 opengraph
属性。
我尝试使用一些基于 functions.php
中已安装插件的过滤器:
function update_the_og_title($content) {
if(is_page('the-page')) {
// Modify the tags
} else{
// Do nothing
}
}
但是,我很快发现 functions.php
无法检测到使用 is_page()
的页面。我该怎么办?
我还能如何在 WordPress 中动态修改页面的 opengraph
标签?
谢谢。
您可以使用 is_page,但如果最初包含 functions.php,则不能。你想要做的是 运行 你的代码在 WP 过程中的特定点,使用 hook:
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 variable。