【问题标题】:Echo the subtitle of a PARENT page within WordPress?在 WordPress 中回显父页面的副标题?
【发布时间】:2013-08-06 20:11:20
【问题描述】:

我正在尝试从父页面获取字幕并在标题标签中回显文本。

功能:

<?php
    $id_to_get = $post->ID;
    if ( $post->post_parent ) {
        $id_to_get = $post->post_parent;
    }
    $parent = $id_to_get;

    $title = get_the_title($parent);
    $subtitle = get_post_meta($id_to_get, '_base_page_subtitle', true);
?>

HTML 实现: ...title="">

很遗憾,它没有成功抓取字幕文本。我能够填充与变量 $title 相呼应的标题,而不是使用变量 $subtitle。

字幕选项的值存储在表“_base_page_subtitle”中;我正在使用选项框架。

我知道我的问题在于 if 语句,但是,作为 PHP 新手,我很难弄清楚如何捕获数据并正确显示它。

再次感谢时间!

【问题讨论】:

  • $post$post_child 属性吗?我想你可以检查$post-&gt;post_parent,然后将其设置为$post-&gt;post_parent。如果帖子没有父级,我相信它设置为零。
  • 不幸的是,使用 post_parent 仍然没有用字幕文本填充它...
  • 如果你回显它,$id_to_get 的设置是否正确?
  • 确实如此,在这种情况下,它会在标题标签中正确地回显“67”。另外,我更新了原始代码 sn-p 以使用 post_parent 而不是 post_child。我在提交帖子之前忘记更新了,因为我还在进行故障排除。

标签: php wordpress subtitle


【解决方案1】:

你可以在没有函数的情况下做到这一点 - 直接回显它

title="<?php if ( $post->post_parent ) { echo get_post_meta($post->post_parent, '_base_page_subtitle', true);}else{echo get_post_meta($post->ID, '_base_page_subtitle', true);}?>"

【讨论】:

  • 马克,这对子页面很有用,但是,它不再在实际的父页面上显示标题。这也将页面标题/子标题放在标题标签中。
  • 我需要它在父页面和该父页面的所有子页面上显示字幕作为标题标签。
  • 所以你只想要父母和孩子的标题标签中的副标题?
  • 没错,马克。我试过用 if 语句来做,不幸的是它不起作用。
  • @JoeyO'Driscoll 我更新了我的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-06
  • 2015-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多