Support

Account

Home Forums General Issues Add Button to Custom Post Type that adds Custom Field

Solved

Add Button to Custom Post Type that adds Custom Field

  • Hey everyone,

    sorry I wasn’t sure in which Forum to put this.
    So I have a Custom Post Type (Events/Termine). Each post under Events has Custom Fields, they are are the same, just textarea-fields.
    Is there any way that I could add a button to the Custom Post Type that allows the creation of a new field right at the post? The field would just have to be the same as the other ones.

    I tried adding a field group to my Custom Post Type that contains a button-group with a button for ‘create new event’ and then added this as a Code Snippet
    (please be patient if this is horrible, I am very much a newbie to programming :))

    if( get_field('termin_hinzufuegen') == 'Neuen Termin hinzufügen' ) {
    acf_add_local_field( array(
    'key' => 'field_' . uniqid(),
    'label' => 'Termin',
    'name' => 'termin_' . uniqid(),
    'type' => 'textarea',
    'parent' => $group_key,
    ) );
    }

    Neuen Termin hinzufügen = Add new Event (Termin is Event).

    I could obviously not get it to work and now I am stuck.
    Does anyone have any idea how this could possibly work?
    Ideally I would also add an option for deleting fields again in the same way – thought of adding a select item that pulls the field values of the current post for choosing which one to delete.

    Thanks so much!

  • The field will only exist when the code it run. For the field to remain the field definition must exist in a file that is loaded every time the page is loaded. What you are trying to do is not possible without making the field a permanent part of the field group.

    What you are describing is what repeater fields are for.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.