如果/ Else陈述

时间:2015-02-15 04:07:39

标签: php wordpress

我在放置IF / ELSE语句时遇到问题而不会破坏页面的其余部分。

我的页面上有以下横幅:

    <div class="awesome-banner">

                    <div class="image-wrap<?php echo $banner ? '' : ' awesome-hide'; ?>">
                        <?php $banner_url = $banner ? wp_get_attachment_url($banner) : ''; ?>
                        <input type="hidden" class="awesome-file-field" value="<?php echo $banner; ?>" name="awesome_banner">
                        <img class="awesome-banner-img" src="<?php echo esc_url($banner_url); ?>">
                        <a class="close awesome-remove-banner-image">&times;</a>
                    </div>



                    <div class="button-area<?php echo $banner ? ' awesome-hide' : ''; ?>">
                        <i class="fa fa-cloud-upload"></i>
                        <a href="#" class="awesome-banner-drag awesome-btn awesome-btn-info"><?php _e('Upload banner', 'awesome'); ?></a>
                        <p class="help-block"><?php _e('(Upload a banner for your profile. Banner size is (825x300) pixel. )', 'awesome'); ?></p>
                    </div>
                </div> <!-- .awesome-banner -->

<?php do_action('awesome_settings_after_banner', $current_user, $profile_info); ?>

我需要在此横幅内放置以下声明(请纠正错误):

<?
if ((current_user_can('manager')) )
{ ?>    
    <div class="access-restricted">
      <h5>Sign In Or Sign Up</h5>
       <p class="non-manager-notice">You need to be a manager to upload a banner</p>
       <div class="upgrade-button">
         <a href="#" class="button-green-upgrade">Upgrade Account</a>
       </div>
    </div>  
<?  
    return;
    } else{ 
    return false;
}                   
?>

所以基本上应该在这个横幅中说明以下内容:

如果当前用户是用户角色"manager",则应显示<div class="button-area">

而不是<div class="access-restricted">

否则只需正常继续。

我试图将语句直接放在内部,如下所示:

    <div class="awesome-banner">

                        <div class="image-wrap<?php echo $banner ? '' : ' awesome-hide'; ?>">
                            <?php $banner_url = $banner ? wp_get_attachment_url($banner) : ''; ?>
                            <input type="hidden" class="awesome-file-field" value="<?php echo $banner; ?>" name="awesome_banner">
                            <img class="awesome-banner-img" src="<?php echo esc_url($banner_url); ?>">
                            <a class="close awesome-remove-banner-image">&times;</a>
                        </div>

                        // Here I am placing my statement like this
                        <?
        if ((current_user_can('manager')) )
        { ?>    
            <div class="access-restricted">
              <h5>Sign In Or Sign Up</h5>
               <p class="non-manager-notice">You need to be a manager to upload a banner</p>
               <div class="upgrade-button">
                 <a href="#" class="button-green-upgrade">Upgrade Account</a>
               </div>
            </div>  
        <?  
            return;
            } else{ 
            return false;
        }                   
        ?>
    // End of my statement

                        <div class="button-area<?php echo $banner ? ' awesome-hide' : ''; ?>">
                            <i class="fa fa-cloud-upload"></i>
                            <a href="#" class="awesome-banner-drag awesome-btn awesome-btn-info"><?php _e('Upload banner', 'awesome'); ?></a>
                            <p class="help-block"><?php _e('(Upload a banner for your profile. Banner size is (825x300) pixel. )', 'awesome'); ?></p>
                        </div>
                    </div> <!-- .awesome-banner -->

<?php do_action('awesome_settings_after_banner', $current_user, $profile_info); ?>

它正确显示了该角色的内容,但它打破了令人敬畏的横幅div后面页面上的其他内容。

我是否正确地结束了我的陈述和其他一切?感谢

1 个答案:

答案 0 :(得分:1)

除了my(和@ maiorano84的评论)之外,如果满足任一条件(true或false),return将停止代码:

if(current_user_can('manager')) { ?>    
    <div class="access-restricted">
        <h5>Sign In Or Sign Up</h5>
        <p class="non-manager-notice">You need to be a manager to upload a banner</p>
        <div class="upgrade-button">
            <a href="#" class="button-green-upgrade">Upgrade Account</a>
        </div>
    </div><?
        // If you need to record a true or false here, assign to a variable.
        // Don't return or your code will stop here.
        $manager = true;
    }
else { ?>
    <div class="button-area<?php if($banner) echo ' awesome-hide'; ?>">
        <i class="fa fa-cloud-upload"></i>
        <a href="#" class="awesome-banner-drag awesome-btn awesome-btn-info"><?php _e('Upload banner', 'awesome'); ?></a>
        <p class="help-block"><?php _e('(Upload a banner for your profile. Banner size is (825x300) pixel. )', 'awesome'); ?></p>
    </div><?php 
        // If you need true/false, assign here
        $manager = false;
    } ?>