admin管理员组文章数量:1414908
I have a page that is assigned as the "Blog". What I'd like to allow is the user to add content to this page then display it before the posts show up on index.php
. I thought I used to be able to do it using this method:
<?php
$blog = get_option('page_for_posts');
echo apply_filters('the_content', get_the_content($blog));
?>
For some reason it is deciding to pull the first post instead of actually pulling the page content. Is this a bug or is there another method to pull the blog page content?
I have a page that is assigned as the "Blog". What I'd like to allow is the user to add content to this page then display it before the posts show up on index.php
. I thought I used to be able to do it using this method:
<?php
$blog = get_option('page_for_posts');
echo apply_filters('the_content', get_the_content($blog));
?>
For some reason it is deciding to pull the first post instead of actually pulling the page content. Is this a bug or is there another method to pull the blog page content?
Share Improve this question asked Oct 2, 2014 at 14:41 Howdy_McGee♦Howdy_McGee 20.9k24 gold badges91 silver badges177 bronze badges2 Answers
Reset to default 11You are using get_the_content()
wrong, it can't take a ID, which is what get_option('page_for_posts')
does return, and generally gets the content of the current post inside the loop, in which it has to be used.
To get the actual content of that page you can do:
$page_for_posts_id = get_option( 'page_for_posts' );
$page_for_posts_obj = get_post( $page_for_posts_id );
echo apply_filters( 'the_content', $page_for_posts_obj->post_content );
Or:
$page_for_posts_id = get_option('page_for_posts');
echo get_post_field( 'post_content', $page_for_posts_id );
With the new WP editor that uses blocks the second code block in the accepted answer won't render the blocks. To get them to render use the following.
$page_for_posts_id = get_option( 'page_for_posts' );
echo apply_filters( 'the_content', get_post_field( 'post_content', $page_for_posts_id ) );
The first code-block in the accepted answer will still work.
本文标签: optionsGet Content From Blog Page
版权声明:本文标题:options - Get Content From Blog Page 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745182023a2646508.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论