将页面slug的默认值添加到高级自定义字段中的自定义字段

发布于 2025-02-03 17:23:02 字数 250 浏览 5 评论 0原文

因此,我在WordPress中安装了高级自定义字段插件,并设置了一个名为page_slug的自定义字段,并想将默认值设置为每个页面的页面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?

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月染过的梦 2025-02-10 17:23:02

如果您击中发布时,则将字段的值设置为页面slug的值。

function my_acf_load_value( $value, $post_id, $field ) {
    global $post;
    if(($value === null || trim($value) === '')) {
        $value = $post->post_name;
    }
    return $value;
}
add_filter('acf/load_value/name=page_slug', 'my_acf_load_value', 10, 3);

如果您在创建帖子后更改sl,则不会更新teh字段,因为它正在检查null。如果您希望它始终进行更新,并且不打算将字段值是页面slug的其他任何内容,则可以删除if语句。

我希望我已经正确回答了这个问题,因为很难知道为什么需要这个问题,因为您应该可以使用。

$post->post_name

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.

function my_acf_load_value( $value, $post_id, $field ) {
    global $post;
    if(($value === null || trim($value) === '')) {
        $value = $post->post_name;
    }
    return $value;
}
add_filter('acf/load_value/name=page_slug', 'my_acf_load_value', 10, 3);

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.

$post->post_name
李白 2025-02-10 17:23:02

OP的问题是针对邮政邮政局的,保罗的答案非常适合此用例。但是,您可以非常稍微编辑保罗的答案以适合更多场景。例如,如果您不愿意打页标题。假设您正在使用Divi并尝试在ACF字段中输出页面标题。代码看起来像这样:

function my_acf_load_value( $value, $post_id, $field ) {
    global $post;
    if(($value === null || trim($value) === '')) {
        $value = $post->post_title;
    }
    return $value;
}
add_filter('acf/load_value/name=headline_text', 'my_acf_load_value', 10, 3);

我的字段名为“ 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:

function my_acf_load_value( $value, $post_id, $field ) {
    global $post;
    if(($value === null || trim($value) === '')) {
        $value = $post->post_title;
    }
    return $value;
}
add_filter('acf/load_value/name=headline_text', 'my_acf_load_value', 10, 3);

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文