是否在子菜单链接上方显示登录问候语?

时间:2013-12-10 作者:eclipsis

我正在使用WordPress的内置菜单系统输出我的主菜单结构。我有一个子菜单,目前有两个链接,“跟踪”和“待办事项列表”。我希望在这两个链接上方显示以下登录问候语:

<?php global $current_user; if ( is_user_logged_in() ) { echo \'Welcome, \'.$current_user->first_name.\'\'; } else { echo \'Welcome, visitor\'; } ?>
    <font id="spacer">•</font>
<?php add_modal_login_button( $login_text = \'Login\', $logout_text = \'Logout\', $logout_url = \'\', $show_admin = false ); ?>
如果没有人登录,则输出如下:Welcome, visitor • Login

如何在两个子菜单链接上方显示此输出?

1 个回复
SO网友:Chittaranjan

从您的代码来看,您似乎正在使用此模式登录插件http://wordpress.org/plugins/wp-modal-login/ 考虑到这一点,我提供了必要的代码来将菜单项添加到菜单中。

您需要使用wp_nav_menu_items 内钩如下functions.php 活动主题的文件。

// Add the hook to the nav menu items
add_filter( \'wp_nav_menu_items\', \'wti_loginout_menu_link\', 10, 2 );

function wti_loginout_menu_link( $items, $args ) {
    // Get the global object for user and the modal login class
    global $current_user, $wp_modal_login_class;

    if ( $args->theme_location == \'primary\' ) {
        $items .= \'<li>\';

        if ( is_user_logged_in() ) {
            $items .= \'Welcome, \' . $current_user->user_nicename;
        } else {
            $items .= \'Welcome, visitor\';
        }

            // Add the modal menu to the nav menu
        $items .= \' \' . $wp_modal_login_class->modal_login_btn( \'Login\', \'Logout\', \'\', false );

        $items .= \'</li>\';
    }

    return $items;
}

Few things to note:

<上述代码适用于primary 主题位置。您需要将其更改为主题位置。

我正在使用user_nicename, 您可以使用first_name 用户对象的。

这里也讨论了上述挂钩http://www.webtechideas.com/adding-login-logout-link-to-wordpress-menu/

结束

相关推荐

在自定义的Single-Portfolio.php中使用COMMENTS_TEMPLATE

我希望我的用户能够在我的公文包页面上发表评论。我用自定义帖子创建了一个公文包页面。在我的博客上,我正在使用comments\\u template();然后是评论。php我有代码。在博客方面,它100%有效但是当我添加comments\\u template()时;在公文包单页上,它什么都不做。我没有收到任何错误或任何东西。人们如何在公文包页面中实现评论?您不能使用相同的注释。所有注释的php文件,b谢谢