php - WordPress - 如何获取帖子的父页面?

标签 php wordpress

我已将我所有的帖子设置到“设置”->“阅读”下名为“工作”的页面。

我假设现在 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/

相关文章:

php - 使用php在数据库中存储点击次数

php - WordPress自定义帖子类型管理页面布局

PHP从MySQL数据库中获取数据

php - 获取当前帖子 ID 的 the_terms 名称

php - 在 Visual Composer 中将 html_template 参数添加到 vc_row 简码

php - 如何从购物车和结帐页面 woocommerce 中删除订单总额

php - 什么时候需要在 PHP 中使用 Classes

php - 我如何发送一个带有变量的http GET请求,该变量在使用php链接点击后增加/减少

php - 在多个实例中运行 Laravel 作业

php - 使用 PHP 的谷歌分析