Home › Forums › Add-ons › Flexible Content Field › 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
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
🚨 The 2023 ACF Annual Survey closes tomorrow! This is your last chance to complete the survey and help guide the evolution of ACF. https://t.co/0cgr9ZFOJ5
— Advanced Custom Fields (@wp_acf) May 18, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.