Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to get my sidebar to change when a user is logged in and i'm using the is_user_logged_in function. Here is the code -

<!-- BEGIN sidebar -->

<?php
/**
 * The Sidebar containing the primary and secondary widget areas.
 *
 * @package WordPress
 * @subpackage Twenty_Ten
 * @since Twenty Ten 1.0
 */
?>

        <div id="primary" class="widget-area" role="complementary">
            <ul class="xoxo">

<?php
    /* When we call the dynamic_sidebar() function, it'll spit out
     * the widgets for that widget area. If it instead returns false,
     * then the sidebar simply doesn't exist, so we'll hard-code in
     * some default sidebar stuff just in case.
     */
    if ( ! dynamic_sidebar( 'primary-widget-area' ) ) : ?>


<li class="widget-container">
<h3 class="widget-title">Sign In / Register</h3>
<ul>    
<?php if ( is_user_logged_in() ) { echo 'Welcome, registered user!';} else { echo 'Welcome, unregistered user!';} ?>
</ul>
</li>


    <li id="meta" class="widget-container">
                <h3 class="widget-title"><?php _e( 'Meta', 'twentyten' ); ?></h3>
                <ul>
                    <?php wp_register(); ?>
                    <li><?php wp_loginout(); ?></li>
                    <?php wp_meta(); ?>
                </ul>
            </li>

        <?php endif; // end primary widget area ?>
            </ul>
        </div><!-- #primary .widget-area -->



<!-- END sidebar -->

When the user is logged in the sidebar shows what it's supposed to. When the user logs out, it disappears completely. What am I doing wrong?

share|improve this question
    
<ul> needs to be followed by a <li>, but besides that the code looks fine, maybe the problem is in another place. –  onetrickpony Jan 25 '11 at 0:52
    
Eh maybe. The UL and LI tags are like that... because that's the default twentyten code. I fixed it and everything went wonky. I can't find the problem, guessing this function just doesn't work on widgetized sidebars. –  Lynne Jan 25 '11 at 1:56
add comment

1 Answer

you warped your code inside

 if ( ! dynamic_sidebar( 'primary-widget-area' ) ) : ?>
     // code here

 ?php endif;

witch means that if you have any widgets on that sidebar the the code within the IF will not work. its basically using the code inside the if to use as default widgets and once you have a widget on the sidebar it ignores it.

so if your sidebar does have any widgets you need to run your is_user_logged_in function inside a PHP capable widget.

hope this helps

share|improve this answer
    
There were no widgets on the sidebar –  Lynne Jan 25 '11 at 5:25
    
@Lynne - are you sure? because i just ran your code and it didn't work the first time, i looked at my sidebar, there were some widgets there by default, i removed them and it show's up fine. and yes i was using the 2010 theme. –  Bainternet Jan 25 '11 at 5:43
    
ahh, ok one quick question, do you have any kind of cache running? –  Bainternet Jan 25 '11 at 11:21
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.