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
- This topic has 5 replies, 2 voices, and was last updated 3 years, 3 months ago by
Cecil Ashitey.
-
AuthorPosts
-
January 16, 2022 at 7:57 PM #33265
Cecil Ashitey
ParticipantHi,
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
January 17, 2022 at 9:56 AM #33275Long Nguyen
ModeratorHi,
Do you mean to use two page templates with two field values but keep the same field ID?
January 17, 2022 at 2:34 PM #33282Cecil Ashitey
ParticipantHi,
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.
January 17, 2022 at 10:33 PM #33288Long Nguyen
ModeratorHi,
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/
January 18, 2022 at 1:58 PM #33303Cecil Ashitey
ParticipantThanks, Long Nguyen
For the information, I would proceed with MB Include Exclude.January 18, 2022 at 9:50 PM #33314Cecil Ashitey
ParticipantHi 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 box3Thanks
-
AuthorPosts
- You must be logged in to reply to this topic.