Support

Account

Forum Replies Created

  • Ahhhh. I didn’t realize this was for the admin. While there might be an ACF filter I am not aware of one that will do what you need.

    In lieu of that, you would have to load in your own javascript to the admin using the admin_enqueue_scripts hook and assign your classes via jQuery that way.

    There may be another way (so someone could definitely chime in here) however I can’t think of anything via a hook.

  • In this case, I think it makes sense to use two separate forms: one with the 3 fields you need on one page, and another form with all the repeater fields on the other page. You could still have those save to the same place — in your case to a user.

    Alternatively, if you need to use the same form for both pages you might have to disable the ‘required’ fields in ACF and then use some front-end validation like jQuery validate: https://jqueryvalidation.org

    While a bit hacky, you could then add ‘required’ asterisks to your fields in your theme after the field label: <span class="required">*</span>

    If it isn’t possible to edit the form label html directly you could use jQuery to append them or with css using the :after pseudo selector.

    I don’t see any disadvantage to creating two forms in this case.

  • Can you explain more what you mean by this statement?

    “7) I then added a ‘course_categories’ selection to the ‘courses’ I had created.”

    What do you mean by ‘selection’? Radio buttons? A Select drop-down?

    Just off the top of my head, if you are adding a taxonomy to your custom post type, you don’t need to add an additional ACF selection field, especially with the same slug/name. This will create unexpected results.

    Once you’ve created the custom taxonomy, you can use the built-in WordPress support for taxonomies.

    In any case, you have two options so pick one but you shouldn’t really use both:

    1) If you want to use your custom taxonomy, make sure your cpt supports the custom taxonomy and vice-versa that your custom taxonomy is attached to your custom post type.

    Then, make sure your edit screen for your custom post type has your taxonomy box checked in ‘Screen Options’. It should look just like the category box for regular Posts.

    Select your taxonomies for your custom post type posts using that box (no ACF select field necessary).

    — OR —

    2) Use an ACF select field (either Select or Checkbox) and then you can query posts by that field (scroll down to Examples): https://www.advancedcustomfields.com/resources/query-posts-custom-fields/

    Thus, you should either use your custom taxonomy or an ACF field but not both – you are just re-selecting something that is already there.

    All that said, there could be uses for grabbing your taxonomies in ACF but make sure that your ACF fields do not have the same slugs as your custom taxonomy.

  • The button title text should default to the Button Label text but until there’s a fix, you could use some jQuery to alter it:

    
    jQuery(document).ready(function($){
        // for a single button
        $('.your-button').attr('title','some title');
    
        // if you have multiple on the page
        $('.your-button-class').each(function() {
           $(this).attr('title', $(this).text());
        });
    });
    
  • Did you try using get_field_objects()?

    https://www.advancedcustomfields.com/resources/get_field_objects/

    You could use a conditional to grab only Flexible Content fields:

    
    <?php 
    if ($field['type'] == 'flexible_content') { // not sure if this is correct 
    
    /*
    *  get all custom fields and dump for testing
    */
    
    $fields = get_field_objects();
    var_dump( $fields ); 
    
    /*
    *  get all custom fields, loop through them and create a label => value markup
    */
    
    $fields = get_field_objects();
    
    if( $fields )
    {
    	foreach( $fields as $field_name => $field )
    	{
    		echo '<div>';
    			echo '<h3>' . $field['label'] . '</h3>';
    			echo $field['value'];
    		echo '</div>';
    	}
    }
    
    } 
    ?>
    

    More here: https://support.advancedcustomfields.com/forums/topic/if-statement-based-on-field-type/

    Does this help?

  • Looking at the ACF docs, you need to add the location array to your field registration and attach it to your custom post type:

    
    'location' => array (
        array (
            array (
                'param' => 'post_type',
                'operator' => '==',
                'value' => 'post', // add your post_type here
    	),
        ),
    ),
    

    I haven’t tested this but I think that should work.

    More here from the docs: https://www.advancedcustomfields.com/resources/register-fields-via-php/

  • I think the problem is your variable: $post_ID. Maybe try calling the global $post object and then using $post->ID like so:

    
    <?php // get WP global post object
    global $post;
    
    // you could add a query here to limit your results
    get_fields($post->ID);
    
    // Also if you are grabbing multiple fields you might need to loop through them
    $fields = get_fields($post->ID);
    
    foreach ($fields as $field) {
       // do something with each post
    }
    

    Not sure if that is exactly what you’re looking for.

  • You could make use of remove_filter() and have your field call like this:

    the_field('wysiwyg_field', false, false);

    If that is stripping away what you need, try this:

    the_field('wysiwyg_field', true, true);

    If that still doesn’t generate what you want, in the WYSIWYG field settings, change the editor mode to ‘Text’ which will retain all of your HTML tags. You won’t get a WYSIWYG preview on edit screens but your formatting will remain intact.

    See this thread for more info: https://support.advancedcustomfields.com/forums/topic/disable-auto-p-tags-in-wysiwyg-editor/

  • You could do something like this and just add the classes to the call in your theme:

    
    <?php
    
    // vars
    $field = get_field_object('your_select_field');
    $value = $field['value'];
    $label = $field['choices'][ $value ];
    
    ?>
    <p>Items: <span class="select2-results__option item-<?php echo $value; ?> <?php echo $value; ?>"><?php echo $label; ?></span></p>
    

    You can add whatever default classes you need and then the particular select option item classes programmatically via the field object.

    Does that help?

  • For radio button fields, you’ll need to add ['value'] or ['label'] after the variable to show either the value or label:

    
    <a href="<?php echo get_permalink( $shopid['value'] ); ?>" class="button">See</a>
    

    For testing, you’ll want to grab the object which will include the values and labels:

    
    <?php
    
    $shopid_obj = get_sub_field_object( 'cat_link' );
    var_dump($shopid_obj);
    
    ?>
    

    The above will spit out the array of your radio button field so you can see what’s there.

    See more here: https://www.advancedcustomfields.com/resources/radio-button/

  • If you want to insert shortcode and have it show on the front end, use a Text field and then use this in your template:

    
    <?php $shortcode = get_field( 'your_text_field' ); ?>
    <?php echo do_shortcode( $shortcode ); ?>
    

    That’s how I do it. The WYSIWYG field will strip out shortcodes.

  • You won’t be able to output all of them unless they are in your loop. I’m not sure what your initial $discipline; call is as well as the one within your loop. You’re not doing anything with it there.

    You’ll need something like this:

    
    <?php
    $output_map[$the_ID]['map'] = '<div class="marker" data-lat="'.$location['lat'].'" data-lng="'.$location['lng'].'"></div>';
    
    $disciplines = get_field("form_disciplines");
    // $discipline; not sure what this is?
    
    if( $disciplines ):
    foreach ($disciplines as $discipline) {         
        echo '<p>' . $discipline . '</p>';
    }
    endif;
    ?>
    

    Your map marker output doesn’t include any of the code in your loop so you can put it before or after your foreach loop depending on where you need it.

  • @mkeys After playing around with it a bit more, I’m not sure if it is Groups or it could be duplicative terms I had with custom taxonomies and ACF fields.

    ACF deals with this well by attaching the key to any field action to avoid namespace collision so you can use the same field name over again (e.g. within repeater fields or groups).

    My guess is WordPress is not liking the way I had things set up.

    For example, if I have a custom post type of Locations with taxonomy slugs of ‘airport’, ‘train-station’, ‘hotel’ then I had a Group named ‘hotel’. With ACF normally this wouldn’t be a problem but I think it is confusing WP.

  • Just tested this and it doesn’t seem to work within a Group field but when I have flat fields it works.

    Any idea why?

  • @mkeys Thanks so much for this – works perfectly.

  • @hube2 this works!! I had a few things in the wrong places. Here is the final code:

    
    $fields = acf_get_fields('204');
    
    $values = get_fields(); 
    
    if( $fields ) { 
    	$show_next = false;
    	foreach ($fields as $field) {
    		$value = $values[$field['name']];  
    	  if ($field['type'] == 'true_false' && $values[$field['name']]) {
    	    // this is a true false field set to true
    	    $show_next = true;
    	  } elseif ($field['type'] == 'true_false' && !$values[$field['name']]) {
    	    // this is a true false field set to false
    	    $show_next = false;
    	  } elseif ($show_next) {
    	    // the field is not a true false field
    	    // but the last true false field was true
    	    // output field
    	    echo '<tr>';
    		echo '<td class="detail-key">' . $field['label'] . '</td>';
    		echo '<td class="detail-value">' . $value . '</td>';
    		echo '</tr>';
    	  }
    	}
    } 
    

    Thank you so much.

  • Thanks @hube2. This logic works and is basically what I had but there is still no way to get the next element’s value from the array.

    There needs to be a way to grab all the field objects in a group with their values. Currently there isn’t as far as I can tell.

  • Just to clarify, this works:

    
    $show_hide_pricing_structure = get_field_object('show_hide_pricing_structure');
    $pricing_structure = get_field_object('pricing_structure');
    $show_hide_base_rent = get_field_object('show_hide_base_rent');
    $base_rent = get_field_object('base_rent');
    $show_hide_pricing_metric = get_field_object('show_hide_pricing_metric');
    $pricing_metric = get_field_object('pricing_metric');
    $show_hide_lease_type = get_field_object('show_hide_lease_type');
    $lease_type = get_field_object('lease_type');
    
    if($show_hide_pricing_structure['value'] == '1') {
    	echo '<tr>';
    	echo '<td class="detail-key">' . $pricing_structure['label'] . '</td>';
    	echo '<td class="detail-value">' . $pricing_structure['value'] . '</td>';
    	echo '</tr>';
    }
    
    if($show_hide_base_rent['value'] == '1') {
    	echo '<tr>';
    	echo '<td class="detail-key">' . $base_rent['label'] . '</td>';
    	echo '<td class="detail-value">$' . $base_rent['value'] . '</td>';
    	echo '</tr>';
    }
    
    if($show_hide_pricing_metric['value'] == '1') {
    	echo '<tr>';
    	echo '<td class="detail-key">' . $pricing_metric['label'] . '</td>';
    	echo '<td class="detail-value">' . $pricing_metric['value'] . '</td>';
    	echo '</tr>';
    }
    

    However this is not really easy to maintain and if the fields change or some are added, it doesn’t do the job.

    Looping through the fields seems like the right way to do this however I need to be able to access the next value in the array for it to work.

  • Hi John,

    This helps in terms of understanding why acf_get_fields(); returns null values however this doesn’t solve the more pressing issue of getting the value of the next item in the $fields array.

    Using var_dump($values); gives you all the fields from that page which is not really what I want here. I really want only the fields in the selected group. And this $values array doesn’t include the group that the fields are associated with.

    Thus, when in the foreach ($fields as $field) loop, it *does* retrieve the values however the only way to retrieve the NEXT item (which is really what I need) requires having the values in the initial array.

    Since there’s no there there, it won’t work.

    Maybe I could take a look at the acf_get_fields(); function and craft another one that retrieves the values as well.

    My ultimate goal is to add a show/no show toggle for each field. The client wants to be able to save the values with each post so it is visible in the admin but toggle the visibility on the front end.

    There are lots of fields and field groups for each post (these are from a “Properties” CPT) and there are 83 fields associated with each property in 7 groups. Not all of the fields will have toggles so that is why I was grabbing them by group.

    If there’s another way to go about this, I’m all ears. I thought about adding a special character as a flag (something like adding “%” at the beginning of the data) but this will only work for text or text area fields. We have radio buttons, checkboxes, drop downs as well.

    Any ideas?

    Thanks!

  • This is what worked for me. Hides fields with no value.

    
        <?php $fields = acf_get_fields('123'); ?>
    
        <?php if( $fields )
        { 
    	foreach( $fields as $field )
    	{
    		$value = get_field( $field['name'] );
    
    		if ($value) {
    		
    			echo '<dl>';
                    echo '<dt>' . $field['label'] . '</dt>';
                    echo '<dd>' . $field['value'] . '</dd>';
                echo '</dl>';
    		}
    	} 
    
        } 
        ?>
    
  • You can do this within Gravity Forms — you don’t need ACF for this.

    Under Form Settings > Notifications > [add new] > Conditional Logic. You can add a notification and use Conditional Logic to “activate” that notification when a particular checkbox is checked; in your case it could be the “workshop” checkbox or dropdown.

    Conditional Logic form fields require either radio buttons, checkboxes, or select (dropdown) in order to work.

  • Using the Basic Loop without setup_postdata(); as explained in the Relationship Field documentation ended up working.

    Here is the code:

    
    <?php $posts = get_field('event_venue'); // Relationship field
     if( $posts ): ?>
     <?php foreach( $posts as $p):  ?>
     
    <td>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    </td>
    <td>
    <?php the_field('venue_address', $p->ID); ?> <?php get_field('venue_city', $p->ID); ?><?php if( get_field('venue_state', $p->ID )): ?>, <?php the_field('venue_state', $p->ID); ?><?php endif; ?> <?php the_field('venue_postcode', $p->ID); ?> <?php the_field('venue_country', $post->ID); ?>
    </td>
    <?php endforeach; ?>
    <?php endif; // end our custom Loop within the loop ?>
    

    I guess the question now is why the Basic Loop with setup_postdata(); doesn’t work? In any event my issue is solved!

  • Ok I figured it out:

    
    <?php
    
    $posts = get_field('venue');
     
    if( $posts ): ?>
        
        <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
            <?php setup_postdata($post); ?>
            <?php $vmap = get_field('venue_google_map', $post->ID); ?>
            <div class="acf-map">
    	  <div class="marker" data-lat="<?php echo $vmap['lat']; ?>" data-lng="<?php echo $vmap['lng']; ?>"></div>
    	</div>
        <?php endforeach; ?>
        <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    <?php endif; ?>
    
Viewing 24 posts - 1 through 24 (of 24 total)