Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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/

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):

http://i49.tinypic.com/2caq0g.jpg

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

share|improve this question

2 Answers 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.

share|improve this answer
    
In my question, I already had done the part of setting the home page to a static page and choosing blog for the posts page in settings > reading. But when I loaded blog page, it just showed all the posts. Now you say that in order to modify this blog page I have to edit the index.php, so does that mean I have to edit index.php to add commenting to post and filter the posts and so forth? –  JohnMerlino Jul 12 '12 at 1:08
    
I made changes to index.php (the index.php for my theme) and still nothing –  JohnMerlino Aug 31 '12 at 19:25

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.

share|improve this answer
    
i want the page to be called blog, and I want to be able to customize the type of posts that display on page while maintaining commenting, etc. home.php is already in use as it serves as a home page. –  JohnMerlino Jul 11 '12 at 19:44
    
@JohnMerlino then don't set that page as the page for posts. you're doing your own query anyway. i'm not entirely clear on your site structure and in what context you're using home.php if you're also setting the page for posts. –  Milo Jul 11 '12 at 20:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.