Support

Account

Home Forums Add-ons Options Page Getting taxonomy from options page

Solved

Getting taxonomy from options page

  • Hi,
    I want to show a repeater field from an options page with an image and a toxonomy (category):

    <main id="main" class="site-main home" role="main">
    
    <?php if( have_rows('bilder', 'option') ): ?>
    <ul>
    <?php while( have_rows('bilder', 'option') ): the_row(); 
    	// vars
    	$image = get_sub_field('bild');
    	$category = get_sub_field('kategorie');
    	?>
    	<li>
    		<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" />
    	    <?php echo $category; ?>
    	</li>
    <?php endwhile; ?>
    </ul>
    
    <?php endif; ?>

    The image is shown but where the taxonomy name should be visible there is only “Array” – what am I doing wrong?

    This is the field for the taxonomy:

    http://fs5.directupload.net/images/160410/3lumu4hx.png

    The background is that I want to be able to upload pictures and sort them by it’s own category.

  • This gives me the term_link, but still the name is not shown:

    <?php if( have_rows('bilder', 'option') ): ?>
    <ul>
    <?php while( have_rows('bilder', 'option') ): the_row(); 
    	// vars
    	$image = get_sub_field('bild');
    	$category = get_sub_field('kategorie');
    	?>
    
    	<?php
    	$terms = get_sub_field('kategorie');
    	if( $terms ): ?>
    	<?php foreach( $terms as $term ): ?>
    		<?php echo get_term_link( $term ); ?>
    		<?php echo $term->name; ?>
    	<?php endforeach; ?>
    	<?php endif; ?>
    
    	<li>
    		<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" />
    	</li>
    <?php endwhile; ?>
    </ul>
    
    <?php endif; ?>
  • You are using a checkbox, this allows multiple values to be selected so the field will return an array. You’re also returning the Term ID, so you need to get the term. If you return term objects you can do

    
    $categories = get_sub_field('kategorie');
    foreach ($categories as $category) {
      echo $category->name.' ';
    }
    

    as it is you need to do

    
    $categories = get_sub_field('kategorie');
    foreach ($categories as $category) {
      $taxonomy = 'category';
      $term = get_term_by('id', $category, $taxonom);
      echo $term->name.' ';
    }
    
  • Hi John, this works, awesome! (when i add a “y” behind “taxonom”).

  • sorry about the typo 🙂

  • Could you tell me what is the difference between “object” and “ID” in custom fields?

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

The topic ‘Getting taxonomy from options page’ is closed to new replies.