Magento hide navigation menu item from guest

2019-07-27 05:31发布

问题:

I'm running a magento custom template on magento community edition 1.7. How do I hide $custom_tab3 from guests? I want that tab to only show up for logged in users. Please see code below. Any help is super highly appreciated!

 <ul id="nav">
    <?php if (Mage::getStoreConfig('celebritysettings/celebritysettings_header/navigation_home')): ?>
     <li class="level0 level-top">
        <a href="<?php echo $this->getBaseUrl(); ?>how-it-works"><span><?php echo $this->__('How It Works'); ?></span></a>
     </li>
 <?php endif; ?>
    <?php
    echo $_menu;
    $custom_tab = Mage::getModel('cms/block')->load('celebrity_navigation_block');
    if($custom_tab->getIsActive()) {
        echo '
        <li class="level0 level-top parent custom-block">
            <a href="'.$this->getBaseUrl().'gift" class="level-top">
                <span>'.$custom_tab->getTitle().'</span>
            </a>
            <div class="sub-wrapper">'.$this->getLayout()->createBlock('cms/block')->setBlockId('celebrity_navigation_block')->toHtml().'</div>
        </li>';
    }

    $custom_tab2 = Mage::getModel('cms/block')->load('celebrity_navigation_block2');
    if($custom_tab2->getIsActive()) {
        echo '
        <li class="level0 level-top parent custom-block" >
            <a href="'.$this->getBaseUrl().'plans" class="level-top">
                <span>'.$custom_tab2->getTitle().'</span>
            </a>
            <div class="sub-wrapper">'.$this->getLayout()->createBlock('cms/block')->setBlockId('celebrity_navigation_block2')->toHtml().'</div>
        </li>';
    }


    $custom_tab3 = Mage::getModel('cms/block')->load('celebrity_navigation_block3');
    if($custom_tab3->getIsActive()) {
        echo '
        <li class="level0 level-top parent custom-block">
            <a href="'.$this->getBaseUrl().'showroom" class="level-top">
                <span>'.$custom_tab3->getTitle().'</span>
            </a>
            <div class="sub-wrapper">'.$this->getLayout()->createBlock('cms/block')->setBlockId('celebrity_navigation_block3')->toHtml().'</div>
        </li>';
    }


    $custom_tab4 = Mage::getModel('cms/block')->load('celebrity_navigation_block4');
    if($custom_tab4->getIsActive()) {
        echo '
        <li class="level0 level-top parent custom-block">
            <a href="'.$this->getBaseUrl().'magazine" class="level-top">
                <span>'.$custom_tab4->getTitle().'</span>
            </a>
            <div class="sub-wrapper">'.$this->getLayout()->createBlock('cms/block')->setBlockId('celebrity_navigation_block4')->toHtml().'</div>
        </li>';
    }

    ?>
</ul>

回答1:

You can use Mage::getSingleton('customer/session')->isLoggedIn() to check whether the current user is logged in. In your code you can use it like this:

$custom_tab3 = Mage::getModel('cms/block')->load('celebrity_navigation_block3');
if($custom_tab3->getIsActive() && Mage::getSingleton('customer/session')->isLoggedIn()) {
    echo '
    <li class="level0 level-top parent custom-block">
        <a href="'.$this->getBaseUrl().'showroom" class="level-top">
            <span>'.$custom_tab3->getTitle().'</span>
        </a>
        <div class="sub-wrapper">'.$this->getLayout()->createBlock('cms/block')->setBlockId('celebrity_navigation_block3')->toHtml().'</div>
    </li>';
}

or even

if(Mage::getSingleton('customer/session')->isLoggedIn()) {
    $custom_tab3 = Mage::getModel('cms/block')->load('celebrity_navigation_block3');
    if($custom_tab3->getIsActive()) {
        echo '
        <li class="level0 level-top parent custom-block">
            <a href="'.$this->getBaseUrl().'showroom" class="level-top">
                <span>'.$custom_tab3->getTitle().'</span>
            </a>
            <div class="sub-wrapper">'.$this->getLayout()->createBlock('cms/block')->setBlockId('celebrity_navigation_block3')->toHtml().'</div>
        </li>';
    }
}

to prevent unnecessary retrieval of the cms/block model.