使用高级自定义字段添加帖子选择复选框 [英] Add posts select checkboxes using Advanced custom fields

查看:26
本文介绍了使用高级自定义字段添加帖子选择复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种自定义帖子类型,称为酒店和房间.用户可以注册到我的网站,他们可以从他们的 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆