• I visited this page to figure out how to make a page require a login before displaying its content. It works great but now it changes everything about how the page looks.

    My website is here: https://www.paulegavin.net , the problem page is “Students & Clients.”

    It looks fine if a user is logged in but looks different if they’re not. I’d like it to look the same as the rest of the site.

    I imagine I’m putting the “if login” in the wrong place. This is the code I currently have for the page template:

    <?php
    /*
    Template Name: Students & Clients
    */
    ?>
    
    <?php
    if(!is_home() ){
    	get_header(); ?>
    	<div id="<?php $meta = get_post_meta($post->ID, $field['id'], true); echo substr($meta['connection'][0], 1); ?>" class="section single">
    
    <?php
    }
    ?>
        <div class="heading">
          <div class="heading_cont">
    		<a href="#main" class="go_up"></a>
            <h1><?php the_title(); ?></h1>
            <span class="heading_bg"></span>
            <!--end heading_bg-->
          </div>
          <!--end heading_cont-->
        </div>
        <!--end heading-->
                            <?php if(is_user_logged_in()):?>
        <div class="content_left">
    
    	<?php
    	if(!is_home()){
    		while (have_posts()) { the_post();
    			the_content('');
    		}
    		wp_reset_query();  // Restore global post data
    	}
    	else
    	{
    		the_content('');
    	}?>
        </div>
    
        <!--end content_left-->
    
    	<div class="sidebar_right">
    		<ul class="sidebar">
    			<?php dynamic_sidebar( 'sidebar-right' ); ?>
    		</ul>
        </div>
        <!--end sidebar_right-->
    
    <?php
    if(!is_home()){?>
    	</div>
    <?php get_footer();
    }
    ?>
    <?php else:
    wp_die('Sorry, you must first <a href="/wp-login.php">log in</a> to view this page. You can <a href="/wp-login.php?action=register">register free here</a>.');
    endif;?>

    Thanks for your help!

Viewing 7 replies - 1 through 7 (of 7 total)
  • How much are you trying to hide? Just the content? The sidebar as well?

    You’ve definitely got too much hacked off here, we’re totally killing some required elements of the page…

    Thread Starter paulegavin

    (@paulegavin)

    It’s at the very bottom if this is what you’re talking about.

    <?php else:
    wp_die('Sorry, you must first <a href="/wp-login.php">log in</a> to view this page. You can <a href="/wp-login.php?action=register">register free here</a>.');
    endif;?>

    Sorry, I saw that after my initial post, and updated my response to see exactly what you are trying to keep hidden

    You are definitely cutting off way too much, I think we just want to wrap the_content portion if that is all you are trying to hide – presently, you have the sidebar and footer getting lopped off

    Thread Starter paulegavin

    (@paulegavin)

    I just want to cut off the content, any idea where exactly I should put it?

    Something like this… (I would think)

    <?php
    /*
    Template Name: Students & Clients
    */
    ?>
    
    <?php
    if(!is_home() ){
    	get_header(); ?>
    	<div id="<?php $meta = get_post_meta($post->ID, $field['id'], true); echo substr($meta['connection'][0], 1); ?>" class="section single">
    
    <?php
    }
    ?>
        <div class="heading">
          <div class="heading_cont">
    		<a href="#main" class="go_up"></a>
            <h1><?php the_title(); ?></h1>
            <span class="heading_bg"></span>
            <!--end heading_bg-->
          </div>
          <!--end heading_cont-->
        </div>
        <!--end heading-->
    
        <div class="content_left">
    
    	<?php if(is_user_logged_in()):?>
    
    	<?php
    	if(!is_home()){
    		while (have_posts()) { the_post();
    			the_content('');
    		}
    		wp_reset_query();  // Restore global post data
    	}
    	else
    	{
    		the_content('');
    	}?>
    
    	<?php else:
    	wp_die('Sorry, you must first <a href="/wp-login.php">log in</a> to view this page. You can <a href="/wp-login.php?action=register">register free here</a>.');
    	endif;?>
    
        </div>
    
        <!--end content_left-->
    
    	<div class="sidebar_right">
    		<ul class="sidebar">
    			<?php dynamic_sidebar( 'sidebar-right' ); ?>
    		</ul>
        </div>
        <!--end sidebar_right-->
    
    <?php
    if(!is_home()){?>
    	</div>
    <?php get_footer();
    }
    ?>

    Thread Starter paulegavin

    (@paulegavin)

    Thanks for your help, that didn’t seem to change anything though.

    I’m actually a bit confused as to what is being output on your theme.

    What I’m seeing in your source code doesn’t match what is above in your code.

    For instance, within your source, your entire get_header seems to be outputting twice. Is that file the entirety of your page template?

    EDIT – actually, I think the problem is triggered by the wp_die command. wp_die is not something I’m wholly familiar with, but reading the docs

    https://codex.www.ads-software.com/Function_Reference/wp_die

    It doesn’t seem the most desireable command. Rather than doing that, couldn’t you just echo/print out the ‘sorry you must be logged in’ statement?

    If everything is wrapped in the conditioanl already, I would think that would be sufficient. If logged in, see the_content, if not, see your echoed ‘If not logged in’ statement

    <?php else: ?>
    	<p>Sorry, you must first <a href="/wp-login.php">log in</a> to view this page. You can <a href="/wp-login.php?action=register">register free here</a>.</p>
    	<?php endif;?>

    Similar to that? (the html would need adjusted I’m sure, just an example)

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Using if(is_user_logged_in()) changes page appearance’ is closed to new replies.