Set a static front page and blog page programmatically in WordPress

In WordPress, default front page display option is set to Your latest posts. You can change it to a static page using the Front page displays option, located at Dashboard->Settings->Reading->Front page displays.

You can do them same using code, all you need to is paste the following code snippet to your functions.php file.

//Set static front page and blog page programmatically in WordPress
function set_static_front_page(){
        // set the static front page
	$home = get_page_by_title('Home');
	update_option('page_on_front',$home->ID);
	update_option('show_on_front','page');
	
	// set the blog page
	$blog = get_page_by_title('Blog');
	update_option('page_for_posts',$blog->ID);
}
add_action('init','set_static_front_page');

Make sure, Home and Blog pages exist, if not, create them.

Useful Read: How to Remove WordPress Logo From Admin Bar

0 Comments

Leave a Comment.