1

What I have:

A PHP function that outputs a log in/out link based on whether the user is correspondingly logged in/out.

<a href="foo">bar</a>

What I need:

I need a span wrapped around the link text inside the anchor element.

<a href="foo"><span>bar</span></a>

My code:

add_filter('wp_nav_menu_items', 'add_login_logout_link', 10, 2);
function add_login_logout_link($items, $args) {

        ob_start();
        wp_loginout('index.php');
        $loginoutlink = ob_get_contents();
        ob_end_clean();

        $items .= '<li>'. $loginoutlink .'</li>';

    return $items;
}

I've checked the wp_loginout() function for a potential parameter but the two that exist do not apply:

<?php wp_loginout( $redirect, $echo ); ?>

My question:

How can I wrap a span inside the anchor using a server-side approach. I don't want to have to resort to client-side approaches like JavaScript.

4

1 回答 1

1

尝试,

wp_logout_url ( string $redirect = '' )函数代替wp_loginout('index.php')

例子,

    ob_start();
    wp_logout_url('index.php');
    $logoutlink= ob_get_contents();
    ob_end_clean();

    $items .= '<a href="'.$logoutlink.'"'><span></span></a>;

用于is_user_logged_in()检查天气用户是否登录。

ob_start();
if (is_user_logged_in()) {
    wp_logout_url('index.php');
} else {
    site_url('index.php') 
}
于 2015-12-20T04:29:08.077 回答