首页 > 解决方案 > 如何在子页面上显示父摘录

问题描述

在 WordPress 中,我有一个附件页面(子页面)。
如何在此页面上显示父摘录?
因为这段代码不起作用:

<?php global $post;
  if ( $post->post_parent ) { ?>

    <?php echo get_the_excerpt( $post->post_parent ); ?>

<?php } ?>

谢谢你的帮助。
对此,我真的非常感激。

标签: phpwordpresswordpress-themingattachmentcustom-wordpress-pages

解决方案


你可以使用wp_get_post_parent_id()which will

返回帖子父级的 ID。

然后您可以显示摘录,get_the_excerpt()其中将

检索帖子摘录。

你应该得到一些东西(以下未经测试)

<?php
echo get_the_excerpt(wp_get_post_parent_id());
//...

推荐阅读