Support

Account

Home Forums General Issues acf if query problem

Solved

acf if query problem

  • hello everyone, I’m new here =) 👋

    For example, I would like to create a color picker.

    A material is available, for example, in the colors: black and white.

    I have now created a checkbox with the corresponding values.

    If these are now selected, then I would like to spend instead of the values a color code in the box.

    So: I have a material that comes in different colors.
    Material 1 = blue, yellow, black
    Material 2 = black, white
    I have now created an acf checkbox with the colors (black, white, blue, yellow, etc)
    In the article material 1 I see now all colors as checkbox. that’s right. Here I select the 3 colors that are available.
    in the frontend these colors are also displayed as text. I would like to replace these with small boxes.
    So with black is then “<div style=”float: left; width: 10px; height: 10px; margin-right: 3px; border: 1px solid #000000; background-color: #1E1E1E” title=”Schwarz”></div>”
    the only problem now is that with this code all colors are displayed and not only those that are available.

    
    
    <?php $field = get_field_object('3DMaterialien');
    			if( $field['farben'] != 'Schwarz'): ?>
    					<div style="float: left; width: 10px; height: 10px; margin-right: 3px; border: 1px solid #000000; background-color: #1E1E1E" title="Schwarz"></div>
    			<?php endif; ?>
              	<?php $field = get_field_object('3DMaterialien');
    			if( $field['farben'] != 'Blau'): ?>
                      <div style="float: left; width: 10px; height: 10px; margin-right: 3px; border: 1px solid #000000; background-color: #3072B5" title="Blau"></div>
    			<?php endif; ?>
    

    however, with this code, everything is displayed always and everywhere. I think that would have to be changed.

    Can anyone help me ?

  • There isn’t any way to tell what you’re trying to do, for example, I have no idea what you’re attempting to do here or why

    
    if( $field['farben'] != 'Schwarz'): ?>
    

    $field as returned by ACF has no index named “farben”

    Nor do I understand why you are using get_field_object() instead of get_field()

    The only thing I can really do is tell you how I’d set this up.

    First I would create a checkbox field. The choices for this field would look something like this

    
    #FF0000 : Red
    #00FF00 : Grean
    #0000FF : Blue
    

    I would set the return value of this field to “Both (Array)”

    Then, in my template I would do this, I’m only adding what would be needed for using the values returned.

    
    $colors = get_field('name_of_color_field_here');
    if ($colors) {
      foreach ($colors as $color) {
        ?><div style="background-color: <?php
              echo $color['value']; ?> title=<?php 
              echo $color['label']; ?>></div><?php 
      }
    }
    
  • Impressive! I think that will get me much further.
    but somewhere you have to have made a mistake.
    maybe you forgot a quotation mark? (“) or too much?

    what I have already managed is that the “color codes” are displayed. but only with “#”.
    So not: #ffffff, just #
    (but at least only as many as colors are available for the material)

    my current code looks like this:

    
    				<?php
    $colors = get_field('farben');
    if( $colors ): ?>
    
        <?php foreach( $colors as $color ): ?>
            <div style="background-color:<?php echo $color['value']; ?>"><?php echo $color['label']; ?></div>
        <?php endforeach; ?>
    
    <?php endif; ?>	
    

    I would like to have the colors shown in small boxes.

  • edit: I’ve got it, that the individual boxes of the colors are displayed. However, these are now empty …. do we have to work with an array here?

    
            <?php
    $colors = get_field('farben');
    if( $colors ): ?>
        <?php foreach( $colors as $color ): ?>
            <div style="float: left; width: 10px; height: 10px; margin-right: 3px; border: 1px solid #000000; background-colo:-<?php echo $color['value']; ?>"></div>
        <?php endforeach; ?>
    <?php endif; ?> 
    

    https://ibb.co/txy4jh2

  • I missed some “s in my code, typed it here and there’s not syntax checking. It should have been

    
    $colors = get_field('name_of_color_field_here');
    if ($colors) {
      foreach ($colors as $color) {
        ?><div style="background-color: <?php
              echo $color['value']; ?>" title="<?php 
              echo $color['label']; ?>"></div><?php 
      }
    }
    
  • nearly…. 😀

    I once looked at the console and found that as a color code only one “#” is output …. without, for example, ffffff

  • `<?php
    $field = get_field_object(‘farben’);
    $colors = $field[‘value’];
    if( $colors ): ?>
    <?php foreach( $colors as $color ): ?>
    <div style=”float: left; width: 10px; height: 10px; margin-right: 3px; border: 1px solid #000000; background-color:<?php echo $color; ?>”></div>
    <?php endforeach; ?>
    <?php endif; ?>`

    This works!
    Thank You!

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

The topic ‘acf if query problem’ is closed to new replies.