Hello,
I am looking to retrieve the content from the page that has been defined as "the posts page" in the wordpress back end. My index.php looks like this:
get_header(); ?>
<section id="main">
<?php
get_template_part( 'loop', 'index' );
the_content();
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
echo "<section class='featuredimage'>";
the_post_thumbnail("full");
echo "</section>";
}
?>
</section><!-- #main -->
<?php get_footer(); ?>
This will execute the loop and generate the posts, which is good, but when the loop is complete I would like to display the actual content of the blog page itself (in other words: the content that has been typed into the text field in the back end). For some reason, I am unable to reset the wp-query and retrieve any of the actual page data.
The above code loops through the posts using loop.php, but instead of giving me the Blog page's post content and feature image, it provides the undesirable result of displaying the content from the very first post, again.
Thanks in advance to anyone who can help resolve this, it is much appreciated!
Cheers,
Matt
Michael Fields answers:
Please try the following code after your WordPress loop:
/* If a page has been assigned to display posts. query for that pages data and create a custom loop. */
$posts_page_id = get_option( 'page_for_posts' );
if ( ! empty( $posts_page_id ) ) {
$posts_page = get_post( $posts_page_id );
if ( isset( $posts_page->ID ) ) {
$_post = $post;
$post = $posts_page;
setup_postdata( $post );
the_title( '<h2>', '</h2>' );
the_content();
$post = $_post;
}
}
Best wishes,
-Mike
Michael Fields comments:
@Denzel Chia - Please navigate to Settings -> Reading -> Front page displays to see what Matt is referring to. WordPress has had an optional posts page for quite some time now.
Roberto Mas answers:
or something like this should work
get_header(); ?>
<?php get_sidebar(left); ?>
<div id="container">
<div id="content" class="weAreEquals" role="main">
<?php
/* Run the loop to output the posts.
* If you want to overload this in a child theme then include a file
* called loop-index.php and that will be used instead.
*/
get_template_part( 'loop', 'index' );
?>
<?php query_posts('post_type=post&showposts=1'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<article>
<?php the_title(); ?>
<?php the_post_thumbnail(); ?>
<?php the_content(); ?>
</article>
<?php endwhile; else: ?>
<?php endif; wp_reset_query(); ?>
</div><!-- #content -->
</div><!-- #container -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Denzel Chia answers:
Hi,
The below codes should be within your loop.php and not after get_template_part('loop','index')
the_content();
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
echo "<section class='featuredimage'>";
the_post_thumbnail("full");
echo "</section>";
}
There should be the wordpress loop within your loop.php, anything outside it will not be included within the loop.
In other words, your code is wrong.
And you should post the code of your loop.php here and increase the price money to at least $20.
$5 dollar will only get you suggestions and not written code solution.
Thanks.
Denzel Chia comments:
<blockquote>
I am looking to retrieve the content from the page that has been defined as "the posts page" in the wordpress back end.
</blockquote>
And to make things clearer, there is no posts page in wordpress. A post is a post and a page is a page, so what do you want exactly? Perhaps its a custom post type that you want?
I think it is better for you to re-phrase your question.
Thanks.
Denzel Chia comments:
@Micheal Fields - Thanks for the clarification. I did not know Matt was referring to Settings for assigning Front Page Displays.