WordPress-动态添加用户ID到URL的末尾
我正在寻找一种将当前登录的用户名添加到链接末尾的方法。
example: www.mywebsite.com/custom-post-type/author-post-title
“作者 - 命名”将被用户名中的当前登录所取代
此链接将导致一个私人帖子,该帖子将自动使用重力表格创建。帖子标题与已登录的人的用户名相同。我希望此链接事先动态地到位。每个作者仅限于一个帖子,并且已经必须登录用户才能查看NAV菜单中的链接。
我认为有一种方法可以在其中编写作者名称是占位符,并且被当前登录的用户ID代替,但我无法弄清楚。类似...但是,这是我新来的PHP。
<?php
function replace_text($text) {
$text = str_replace('author-post-title', '$user_id', $text);
$user_id = get_current_user_id();
return $text;
}
?>
I'm looking for a way to have the current logged in username added to the end of a link.
example: www.mywebsite.com/custom-post-type/author-post-title
"author-post-title" would be replaced with the current logged in username
This link will lead to a private post that is automatically created using Gravity Forms. The post title is the same as the logged-in person's username. I would like this link to be in place dynamically beforehand. Each author is limited to only one post and users already have to be logged in to see the link in nav menu.
I'm thinking there is a way to code this where the author name is a placeholder and is replaced by the currently logged in user ID, but I have not been able to figure it out. Something like... but this is way off as I am new to php.
<?php
function replace_text($text) {
$text = str_replace('author-post-title', '$user_id', $text);
$user_id = get_current_user_id();
return $text;
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议您制作为所有用户具有相同URL的通用页面,并为特定用户修改其内容。将此链接附加到导航菜单会更容易。
但是,如果您确实需要创建此类链接,则可以使用函数创建它:
那么您可能需要为此链接结构创建自定义重写规则,以显示用户的特定页面:
I would recommend you to make generic page which has same URL for all users and modify it's content for specific user. It will be easier to attach this link to nav menu.
But, if you really need to create this kind of link you may create it using function:
Then you may want to create custom rewrite rule for this link structure to display specific page for user:
您可以尝试将以下短代码添加到您的页面:
[repleast_link_w_user link =“ 在这里添加您的链接,而无需用户名“ button =” 在此处添加按钮文本”]
然后将以下内容添加到您的functions.php:php:
You could try adding the following shortcode to your page:
[replace_link_w_user link="add your link here without username" button="Add button text here"]
Then add the following to your functions.php: