简体   繁体   English

如何在我的自定义帖子类型中由 ACF 生成的下拉列表中显示类别描述字段?

[英]How do I display the Category description field within the dropdown generated by ACF in my custom post type?

I'm using a custom post type to display a taxonomy category within itself, when editing a post from this type, and I would like the dropdown (or perhaps some other way to select) to be able to also display the Description field from the category so that the simple 'region 1', 'region 2' has an explanation as to what geographical areas are contained in those regions.在编辑此类型的帖子时,我正在使用自定义帖子类型在其自身中显示分类类别,并且我希望下拉列表(或者可能是其他选择方式)也能够显示描述字段类别,以便简单的“区域 1”、“区域 2”可以解释这些区域中包含哪些地理区域。

Setting up the taxonomy for the CPT is simple enough,为 CPT 设置分类非常简单,

    $clubregions = array(
        'labels' => array(
            'name' => 'Club Regions',
            'singular_item' => 'Club Region',
            'add_new_item' => 'Add New Club Region',
            'edit_item' => 'Edit Club Region',
        ),
        'hierarchical' => true,
        'public' => true,
        'show_ui' => true,
        'show_admin_column' => true,
        'show_in_nav_menus' => false,
        'show_in_rest' => false,
        'show_tagcloud' => false,
    );
    register_taxonomy( 'clubregions', array( 'clubdetails' ), $clubregions );

but how do I get the page that ACF Pro generates to edit the custom post type with, to use the description field?但是如何获取 ACF Pro 生成的页面来编辑自定义帖子类型,以使用描述字段?

ACF 生成的区域下拉列表的屏幕截图


Now, you might think, "just save as draft, go back up to the CPT top level and click over to the category to read the descriptions", however we are additionally using Publishpress Capabilities Manager to have limited-access users for this custom post type, so they cannot see the category section from that level.现在,您可能会想,“只需保存为草稿,go 备份到 CPT 顶层并单击类别以阅读说明”,但是我们还使用 Publishpress Capabilities Manager 来限制访问此自定义帖子的用户类型,因此他们无法从该级别看到类别部分。

Cases like this one, where you need custom functionality beyond what ACF offers by default, are a great opportunity to create a new field type .在这种情况下,您需要超出 ACF 默认提供的自定义功能,这是创建新字段类型的绝佳机会。

ACF offers an official Field Type Template,available at github , to speed-up the development of new field types. ACF 提供官方字段类型模板,可在 github 获得,以加快新字段类型的开发。 This includes all the files you need and has great inline commenting that walks you through the process.这包括您需要的所有文件,并具有出色的内联注释,可引导您完成整个过程。 It also contains all the possible functions you can use.它还包含您可以使用的所有可能的功能。

There are also many great tutorials, like this one , on how to use this plugin to create your own custom fields.还有很多很棒的教程,比如这个,关于如何使用这个插件来创建你自己的自定义字段。

As a summary, you would need to create a custom method, eg.作为总结,您需要创建一个自定义方法,例如。 get_regions , to return all your available Regions and register that as field options. get_regions ,返回所有可用区域并将其注册为字段选项。 You would do that in the create_options() method like this:您可以在create_options()方法中执行此操作,如下所示:

do_action('acf/create_field', array(
    'type'      =>   'select',
    'name'      =>   'fields['.$key.'][initial_value]',
    'value'     =>   $field['initial_value'],
    'choices'   =>   $this->get_regions()
));

Then it will all come down to creating the control the user will actually see.然后一切都将归结为创建用户将实际看到的控件。 This is done in the create_field() method.这是在create_field()方法中完成的。 You'll need to create the HTML for your <select> field and that's where you can add more data for each option:您需要为<select>字段创建 HTML ,您可以在其中为每个选项添加更多数据:

function create_field( $field ) {
    $field = array_merge($this->defaults, $field);
    ?>
    <div>
        <select name='<?php echo $field['name'] ?>'>
            <?php
                foreach( $this->get_regions() as $region ) :
                    // get your $description here
            ?>
                <option <?php selected( $field['value'], $region ) ?> value='<?php echo $country ?>'>
                    <?php echo $region . ' - ' . $description; ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <?php
}

You can of course opt for a different element, like checkboxes or radio buttons, instead of a select .您当然可以选择不同的元素,例如复选框或单选按钮,而不是select

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何显示自定义帖子类型的类别页面(使用 ACF 制作)? - How to display category page for custom post type (made with ACF)? ACF-显示来自“自定义帖子类型”自定义类别的图像 - ACF - Display image from Custom Post Type custom Category 显示自定义帖子类型类别中的ACF字段值 - show ACF field value from custom post type category 如何在author.php上显示类别中的ACF自定义字段 - How to display ACF custom field from category on author.php 如何在 acf 表单前端添加 acf 表单帖子类别字段但类别下拉列表未显示 - How to add acf form post categry field in acf form front end but category dropdown not showing 在Wordpress中我的自定义帖子类型中按产品中的ACF字段排序 - Sort by ACF field in the products in my Custom Post Type in Wordpress WordPress-如何在使用category.php的页面中显示自定义帖子类型 - WordPress - How do I display custom post type in page that uses category.php 如何通过 ACF 关系字段查询自定义帖子类型 (CPT)? - How do you query a custom post type (CPT) via an ACF relationship field? 如何在 wordpress 中显示自定义帖子类型类别? - How to display custom post type category in wordpress? Wordpress:如何使用 ACF 和自定义帖子类型制作唯一字​​段 - Wordpress: How to make unique field with ACF and custom post type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM