最合适的回答,由SO网友:Suman A. 整理而成
根据注释更新,以下是更新的解决方案。
Note: 复制粘贴之前,不要忘记更改somePrefix 使用any other prefix以避免与任何其他短代码冲突。
Step 1: 确保已安装此筛选器functions.php (如果没有,请复制粘贴到那里)
add_filter(\'wp_nav_menu_items\', \'do_shortcode\');
Step 2: 使用此代码段返回作者URL。将此粘贴到
functions.phpadd_shortcode( \'somePrefix_author_url\', function() {
$author_link = get_author_posts_url( get_current_user_id() ); // original link
$without_protocol = trim( str_replace( array( \'http://\', \'https://\' ), \'\', $author_link ), \'/\' );
return $without_protocol; // returns author link without protocol
});
Step 3: 转到导航菜单,添加自定义菜单链接并使用
[somePrefix_author_url] 在
URL 字段和“我的个人页面”或名称字段中的其他内容。
Step 4: 从前端测试链接是否正常。
Reason for using link without protocol:
默认情况下
http/ without colon 附加到
shortcode 之后出于某种原因
this 使现代化这可能是一个bug,也可能不是,因为这种行为可能是故意的。无论如何,上述代码可以安全使用,因为在这种情况下,WP会自动将协议附加到自定义链接。
是的,这样就可以了。对于HTML链接,而不仅仅是输出URL的函数,请从代码段中使用以下内容:
echo \'<a href="\' . esc_url(get_author_posts_url( wp_get_current_user()->ID )) .\'">
My personal page
</a>\';
它应该输出超链接到当前用户作者页面的“我的个人页面”文本。
Additional Tip:您可以通过以下方式获取用户对象:
$currentUser = wp_get_current_user();
$currentUserID = $currentUser->ID;
$userAuthorLink = get_author_posts_url($currentUserID);
$userFirstName = $currentUser->first_name;
// and so on
而不是重复调用同一对象
wp_get_current_user() 和
global $current_user get_currentuserinfo();, 您可以调用用户对象一次,并使用其在提示中共享的属性。
看见WP_User Class 对于可用的对象属性(ID、first\\u name…)和方法(has\\u role()…)