Support

Account

Home Forums Add-ons Flexible Content Field conditional in generated php code

Solving

conditional in generated php code

  • hi,

    I’m building a tool with ACF flexible content, because I want to use this on several websites I’m including this into my functions.php file so updating a number of sites will be easier. In the theme options page I’ve created options to turn on or off some custom post types which are not necessary for every client who is using this theme. Inside my functions.php file I’ve made some conditional statements that make a custom post type visible/hidden in the WordPress backend, this works pretty well and there are less options for the editor.

    Inside the flexible content group I’ve made some layouts where you can select these custom post types, butt even when the client does not use them. A global acf conditional statement would be awesome for this case but unfortunately this option doesn’t exsists so I’m trying to accomplish this inside the exported php code.

    Fot the custom post type i’m using:

    $cpt_selection = get_field('cpt_selection','option');
    if( $cpt_selection && in_array('services', $cpt_selection) ) {
    custom post type code...
    }

    Unfortunately this doesn’t seem to work inside several arrays (see printscreen)

    I’m not a die hard programmer, perhaps the sollution is pretty simple 🙂

  • Not sure what type of field you’re talking about or if you want to hide the entire layout.

    If this is a select type field then you can dynamically load the choices in the select field https://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/. This also works with radio and checkbox fields.

  • @jochem You shouldn’t apply logic while building an array.
    You might try:

    $layouts = array();
    
    if( in_array( 'service', $cpt_selection ) ) {
       $layouts[] = array(
          'id' => '',
          'key' => '',
          '
       );
    }
    
    acf_add_local_field_group( array(
        'key'                   => 'unique-key',
        'title'                 => __('Content', 'textdomain'),
    
        'fields'                => array(
            array(
                'key'          => 'unique-key',
                'label'        => __('Content', 'textdomain'),
                'name'         => 'field-name',
                'type'         => 'flexible_content',
    
                'button_label' => __( 'Add content', 'textdomain' ),
                'layouts'      => $layouts,
            )
        ),
        'location'              => array(),
    
        'menu_order'            => 10,
        'position'              => 'normal',
        'style'                 => 'seamless',
        'label_placement'       => 'top',
        'instruction_placement' => 'label',
        'hide_on_screen'        => ''
    ) );
    

    Hope this helps
    Kind regards

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

The topic ‘conditional in generated php code’ is closed to new replies.