Search code examples
wordpressadvanced-custom-fieldscustom-post-type

ACF fields in submenu_page of custom post type


I've created a custom post type called "Dealers". Within these dealers, there's one dealer that is unique and requires its own set of fields and page. Although it falls under the category of dealers, I want this page to be editable within the section of this custom post type.

That's why I've created a submenu page within this custom post type. Here, I want to add the appropriate ACF fields.

Here's how I create my custom post type:

<?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);

And here's how I create a submenu page within this custom post type:

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
}

I'm having trouble displaying ACF fields on this submenu page. Should I handle this through ACF fields? Because I couldn't find a way to link this submenu page with ACF fields there. Or do I need to trigger something within the call_back_function?

Assistance would be greatly appreciated.


Solution

  • I have found a simple solution to my problem: ACF Option Page. A new feature (starting from version 6.2). With this, I have been able to create a submenu page under my custom post type "Dealers". Then, you can add fields to this ACF Option Page within ACF.