Thanks xdesi! I have figured it out!
I did a bit of investigation which temlate file is used for pages, which for blog.
So in my case index.php is used for blog. So I have added <h2><?php echo get_the_title($page_id) ?></h2>
. So now it is completely flexible. If I assign blog to any page it works correctly.
Otherwise, the_title()
returns the title of the post (if used in blog template file), not the page title. So if you use it outside of the loop, you just get the first post title.