我已将我所有的帖子设置到“设置”->“阅读”下名为“工作”的页面。
我假设现在 Work 是帖子的父页面。
所以现在我正在尝试使用此代码获取帖子的父级,即 Work,
print_r(get_permalink($post->post_parent));
但是为什么我看不到这个父页面的数据呢?我获取的是当前页面的数据。
有什么想法吗?
编辑:
单个.php
<!-- container -->
<div class="container">
<?php
// Include the page nav.
get_template_part( 'nav-page', 'nav' );
?>
<?php
// Start the loop.
while ( have_posts() ) : the_post();
/*
* Include the post format-specific template for the content. If you want to
* use this in a child theme, then include a file called called content-___.php
* (where ___ is the post format) and that will be used instead.
*/
get_template_part( 'content-single', get_post_format() );
// End the loop.
endwhile;
?>
</div>
<!-- container -->
content-single.php
<?php global $post;?>
<div id="wrapper">
<div class="col-md-6">
///
</div>
<div class="col-md-6">
<?php the_content(); ?>
<?php
var_dump($post->post_parent->ID);
?>
</div>
</div>
最佳答案
因为您已经分配了一个帖子页面,所以这不是父页面。相反,您必须调用:
$blog_id = get_option('page_for_posts');
get_permalink($blog_id);
关于php - WordPress - 如何获取帖子的父页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35645803/