使用高级自定义字段添加帖子选择复选框

编程入门 行业动态 更新时间:2024-10-26 06:30:28
本文介绍了使用高级自定义字段添加帖子选择复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我有两种自定义帖子类型,称为酒店和房间.用户可以注册到我的网站,他们可以从他们的 wp 后端添加他们的酒店和房间.所以我的问题是当他们添加酒店时,他们需要选择酒店房间.

I have two custom post types called hotels and rooms. Users can register to my web site and they can add their hotels and rooms from their wp backend. so my problem is when they are adding hotels they need to select their hotel rooms.

我正在使用高级自定义字段,它具有称为 post 对象的字段类型.但该字段在选择菜单中显示帖子.我需要用复选框来完成它.所以它会很容易使用.

I'm using Advanced custom fields and it has field type called post object. but that field displaying posts in select menu. I need to do it with checkboxes.So it'll be so easy to user.

如果有人可以帮助我,我真的很感激.

I really appritiate if someone can help me.

谢谢

推荐答案

ACF 需要一些 bt 代码

It will require a little bt of code along with ACF

添加复选框字段(例如:品牌)并在您的functions.php文件中添加以下代码

Add checkbox field (ex : brands) and add following code in your functions.php file

function my_acf_load_field( $field )
{
    global $post;
    $field['choices'] = array();
    wp_reset_query();
    $query = new WP_Query(array('show_posts' => 100));
    foreach($query->posts as $product_id=>$macthed_product){
            $choices[$macthed_product->ID] = $macthed_product->post_title;
    }
    $field['choices'] = array();

    if( is_array($choices) )
    {
        foreach( $choices as $key=>$choice )
        {
            $field['choices'][$key] = $choice;
        }
    }
     wp_reset_query();
    return $field;
}
add_filter('acf/load_field/name=brands', 'my_acf_load_field');

使用 wp 查询参数来过滤帖子.

帖子 id 将是复选框值,标题为元框中的标签.

Post id will be the checkbox value with title as label in metabox.

这篇关于使用高级自定义字段添加帖子选择复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-23 08:02:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1039130.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   字段   复选框   高级   帖子

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!