skip to Main Content

In the ACF admin, if you create a field, you can set the parameter where to display this metabox Show this field group if and set, for example, page = about and in this way display metaboxes only on the About page

If you do it through code

'location' => array (
                array (
                    array (
                        'param' => 'post_type',
                        'operator' => '==',
                        'value' => 'page',
                    ),
                ),
            ),

it is possible this way, but then only on ALL pages

'location' => array (
                array (
                    array (
                        'param' => 'page_type',
                        'operator' => '==',
                        'value' => 'front_page',
                    ),
                ),
            ),

Or like this, the main one.

But I don’t quite understand how to specify which page to link to.

I want to link metaboxes to a specific page, "about-us"

<?php
acf_add_local_field_group(array (
    'key' => 'group_1',
    'title' => 'My Group',
    'fields' => array (
        array (
            'key' => 'field_1',
            'label' => 'Sub Title',
            'name' => 'sub_title',
            'type' => 'text',
            'prefix' => '',
            'instructions' => '',
            'required' => 0,
            'conditional_logic' => 0,
            'wrapper' => array (
                'width' => '',
                'class' => '',
                'id' => '',
            ),
            'default_value' => '',
            'placeholder' => '',
            'prepend' => '',
            'append' => '',
            'maxlength' => '',
            'readonly' => 0,
            'disabled' => 0,
        )
    ),
    'location' => array (
        array (
            array (
                'param' => 'page_type',
                'operator' => '==',
                'value' => 'front_page',
            ),
        ),
    ),
    'menu_order' => 0,
    'position' => 'normal',
    'style' => 'default',
    'label_placement' => 'top',
    'instruction_placement' => 'label',
    'hide_on_screen' => '',
));

2

Answers


  1. If you want to display the ACF Field for a specific page you could change your ‘location’ to the following:

    'location' => array(
            array(
                array(
                    'param' => 'page',
                    'operator' => '==',
                    'value' => '10',
                ),
            ),
        ),
    

    10 = page-ID for the desired site.

    FYI:
    I often use the ACF-Export to PHP function if I´m not sure which values are required when creating fields by code.

    I temporarily create the fields within the ACF admin and use the export tool to see the required code.

    enter image description here

    enter image description here

    enter image description here

    Hope it helps you a little further.

    Login or Signup to reply.
  2. I had a similar need, but didn’t want to use IDs, as that can change across environments, or in the future, so I created a plugin to create a page slug rule.

    Add that file in the wp-content/mu-plugins directory, and when editing your field group, there will be a new "Page Slug" dropdown item.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search