admin管理员组文章数量:1287887
I have a template which outputs the blog posts (Blog Page), I open one of the posts which use single.php template, Now how can I get the ID of it's parent which is the (Blog Page)?
I should say that I want to use this ID to get the meta box value of the blog page in single.php.
The same goes for a custom post type, let's say 'project', Where a page template list the project's posts (Projects Page) and single-project.php for the single project post. in this case I want to get the (Projects Page) ID in single-project.php.
I have a template which outputs the blog posts (Blog Page), I open one of the posts which use single.php template, Now how can I get the ID of it's parent which is the (Blog Page)?
I should say that I want to use this ID to get the meta box value of the blog page in single.php.
The same goes for a custom post type, let's say 'project', Where a page template list the project's posts (Projects Page) and single-project.php for the single project post. in this case I want to get the (Projects Page) ID in single-project.php.
Share Improve this question edited Apr 25, 2013 at 8:03 metalzade asked Apr 25, 2013 at 7:06 metalzademetalzade 1171 gold badge2 silver badges5 bronze badges 11- 1 Can you explain the situation, Why do you want to get it? – Vinod Dalvi Commented Apr 25, 2013 at 7:11
- Post's parent id is not a blog page, but another post. There is no relation between post and your blog page, – Rajeev Vyas Commented Apr 25, 2013 at 7:12
- @VinodDalvi I edited the queston. – metalzade Commented Apr 25, 2013 at 7:24
- @RajeevVyas so you're saying there is no way to get the ID of blog page? – metalzade Commented Apr 25, 2013 at 7:24
- @metalzade offcourse not, try using get_option('page_for_posts') or see the is_home function from codex for reference. – Rajeev Vyas Commented Apr 25, 2013 at 7:37
2 Answers
Reset to default 5WordPress 5.7 introduces a new helper function to more easily fetch the parent post's ID:
get_post_parent()
This can also be used in conjunction with has_post_parent()
, so you could have something like looks like:
<?php if ( has_post_parent() ) : ?>
<a href="<?php the_permalink( get_post_parent() ); ?>">
<?php
echo sprintf(
esc_html__( 'Back to parent page: %s', 'text-domain' ),
get_the_title( get_post_parent() )
);
?>
</a>
<?php endif; ?>
Note that these functions accept a "child post ID" as a parameter, which defaults to the current post.
https://make.wordpress/core/2021/02/10/introducing-new-post-parent-related-functions-in-wordpress-5-7/
Use $post->post_parent
to get the parent ID of the post. Here $post
is an object with properties.
本文标签: blogHow to get the post39s parent ID
版权声明:本文标题:blog - How to get the post's parent ID? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741270565a2369207.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论