Show listings and vendors only for registered users

Hello - I need to log into every page to stay logged in. Otherwise, each time I choose a menu item, I am logged out and have to log in again. When I deactivate WP Code Snippits, then the problem goes away. Here is my code snippit - I only want logged in uses to see the vendors and listings. It works, but then I get this weird behavior of logging out and in every time I click a menue item (logged in on ‘experts’ but not on ‘listings’, etc:

add_action('wp_footer', 'hide_results_for_logged_out_users');

function hide_results_for_logged_out_users() {
    if ( !is_user_logged_in() ) { 
        echo '
        <style>
        /* Hide search result details for vendors (experts) */
        .hp-vendors,
        .hp-vendor__header,
        .hp-vendor__content,
        .hp-vendor__footer {
          display: none !important; /* Hide all vendor (expert) details */
        }

        /* Hide service listings */
        .hp-listings,
        .hp-listing__header,
        .hp-listing__content,
        .hp-listing__footer,
        .hp-listing__attributes,
        .hp-listing__details {
          display: none !important; /* Hide all service listing details */
        }

        /* Display a friendly message asking users to create an account */
        .hp-page__content::before {
          content: "Please create a FREE account to view the full search results.";
          display: block;
          font-size: 1.5em;
          color: #555;
          text-align: center;
          margin: 20px 0;
        }
        </style>
        ';
    }
}

2 posts were merged into an existing topic: Plugin for menu restriction by role