Adding if statement

时间:2019-11-14 作者:Scott

我对PHP还很陌生,但是我成功地建立了一个我很满意的网站。我遇到的一个小问题是,我想为具有特定角色的用户添加内容,这意味着在我预先存在的语句中间添加一个if语句。

以下是我当前的代码:

<?php global $current_user; wp_get_current_user(); ?>
        <?php if ( is_user_logged_in() ) { 
         echo \'<ul style="padding: 0; margin: 0;">
                    <li class="adminBar" style="float: left; line-height: 60px; border-left: 1px solid #555555; border-right: 1px solid #555555;"><a href="#" target="_blank"><img src="#" height="40px" style="padding-left: 15px; padding-right: 15px;"></a></li>
                    <li class="adminBar" style="float: left; line-height: 60px; border-right: 1px solid #555555;"><a href="#" target="_blank"><img src="#" height="40px" style="padding-left: 15px; padding-right: 15px;"></a></li>
                    <li class="adminBar" style="float: left; line-height: 60px; border-right: 1px solid #555555;"><a href="#" target="_blank"><img src="#" height="40px" style="padding-left: 15px; padding-right: 15px;"></a></li>
                    <li class="adminBar" style="float: left; line-height: 60px; border-right: 1px solid #555555;"><a href="#" target="_blank"><img src="#" height="40px" style="padding-left: 15px; padding-right: 15px;"></a></li>
                    <li class="adminBar" style="float: left; line-height: 60px; border-right: 1px solid #555555;"><a href="#" target="_blank"><img src="#" height="40px" style="padding-left: 15px; padding-right: 15px;"></a></li>
                    <li id="logoutDrop" class="loginBtn" style="background-color: #27618d; float: right;"><a style="text-decoration: none; text-transform: none; line-height: 60px; color: #fff; padding: 15px 20px 15px 20px;" href=""\' . wp_logout_url() . \'">Logout</a>\' . \'
                    <li id="logoutDrop" style="border-right: 1px solid #555; padding: 0 15px 0 15px; float: right;"><a style="text-transform: none; text-decoration: none; line-height: 60px; color: #fff;">Hi, \' . $current_user->user_login . \'</a>\' . \'

                        <ul style="display: none;">
                            <li><a href="#" style="">Logout</a></li>
                        </ul>
                    </li>\' .
                \'</ul>\' . "\\n";} 

        else  { echo \'
            <div id="stmposTitle">
            <a href="/" style="text-decoration: none; float: left; text-transform: none; line-height: 35px; color: #fff;">
            <img src="#" style="margin-top: 2px; float:left; padding-right: 10px;">Name</a></div>
            <a class="loginBtn" style="float: right; line-height: 35px; color: #fff; background-color: #27618d; padding: 0 15px; 0 15px; text-decoration: none;" href="\' . wp_login_url() . \'">Login</a>\' ;} ?>
我本质上是想允许它,以便只有特定角色的登录用户才能看到该菜单的一部分。如下所示,但这并不完全正确,因为它会阻止站点加载:

<?php global $current_user; wp_get_current_user(); ?>
        <?php if ( is_user_logged_in() ) { 
         echo \'<ul style="padding: 0; margin: 0;">
                    <li class="adminBar" style="float: left; line-height: 60px; border-left: 1px solid #555555; border-right: 1px solid #555555;"><a href="#" target="_blank"><img src="#" height="40px" style="padding-left: 15px; padding-right: 15px;"></a></li>
                    <li class="adminBar" style="float: left; line-height: 60px; border-right: 1px solid #555555;"><a href="#" target="_blank"><img src="#" height="40px" style="padding-left: 15px; padding-right: 15px;"></a></li>
                    <li class="adminBar" style="float: left; line-height: 60px; border-right: 1px solid #555555;"><a href="#" target="_blank"><img src="#" height="40px" style="padding-left: 15px; padding-right: 15px;"></a></li>
                    <li class="adminBar" style="float: left; line-height: 60px; border-right: 1px solid #555555;"><a href="#" target="_blank"><img src="#" height="40px" style="padding-left: 15px; padding-right: 15px;"></a></li>
                    <li class="adminBar" style="float: left; line-height: 60px; border-right: 1px solid #555555;"><a href="#" target="_blank"><img src="#" height="40px" style="padding-left: 15px; padding-right: 15px;"></a></li>\'

                    <?php if( current_user_can(\'editor\') || current_user_can(\'administrator\') ) {  ?>
                        content
                    <?php } ?>

                    \'
                    <li id="logoutDrop" class="loginBtn" style="background-color: #27618d; float: right;"><a style="text-decoration: none; text-transform: none; line-height: 60px; color: #fff; padding: 15px 20px 15px 20px;" href=""\' . wp_logout_url() . \'">Logout</a>\' . \'
                    <li id="logoutDrop" style="border-right: 1px solid #555; padding: 0 15px 0 15px; float: right;"><a style="text-transform: none; text-decoration: none; line-height: 60px; color: #fff;">Hi, \' . $current_user->user_login . \'</a>\' . \'

                        <ul style="display: none;">
                            <li><a href="#" style="">Logout</a></li>
                        </ul>
                    </li>\' .
                \'</ul>\' . "\\n";} 


        else  { echo \'
            <div id="stmposTitle">
            <a href="/" style="text-decoration: none; float: left; text-transform: none; line-height: 35px; color: #fff;">
            <img src="#" style="margin-top: 2px; float:left; padding-right: 10px;">Name</a></div>
            <a class="loginBtn" style="float: right; line-height: 35px; color: #fff; background-color: #27618d; padding: 0 15px; 0 15px; text-decoration: none;" href="\' . wp_login_url() . \'">Login</a>\' ;} ?>
提前感谢您的帮助!

2 个回复
SO网友:Chetan Vaghela

我已格式化您的代码并中断echo 语句分为多个部分并添加if 条件您可以从下面的代码中获取参考。

 <?php if ( is_user_logged_in() ) { 
         echo \'<ul style="padding: 0; margin: 0;">
            <li class="adminBar" style="float: left; line-height: 60px; border-left: 1px solid #555555; border-right: 1px solid #555555;"><a href="#" target="_blank"><img src="#" height="40px" style="padding-left: 15px; padding-right: 15px;"></a></li>
            <li class="adminBar" style="float: left; line-height: 60px; border-right: 1px solid #555555;"><a href="#" target="_blank"><img src="#" height="40px" style="padding-left: 15px; padding-right: 15px;"></a></li>
            <li class="adminBar" style="float: left; line-height: 60px; border-right: 1px solid #555555;"><a href="#" target="_blank"><img src="#" height="40px" style="padding-left: 15px; padding-right: 15px;"></a></li>
            <li class="adminBar" style="float: left; line-height: 60px; border-right: 1px solid #555555;"><a href="#" target="_blank"><img src="#" height="40px" style="padding-left: 15px; padding-right: 15px;"></a></li>
            <li class="adminBar" style="float: left; line-height: 60px; border-right: 1px solid #555555;"><a href="#" target="_blank"><img src="#" height="40px" style="padding-left: 15px; padding-right: 15px;"></a></li>\' ; 

            if( current_user_can(\'editor\') || current_user_can(\'administrator\') ) {  
                ?>
                # add you content here
                <?php 
            } 

           echo  \'<li id="logoutDrop" class="loginBtn" style="background-color: #27618d; float: right;"><a style="text-decoration: none; text-transform: none; line-height: 60px; color: #fff; padding: 15px 20px 15px 20px;" href=""\' . wp_logout_url() . \'">Logout</a>\' . \'
            <li id="logoutDrop" style="border-right: 1px solid #555; padding: 0 15px 0 15px; float: right;"><a style="text-transform: none; text-decoration: none; line-height: 60px; color: #fff;">Hi, \'.$current_user->user_login. \'</a>
              <ul style="display: none;">
                <li><a href="#" style="">Logout</a></li>
              </ul>
            </li>\' .
          \'</ul>\';
      }      
      else  
      { 
        echo \'<div id="stmposTitle">
        <a href="/" style="text-decoration: none; float: left; text-transform: none; line-height: 35px; color: #fff;">
        <img src="#" style="margin-top: 2px; float:left; padding-right: 10px;">Name</a></div>
        <a class="loginBtn" style="float: right; line-height: 35px; color: #fff; background-color: #27618d; padding: 0 15px; 0 15px; text-decoration: none;" href="\' . wp_login_url() . \'">Login</a>\' ;
      } 
  ?>

SO网友:Kristián Filo

首先,您的代码是错误的。不允许您将if 内部声明echo.

然而,如果您使用默认WordPress菜单(外观-菜单)创建菜单,这绝对是正确的方法,而不是硬编码,更好的方法是根据您的条件使用wp_nav_menu_objects 滤器

add_filter(\'wp_nav_menu_objects\', \'remove_menu_items\', 10, 2);

function remove_menu_items($removed_menu_objects, $args) {

    // if current user role is Editor or Administrator, do not remove anything
    if( current_user_can(\'editor\') || current_user_can(\'administrator\') )
        return $removed_menu_objects;

    // for any other user role, remove menu items with title of "Logout" and "Contact"
    $remove = array(\'Logout\', \'Contact\');

    foreach ($removed_menu_objects as $key => $menu_object) {
        if (in_array($menu_object->title, $remove)) {
            unset($removed_menu_objects[$key]);
        }
    }

    return $removed_menu_objects;
}
代码进入函数内部。活动子主题的php。您还可以比较菜单项ID,而不是它们的标题。