我在这篇文章中找到了一个很好的解决方案:How to add wordpress username after url?
/*
Plugin Name: My Awesome Button
Description: The shortcode [my-awesome-button] will be replaced with the HTML code for a button for logged-in users and for guests it will be replaced with an empty string (so it will be removed). This will work for posts and pages.
Author: Nikolay Nikolov
Version: 1.0.0
*/
add_filter( \'the_content\', \'my_awesome_button_function\', 99999999999 );
function my_awesome_button_function( $content ) {
if ( strpos( $content, \'[my-awesome-button]\' ) !== false ) {
if ( is_user_logged_in() ) {
$current_user = wp_get_current_user();
$button_html = \'<a href="https://direktoriku.com/shopping/?user=\' . esc_attr( $current_user->user_login ) . \'"><button>Click me</button></a>\';
$content = str_replace( \'[my-awesome-button]\', $button_html, $content );
} else {
$content = str_replace( \'[my-awesome-button]\', \'\', $content );
}
}
return $content;
}
我唯一的问题是,该按钮仅在主页上处于活动状态,而在网站的所有其他页面(例如mydomain.com/documents)上处于非活动状态。
请你帮我把它修好。
非常感谢!
SO网友:Baikare Sandeep
您可以在此插件文件中尝试此短代码,也可以将此方法粘贴到活动主题的底部functions.php. 如果您是WordPress新手,需要帮助,可以访问here 有关在函数中添加代码的步骤。
add_shortcode( \'my-awesome-button\', \'wp_my_awesome_bytton\' );
function wp_my_awesome_bytton( $atts ) {
$button_html = \'\';
if ( is_user_logged_in() ) {
$current_user = wp_get_current_user();
$button_html = \'<a href="https://direktoriku.com/shopping/?user=\' . esc_attr( $current_user->user_login ) . \'"><button>Click me</button></a>\';
}
return $button_html;
}
//在页面/帖子内容中的任何位置使用此短代码,如下所示:
[my-awesome-button] 它会将您的短代码替换为html代码。有关shortcode的详细信息
visit here.