Use same custom field with two page templates with slight variation in wp admin

Support › General › Use same custom field with two page templates with slight variation in wp adminResolved

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #33265
    Cecil AshiteyCecil Ashitey
    Participant

    Hi,

    I am trying to create two variations of a page using templates. it works fine in terms of layout but the dropdown field is not showing condition-based fields. Like when I select an option from the select field it doesn't show the related fields with the 'visible' condition applied.

    But if I change the id of any field in a template then it works.
    I want to use the same field id as I only want to switch templates but have the same fields positioned a bit differently in the admin area.

    Thanks

    #33275
    Long NguyenLong Nguyen
    Moderator

    Hi,

    Do you mean to use two page templates with two field values but keep the same field ID?

    #33282
    Cecil AshiteyCecil Ashitey
    Participant

    Hi,

    Thanks for the reply.
    Yes I have two page templates but i want to use the same field value and same field id. Following is the varition i want to use.
    varition one for template 1:-

    $meta_boxes[] = [
            'title'      => 'Section One',
            'id'         => 'section1',
            'post_types' => ['page'],
            'context'    => 'after_title',
            'visible' => array('page_template','=','template-1.php'),
            'fields'     => [
                [
                   'type'    => 'select',
                    'id'      => 'field_one',
                    'options' => range_options(),
                    'multiple'  => false,
                    'placeholder'  => 'Select an Item',
                    'columns' => 6,
                ],
                [
                        'visible' => array( 'field_one' , '=', 'field_123' ),
                        'name' => 'Title',
                        'id'   => 'one_title',
                        'type' => 'text',
                    ],
                [
                   'type'    => 'select',
                    'id'      => 'field_two',
                    'options' => range_options(),
                    'multiple'  => false,
                    'placeholder'  => 'Select an Item',
                    'columns' => 6,
                ],
                [
                        'visible' => array( 'field_two' , '=', 'field_456' ),
                        'name' => 'Title',
                        'id'   => 'two_title',
                        'type' => 'text',
                    ],
          ]
        ];
    

    Variation 2 for template 2 :

    $meta_boxes[] = [
            'title'      => 'Section One',
            'id'         => 'section1',
            'post_types' => ['page'],
            'context'    => 'after_title',
            'visible' => array('page_template','=','template-2.php'),
            'fields'     => [
                [
                   'type'    => 'select',
                    'id'      => 'field_one',
                    'options' => range_options(),
                    'multiple'  => false,
                    'placeholder'  => 'Select an Item',
                ], 
              [
                        'visible' => array( 'field_one' , '=', 'field_123' ),
                        'name' => 'Title',
                        'id'   => 'one_title',
                        'type' => 'text',
                    ],
            ]
            ];
    $meta_boxes[] = [
            'title'      => 'Section Two',
            'id'         => 'section2',
            'post_types' => ['page'],
            'context'    => 'after_title',
            'visible' => array('page_template','=','template-2.php'),
            'fields'     => [
                [
                   'type'    => 'select',
                    'id'      => 'field_two',
                    'options' => range_options(),
                    'multiple'  => false,
                    'placeholder'  => 'Select an Item',
                ], 
                [
                        'visible' => array( 'field_two' , '=', 'field_456' ),
                        'name' => 'Title',
                        'id'   => 'two_title',
                        'type' => 'text',
                    ],
            ]
            ];

    I am able to set field value in one template and when I switch templates it has that value saved which is what I am looking for.

    The issue is with the condition-based visibility of the fields. I mean when I select an option from the dropdown then on the 1st template it displays the subfields associated with it but it fails to do the same when I switch the template.

    #33288
    Long NguyenLong Nguyen
    Moderator

    Hi,

    It is not possible to have two meta boxes and fields with the same ID on a page so the conditional logic extension will not work. You can use the extension MB Include Exclude to load the meta box based on the template saved. For example:

    $meta_boxes[] = [
        'title'      => 'Section One',
        'id'         => 'section1',
        'post_types' => ['page'],
        'include' => array( 
            'template' => 'page-template-1.php',
        ),
        'fields'     => [
            ...
        ]
    ];
    
    $meta_boxes[] = [
        'title'      => 'Section One',
        'id'         => 'section1',
        'post_types' => ['page'],
        'include' => array( 
            'template' => 'page-template-2.php',
        ),
        'fields'     => [
            ...
        ]
    ];
    
    $meta_boxes[] = [
        'title'      => 'Section Two',
        'id'         => 'section2',
        'post_types' => ['page'],
        'include' => array( 
            'template' => 'page-template-2.php',
        ),
        'fields'     => [
            ...
        ]
    ];

    Refer to the documentation https://docs.metabox.io/extensions/meta-box-include-exclude/

    #33303
    Cecil AshiteyCecil Ashitey
    Participant

    Thanks, Long Nguyen
    For the information, I would proceed with MB Include Exclude.

    #33314
    Cecil AshiteyCecil Ashitey
    Participant

    Hi Long Nguyen,

    I was able to use MB Include Exclude but at the same time, I am thinking if I can switch places of the boxes when I change the template.
    For example
    Template1 has section1 which has box 1 and box2, Section2 has box3 and box4
    Template2 has section1 which has box1, section2 has box2 and box3

    Thanks

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.