Support

Account

Home Forums ACF PRO Custom Taxonomy and ACF

Helping

Custom Taxonomy and ACF

  • Good day to everyone

    Let me quickly say that ACF is one of the most important and great things that ever happened to WP.

    Now onward to my problem.

    I have a custom taxonomy populated with a few categories. I have created some custom fields (a multiple select and a textfield) for these categories but i cant figure out how to show them.

    For example i can populate the options of a select with the following query

    $taxonomy = array( 'katigories-ekdoseon' );
    $taxargs = array(
       'orderby'           => 'name',
       'order'             => 'ASC',
       'fields'            => 'all',
       'parent'            => 108,
       'hierarchical'      => true,
       'child_of'          => 0
    );
    $taxterms = get_terms($taxonomy, $taxargs);

    … and then …

    <?php foreach ($taxterms as $taxterm) { ?>
       <option value=".<?php echo $taxterm->slug; ?>"
               data-cf="">
               <?php echo $taxterm->name; ?>
       >
      </option>
    <?php } ?>

    When i try to fetch the custom fields for each of my categories, it simply does not show them.
    An example of what i tried is the following:

    <?php foreach ($taxterms as $taxterm) { ?>
        <option value=".<?php echo $taxterm->slug; ?>"
                data-cf="<?php $custom_field = get_field('test', $taxterm ); ?>">
            <?php echo $taxterm->name; ?>
        </option>
    <?php } ?>

    or

    <?php foreach ($taxterms as $taxterm) { ?>      
         <option value=".<?php echo $taxterm->slug; ?>"
                 <?php $cfmultipleselect = get_field('multiple_select'); ?>
                 <?php if( $cfmultipleselect ) : ?>
                 data-cf="<?php echo implode(', ', $cfmultipleselect); ?>"
                 <?php endif; ?>
           >
              <?php echo $taxterm->name; ?>
           </option>
    <?php } ?>

    Am i not getting something?

    Thank you for your time people

  • It should be working, sort of. The loop you are using looks right but some of the other stuff may be wrong.

    On the text field, you’re not echoing the value

    
    data-cf="<?php $custom_field = get_field('test', $taxterm ); ?>">
    

    should be

    
    data-cf="<?php echo get_field('test', $taxterm ); ?>">
    

    or

    
    data-cf="<?php the_field('test', $taxterm ); ?>">
    

    The select field on the other hand might be what you have the return value set to. If you are returning the label or alue then it will be correct, but if you’re returning an array then the issue is that you’re trying to implode a nested array, which will not work.

    Also, I don’t know what the “cf-data” attribute is. An attribute of an html element will not be shown on the page, unless you are looking at the html output, in which case you can ignore my ignorance πŸ˜›

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

You must be logged in to reply to this topic.