1

I am trying to set up my new theme in a proper way. I have created a custom home page for my new theme called "template-home.php" and then I created a "blog" page. I went to settings and changed my "Reading" settings to "A static page" and I set my front page to my home template and then I set my "Posts page" to "blog". I did not assign a custom template to that page. It is my understanding that the index.php file would be used to get those posts. I have been trying to use is_page() and is_category() to say that if it's the blog page, then get the blog posts. My hope was that when turning on the theme for the first time, all posts would be displayed on the front page but if I visited the blog page, the conditionals would kick in and get only the posts on the blog page. I thought I might be able to accomplish this task by using this code

add_action( 'pre_get_posts', 'blog_query');

function blog_query( $query ){
    if ( $query->is_page('blog') ) {
    $query->set( 'category_name', 'blog' );
   }    
}

That isn't working. That gives me an error on the front page "Trying to get property of non object" I am just not sure which method to use to set up the blog page. Should I set a custom template for it and let the index page get all posts or try using the pre_get_posts?

Your input is appreciated.

2

Your blog page is actually your home page, so you need to use is_home(). For the static frontpage, is_front_page() would be used

EDIT

Sorry for being confusing :-). This how it works when a static frontpage is set

The page set as your frontpage is actaully just a cover page. When you think of a book, this is the page that will tell the reader what the book is all about. The page used here will be the custom page template set by you. The correct naming should be front-page.php, but any page template will do. The proper conditional tag to use here will be is_front_page()

Now, the blogpage, whether explicitely set or not, will either use home.php, and if that is not available, will use index.php. Not many people knows this, but this is regarded as your site's homepage, the normal index page you will see when a static frontpage is not set. To target your blogpage, you'll need to use is_home() conditional check. Strange, but true

To restrict your blogpage to one category only, use pre_get_posts as you did, just change your conditional tag accordingle

function only_one_cat_blogpage($query) {
    if( !is_admin() && $query->is_home() && $query->is_main_query() ){
        $query->set( 'cat', 'ID OF CATEGORY' );
    }
}
add_action( 'pre_get_posts', 'only_one_cat_blogpage' );

EDIT 2

For further reading, check out

  • Sorry Pieter, but your answer doesn't make any sense to me. The blog page can't be my front page if I set a static front page as a template. So I am trying to filter the posts that go to the blog page through the index loop so that only the posts in the blog category will show, otherwise, it should get all posts – Jamie Sep 6 '14 at 2:15
  • Actually he is right. You will need to use is_home() for this. – Robert hue Sep 6 '14 at 2:52
  • @Jamie please see my edit. Hope it make more sense now – Pieter Goosen Sep 6 '14 at 4:50
  • I think it would be more correct to say that I didn't understand your explanation. It may have made perfect sense. n any case, your solution isn't working for me. I got an error with the $query->is_main_query. I had to take it out. But it also makes the home page show the blog posts and the blog page show the blog posts when "Show latest posts" is selected. I am trying to filter just the blog posts for the blog page through the index instead of setting up a separate template. – Jamie Sep 6 '14 at 6:20
  • 1
    is_front_page() will not work codex.wordpress.org/Plugin_API/Action_Reference/… – Brad Dalton Feb 12 '15 at 17:52
1

Try this. As mentioned by Pieter you will have to check for is_home() and set conditions accordingly.

function show_category_posts( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'category_name', 'blog' );
    }
}
add_action( 'pre_get_posts', 'show_category_posts' );
  • This is what I ended up with. This gave me the best result.. Adding 'page' == get_option('show_on_front') seems to have worked the best – Jamie Sep 6 '14 at 6:40
  • @Jamie, please post your comment as an answer if that solved your problem, and also accept your own answer :-) – Pieter Goosen Sep 6 '14 at 6:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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