自定义帖子类型的 submenu_page 中的 ACF 字段

问题描述 投票:0回答:1

我创建了一个名为“经销商”的自定义帖子类型。在这些经销商中,有一个经销商是独一无二的,需要自己的一组字段和页面。尽管它属于经销商类别,但我希望此页面在此自定义帖子类型的部分中是可编辑的。

这就是我在此自定义帖子类型中创建子菜单页面的原因。在这里,我想添加适当的 ACF 字段。

以下是我创建自定义帖子类型的方法:

<?php
/**
 * Custom posttype
 */
function vivafloors_custom_posttype_dealer()
{
    $dealersSlug = 'dealers';

    if (is_array(get_field('dealers', 'option'))) {
        $dealersSlug = get_field('dealers', 'option')['dealers_slug'];
    }

    $dealersName = ucfirst($dealersSlug);

    $labels = array(
        'name' => _x($dealersName, 'Post Type General Name', 'vivafloors'),
        'singular_name' => _x('Dealer', 'Post Type Singular Name', 'vivafloors'),
        'menu_name' => __('Dealers', 'vivafloors'),
        'name_admin_bar' => __('Dealer', 'vivafloors'),
        // 'parent_item_colon' => __('Hoofd case:', 'vivafloors'),
        'all_items' => __('Alle dealers', 'vivafloors'),
        'add_new_item' => __('Nieuwe dealer', 'vivafloors'),
        'add_new' => __('Nieuwe dealer', 'vivafloors'),
        'new_item' => __('Nieuwe dealer', 'vivafloors'),
        'edit_item' => __('Dealer bewerken', 'vivafloors'),
        'update_item' => __('Wijzig dealer', 'vivafloors'),
        'view_item' => __('Bekijk dealer', 'vivafloors'),
        'search_items' => __('Zoek dealer', 'vivafloors'),
        'not_found' => __('Not found', 'vivafloors'),
        'not_found_in_trash' => __('Not found in Trash', 'vivafloors'),
    );
    $rewrite = array(
        'slug' => $dealersSlug,
        'with_front' => true,
        'pages' => true,
        'feeds' => true,
    );
    $args = array(
        'label' => __('dealer', 'vivafloors'),
        'description' => __('Dealers', 'vivafloors'),
        'labels' => $labels,
        'supports' => array('title', 'revisions', 'thumbnail', 'revisions'),
        'hierarchical' => true,
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 20,
        'menu_icon' => 'dashicons-store',
        'show_in_admin_bar' => true,
        'show_in_nav_menus' => true,
        'can_export' => true,
        'has_archive' => false,
        'exclude_from_search' => false,
        'publicly_queryable' => true,
        'rewrite' => $rewrite
    );
    register_post_type('dealer', $args);
    register_taxonomy('dealer_category','dealer', array('hierarchical' => true, 'label' => 'Categories', 'query_var' => true, 'rewrite' => true)); 
}

// Hook into the 'init' action
add_action('init', 'vivafloors_custom_posttype_dealer', 0);

以下是我如何在此自定义帖子类型中创建子菜单页面:

function add_custom_page_to_menu() {
    add_submenu_page(
        'edit.php?post_type=dealer',
        __('Dealer Vivafloors', 'text_domain'),
        __('Dealer Vivafloors', 'text_domain'),
        'edit_posts', 
        'dealer-vivafloors',
        'call_back_function'
    );
}
add_action('admin_menu', 'add_custom_page_to_menu');

function call_back_function() {
    ?>
    <h1>Hello</h1>
    <?php
}

我无法在此子菜单页面上显示 ACF 字段。我应该通过 ACF 字段处理这个问题吗?因为我找不到将该子菜单页面与那里的 ACF 字段链接起来的方法。或者我需要触发

call_back_function
内的某些内容吗?

非常感谢您的帮助。

wordpress advanced-custom-fields custom-post-type
1个回答
0
投票

我找到了解决问题的简单方法:ACF 选项页面。新功能(从版本 6.2 开始)。这样,我就可以在我的自定义帖子类型“经销商”下创建一个子菜单页面。然后,您可以将字段添加到 ACF 内的此 ACF 选项页面。

© www.soinside.com 2019 - 2024. All rights reserved.