将页面slug的默认值添加到高级自定义字段中的自定义字段
So I installed the Advanced custom fields plugin in wordpress and setup a custom field called page_slug and want to set the default value to the page slug of each page, is that possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您击中发布时,则将字段的值设置为页面slug的值。
如果您在创建帖子后更改sl,则不会更新teh字段,因为它正在检查null。如果您希望它始终进行更新,并且不打算将字段值是页面slug的其他任何内容,则可以删除if语句。
我希望我已经正确回答了这个问题,因为很难知道为什么需要这个问题,因为您应该可以使用。
This will set the value of the field to the page slug if it's empty so on creation of the page when you hit publish.
If you change the slug after the post has been created this won't update teh field as it's checking for null. If you want it to always be updated and don't intend the field value to be anything other that the page slug then you can remove the if statement.
I hope I've answered the question correctly as it's difficult to know why you'd need this as you should be able to use.
OP的问题是针对邮政邮政局的,保罗的答案非常适合此用例。但是,您可以非常稍微编辑保罗的答案以适合更多场景。例如,如果您不愿意打页标题。假设您正在使用Divi并尝试在ACF字段中输出页面标题。代码看起来像这样:
我的字段名为“ headline_text”,因此请注意,您可以在最后一行中对字段名称进行编辑。唯一的另一个更改是$ post- post_title而不是 - > post_name。
我希望这对某人有帮助。 :d
OP's question was for the Post Slug and Paul's answer is very specific to this use case. However, you can edit Paul's answer very slightly to fit more scenarios. For example, if instead of the slug you wanted to Page Title. Say you're using Divi and trying to output the page title in a ACF field. The code would look like this:
My field is named "headline_text", so note you can edit that in the last line to whatever your field name is. The only other change is $post->post_title instead of ->post_name.
I hope this is helpful to somebody. :D