猿问

WordPress 网址简码

我从未在 php 中编写过任何代码,但我制作了一个 WordPress 仪表板,需要根据用户名重定向到页面。我想要主页上有一个按钮,将登录的用户带到正确的页面,所以我制作了一个短代码(不知道这是否是最好的解决方案)。


到目前为止,通过网上搜索,我有这样的代码:


function url_sc() {

    $current_user = wp_get_current_user() {

        $username = $current_user->login_user {

            $username_link = "https://testwebpage.no/?username=<?php echo $username; ?>"

        }

    }

    return $username_link;

}


add_shortcode( 'username-url', 'url_sc' );

我不知道它是否会起作用,因为我收到此语法错误:


语法错误,意外的“返回”(T_RETURN)


有人可以向我解释为什么我会收到此错误,以及如何修复它吗?


ITMISS
浏览 127回答 3
3回答

慕虎7371278

试试这个代码。这将检查用户是否登录,并根据您给定的用户名检查您是否登录。function url_sc() {&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; $current_user&nbsp; = wp_get_current_user();&nbsp; &nbsp; $username&nbsp; &nbsp; &nbsp; = $current_user->user_login;&nbsp; &nbsp; $username_link = "https://testwebpage.no/?username=".$username;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; return $username_link;}add_shortcode( 'username-url', 'url_sc' );

婷婷同学_

我不是 PHP 专家,但我确实涉猎过。整个功能块布局错误,尝试;function url_sc() {&nbsp; &nbsp; $current_user = wp_get_current_user();&nbsp; &nbsp; $username = $current_user->user_login;&nbsp; &nbsp; $username_link = "https://testwebpage.no/?username=" . $username;&nbsp; &nbsp; return $username_link;}add_shortcode( 'username-url', 'url_sc' );您可以在这里更进一步,并在此代码块中创建按钮 html,然后它将输出为带有短代码的完整链接按钮。function url_sc() {&nbsp; &nbsp;$current_user = wp_get_current_user();&nbsp; &nbsp;$username = $current_user->user_login;&nbsp; &nbsp;$username_link = "https://testwebpage.no/?username=" . $username;&nbsp; &nbsp;$button = '<a href="' . $username_link . '" class="button" target="_blank">Go To My Profile Page</a>';&nbsp; &nbsp;return $button;}add_shortcode( 'username-url', 'url_sc' );

呼唤远方

那是因为你忘记了后面的分号:$username_link = "https://testwebpage.no/?username=<?php echo $username; ?>" 并且忘记转义php代码,这应该是:$username_link = "https://testwebpage.no/?username=". echo $username; ."";&nbsp; 所以完整的代码如下所示:function url_sc() {&nbsp; &nbsp; $current_user = wp_get_current_user() {&nbsp; &nbsp; &nbsp; &nbsp; $username = $current_user->login_user {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $username_link = "https://testwebpage.no/?username=". echo $username; ."";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return $username_link;}add_shortcode( 'username-url', 'url_sc' );
随时随地看视频慕课网APP
我要回答