Custom Blog Post Page

I found this example here of creating a custom blog post page:

http://www.wpbeginner.com/wp-themes/how-to-create-a-custom-homepage-in-wordpress/

Read More

1) I created a blog page under Pages > Add New
2) Under Template, I select “Blog”
3) Note that I created a blog.php file in my custom theme that has this content:

<?php
/*
Template Name: Blog
*/
?>
<?php
    echo "Hello World";
    $temp = $wp_query;
    $wp_query= null;
    $wp_query = new WP_Query();
    $wp_query->query('posts_per_page=5'.'&paged='.$paged);
    while ($wp_query->have_posts()) : $wp_query->the_post();
?>

4) In settings > reading, under “Front page displays”, I set “Posts page:” to “Blog”

Now I view my blog page in browser, it just renders all my posts. But it doesnt echo the “Hello World” and if I remove the code from blog.php, it has no effect! It still just renders all the posts. And then I try to add a custom query:

<?php
/*
Template Name: Blog
*/
?>
<?php
    global $wpdb;
    $content = $wpdb->get_row(
        $wpdb->prepare(
            "SELECT * FROM $wpdb->posts
             WHERE post_type = %s
             LIMIT 1
            ",
            'header_post'
        )
    );
    echo $content->post_content; 
?>

And again it ignores this and just renders all the posts with post_type of post and a post_status of publish.

Obviously, the code in my blog.php is being ignored. Why?

This is what it currently looks like too (post date is missing, comment is missing):

I made changes to the index.php in my theme:

<?php
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('posts_per_page=1'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();
?>

and still no effect.

Related posts

Leave a Reply

2 comments

  1. When you set a page to be the page for posts it no longer operates like a vanilla page, the template hierarchy dictates a different set of templates for that page. Your selected template doesn’t appear to work, because it’s not being loaded.

    See the template hierarchy for more info. You should name your template home.php if you want it to be used on the page for posts page.

  2. Like Milo says the Template Hierarchy determines what template gets used. If you want your blog posts to go on the blog page you have 2 options.

    1. Set your home page to a static page and choose blog for your posts page in settings reading. Then you would need to make your changes to index.php. You would get the latest posts and the page would be named blog.

      • If you do this there would be no point in adding a custom query because WordPress will have already queried the posts. You can add a filter to pre_get_posts if you need to alter this query.
    2. Set your home page to static and don’t choose a blog post page then create a custom template like you did in your sample code. However in your code sample there really isn’t a need to set $wp_query to $temp. Just rename your new WP_Query $my_query or something.