You have to add JFactory/getUser function to check if user is loged in and if not to use JApplication::redirect to redirect him to desired url.
$user = JFactory::getUser();
$app = JFactory::getApplication();
if (!$user->guest) {
echo "You are logged in";
} else {
$login_url = "index.php?option=com_users&view=login";
$app->redirect($login_url,'message to display','message');
}
EDIT:
There are built in methods in joomla to display content only to registered users without any coding.
For articles:
Content -> Article Manager -> *Article_name -> Access: Registered
For Menu Items:
Menus -> *Menu_name -> *Menu_item_title -> Access: Registered
There is also an option to set redirect link after login.
For Menu item login page:
Menus -> *Menu_Name -> New -> Menu Item Type: (Users Manager -> Login Form) -> Options -> Login Redirect
For Login Module:
Extensions -> Module Manager -> New -> Login -> Login Redirection Page
*A quick tip if you haven't login button in your menu is to create a new 'hidden' menu (that won't exist in any frontend position) and place the 'login redirection url' there.
Good Luck!