Support

Account

Home Forums Front-end Issues Trying to add conditional display to blog-list

Solved

Trying to add conditional display to blog-list

  • I have the list of posts (aka blog-list)
    They currently display featured image, excerpt, and a read-more link.

    I want to add the labels and values of the advanced custom fields to this display.

    However, when I use the below code, it does not display the label (text) or the value (link) for anything and treats every entry as “!empty” despite many of the fields being empty.

    
    <?php
    $buttons = get_field_objects();
    if( $buttons ) { ?>
      <div class="et_pb_button_module_wrapper et_pb_button_3_tb_body_wrapper">
        <?php foreach( $buttons as $button ) { ?>
          <?php if (!empty($button['value'])) { ?>
            <a class="vendor-link" title="<?php echo $button['label']; ?>" href="<?php echo $button['value']; ?>"><?php echo $field['label']; ?></a>
          <?php } ?>
        <?php } ?>
      </div>
    <?php }?>
    

    I would use get_field — except I want this to be a one time edit which pulls the data, even if I add or remove fields.
    (the goal is to display a series of links, styled as buttons for each post in the list)

    TYIA for any assistance you can provide

  • empty depends on what the value is.

    https://www.php.net/manual/en/language.types.boolean.php#language.types.boolean.casting.

    The values in the field object are the values before acf has formatted them (raw value), a true/false field set to false could be a string value "0" which is not empty.

    Try get_fields() instead of get_field_objects() and then use get_field_object() for non empty fields to get the field label.

  • ok, trying again. This doesn’t display empty buttons… but seems to not display anything at all.

    
    							$buttons = get_fields();
    							if( $buttons ) { 
    								echo
    									'<div class="et_pb_button_module_wrapper et_pb_button_3_tb_body_wrapper">';
    									foreach( $buttons as $but_name => $but_value ) {
    										if( !empty( $but_value ) ) { 
    											$but_label = get_field_object($but_name);
    											echo
    												'<a class="vendor-link" title="',$but_label['label'] ,'" href="', $but_value ,'">', $but_label['label'] ,'</a>';
    										}
    									}
    								echo
    									'</div>';
    							}
    

    Am I missing something obvious?

  • I don’t see anything obvious.

    What is it getting?

    
    $buttons = get_fields();
    var_dump($buttons);
    
  • Ok, found the problem. I had to add the code in TWO placed within the theme template file….

    This was my final working code – if anyone finds this in a future search

    							/* START position for Advanced Custom Fields Content -- aka vendor-links */
    							$buttons = get_fields();
    							if( $buttons ) { 
    								echo
    									'<div class="button-strip et_pb_button_module_wrapper et_pb_button_3_tb_body_wrapper">';
    									foreach( $buttons as $but_name => $but_value ) {
    										if( !empty( $but_value ) ) { 
    											$but_label = get_field_object($but_name);
    											echo
    												'<a class="vendor-link3 ', $but_name ,'-button" title="',$but_label['label'] ,'" href="', $but_value ,'" target="_blank">', $but_label['label'] ,'</a>';
    										}
    									}
    								echo
    									'</div>';
    							}
    							/* FINISH position for Advanced Custom Fields Content -- aka vendor-links */
    

    I then used some CSS on “vendor-link3” and the button name (like amazon-button) to style them as buttons.

    Thanks for the pointers John.

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

You must be logged in to reply to this topic.