wordpress - 在 php 中手动设置帖子的作者
我正在使用一个 WordPress 主题,允许用户创建帖子(他们不需要登录)。问题是,当显示帖子时, 始终显示为“admin”。创建后表单有一个名称字段,我可以在functions.php中获取该名称字段,但是如何手动将帖子的作者更改为在表单字段中输入的名称?
I'm using a wordpress theme that allows users to create posts (they don't need to be logged in). The problem is that when the posts are displayed, <?php the_author(); ?>
always shows as "admin". The post-creation form has a name field that I can get at in functions.php, but how can I manually change the author of the post to the name that was entered in the form field?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在看不到上下文的情况下,我只能向正确的方向迈出一步。在主题的插件或functions.php中,您可以调用
wp_insert_post_data
过滤器来更改将作为帖子插入的数据。例如,如果您希望每个帖子都归属于 ID 为 5 的作者,您可以执行以下操作:我之前做过类似的操作,我有前端表单,我将其捕获为帖子并自动分配给预定义的作者我称之为 Web Monkey 或类似的东西;)查看此页面了解更多信息:http://codex.wordpress.org/Plugin_API/Filter_Reference/wp_insert_post_data。这个过滤器非常强大,因为您可以在数据到达数据库之前更改将成为您的帖子的数据。
Without seeing the context, I can only offer a step in the right direction. Within a plugin or functions.php in your theme, you can call on the
wp_insert_post_data
filter to change the data that will be inserted as your post. For instance, if you wanted every post to be attributed to author with ID 5, you could do something like:I've done something like this before where I have front end forms that I capture as posts and automatically assign to a predefined author that I call Web Monkey or something like that ;) Check out this page for more: http://codex.wordpress.org/Plugin_API/Filter_Reference/wp_insert_post_data. This filter is really powerful in that you can alter your data that will become your post in anyway before it hits the database.
您检查过 the_author_meta() 包含的内容吗? WP 必须为每个帖子附加一个用户 ID,因此无论用户在框中输入什么内容,它都有可能将用户 1 分配给这些帖子。如果没有看到自定义函数或不知道您正在使用什么主题来测试,那么排除故障将非常困难。
Have you checked what the_author_meta() contains? WP has to attach a userID to each post, so chances are it's assigning user 1 to those posts regardless of what they're entering into the box. Without seeing the custom function or knowing what theme you're using to test, it'll be pretty difficult to troubleshoot.