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.