首页 > 解决方案 > 无法在 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 中动态修改页面的标签?

谢谢。

标签: phpwordpressfacebook-opengraph

解决方案


您可以使用 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');

Wordpress 动作参考

您可以通过多种方式获取页面 ID,包括根据您的要求使用$post 变量。


推荐阅读