First, ensure that your template file is named home.php
.
Second, there’s no need to use a custom query loop in this context. If you only want to display 3 posts on the blog posts index (i.e. the “homepage”), then filter the main loop query via pre_get_posts
:
function wpse83660_filter_pre_get_posts( $query ) {
if ( is_home() && $query->is_main_query() ) {
$query->set( 'posts_per_page', '3' );
}
}
add_action( 'pre_get_posts', 'wpse83660_filter_pre_get_posts' );
And you can replace your entire recentPosts()
function with a normal loop.
Thurd, you’re calling the_content()
both outside of a loop instantiation, and expecting it to return content other than what it will return:
<?php get_header(); ?>
<?php the_content(); ?>
<?php get_footer(); ?>
This call to get_content()
has 2 problems:
-
You haven’t instantiated a loop:
if ( have_posts() ) : while ( have_posts() ) : the_post();
-
In the blog posts index context, this call to
the_content()
will return the content of the posts in the blog posts index, not the content of the *page assigned aspage_for_posts
By solving the second problem, we’ll fix the first as well.
To get at the post_content for the page assigned as page_for_posts
, use get_page()
, and pass it get_option( 'page_for_posts' )
:
$page_object = get_page( get_option( 'page_for_posts' ) );
Then, output its results:
if ( ! is_null( $page_object ) ) {
echo apply_filters( 'the_content', $page_object->post_content );
}
So, your entire template file will look like so:
/**
* Blog posts index template file
*
* Displays the blog posts index
*
* @filename: home.php
*/
get_header();
if ( have_posts() ) : while ( have_posts() ) : the_post();
<a href="https://wordpress.stackexchange.com/questions/83660/<?php the_permalink();?>"><?php the_post_thumbnail('recent-thumbnails'); ?></a>
<h2><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h2>
<?php the_content(); ?>
endwhile; endif;
$page_object = get_page( get_option( 'page_for_posts' ) );
if ( ! is_null( $page_object ) ) {
echo apply_filters( 'the_content', $page_object->post_content );
}
get_footer();