【问题标题】:Get title of Wordpress page in included PHP在包含的 PHP 中获取 Wordpress 页面的标题
【发布时间】:2012-06-20 00:44:44
【问题描述】:

(长期阅读,第一次写)

我有一个 wordpress 安装,其中每个页面都显示一个信息表,该表根据它所在的页面而有所不同。我试图使用页面标题作为告诉表格使用此代码显示什么的变量:

<?php $post_title = the_title(); ?>
<?php $parent_title = get_the_title($post->post_parent); ?>

...获取页面标题以及父页面的标题。我要显示的表格在页面模板中的 php include 命令中。但是,看起来页面模板中的变量不会延续到包含的 php 页面中。如何将页面标题放入我可以在包含的 php 中使用的变量中?

【问题讨论】:

  • the_title() 确实输出,所以你不会捕获任何东西。这就是 wp 拥有所有 get_the_whatever() 函数的原因,它们返回数据而不是输出数据。

标签: php wordpress include title


【解决方案1】:

下面的函数有 2 个参数,包含的文件和一个参数数组。例如:

render('yourfile.php', array('title' => 'Your title'));

功能:

<?php
function render($file, $params)
{
    extract($params); 
    ob_start(); 
    require($file); 
    return ob_get_clean(); 
}
?>

然后,您会将$title 放入包含的文件中。

【讨论】:

  • 那么,您是说函数定义和渲染调用都将进入页面模板?我似乎无法让它工作
  • "我要显示的表格在页面模板中的 php include 命令中。"此函数将包含您想要的其中包含变量的文件。将设置变量,因为在函数中您将包含这些值。 php.net/manual/en/function.extract.php
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-02
  • 2014-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多