Support

Account

Home Forums General Issues Taxonomy Term Field in Categories

Solved

Taxonomy Term Field in Categories

  • Hi i succesfull added a field to my categorie, however I am unable to show it in my Theme. I used <?php the_field('short_description',$category); ?> and also just <?php the_field('short_description'); ?> However it just does not show!

  • Is $category formatted like: "{$term->taxonomy}_{$term->term_id}"? Getting field values from taxonomy fields is a little funky, full docs at: http://www.advancedcustomfields.com/resources/how-to-get-values-from-a-taxonomy-term/

  • sorry this did not help me i already saw this and tryed everything with no success, I am a newbie what does {$term->taxonomy}_{$term->term_id} mean???

  • Those items are fields from a taxonomy. Say you’re wanting to list out the default category taxonomy, you’d need to do something like:

    
     $terms = get_terms( 'category' );
     if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
         echo '<ul>';
         foreach ( $terms as $term ) {
           echo '<li>' . $term->name . '</li>';
            
         }
         echo '</ul>';
     }
    

    That will output the name of all the categories you’ve created. If you want to output your custom field as well you’ll add your get_field() like:

    
     $terms = get_terms( 'category' );
     if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
         echo '<ul>';
         foreach ( $terms as $term ) {
           echo '<li>' . $term->name . ' - ' . get_field('short_description', $term->taxonomy . '_' . $term->term_id) . '</li>';
            
         }
         echo '</ul>';
     }
    

    For the get_field() function you’re passing your field name short_description along with the $term->taxonomy and $term->term_id separated by an underscore.

  • okay thanks that helped a little, first time i could get the custom filed, however it shows a list of all categories i only nedd to see the custom field within the category page, how do i archive this?

  • Could you paste the code from your category page?

  • yep, its a custom theme and this is the part where i have to add the short_description custom field

    <?php global $post, $swp_bread_status_blog, $swp_postmeta_status_blog,$swp_colsidemasonrywidth,$swp_colnosidemasonrywidth,$swp_colsidemasonrywidth_tablet, $swp_colnosidemasonrywidth_tablet, $swp_colspan, $swp_colspanmeta, $swp_leadingposts, $swp_leadingcolspan, $swp_post_wordlimit_blog, $swp_responsive; ?>
    
    <?php dynamic_sidebar('sidebar-7'); ?>
    
    <div class="post nocolsidepost"><div class="posttop"></div><div class="postinner-h1">
    <h1><?php echo single_cat_title(); ?> günstig online kaufen!</h1>
    
    </div></div>
    
    <div id="masonthis">
    
    <?php $swp_count=1; ?>
    
    <div role="main" itemprop="mainContentOfPage">
    
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    <?php get_ts_colspan(); if ($swp_count <= $swp_leadingposts) {$swp_colspan = $swp_leadingcolspan;} else if ($swp_colspanmeta != "" ) {$swp_colspan = $swp_colspanmeta;} else {$swp_colspan = 1;} ?>
    
    <?php get_theme_div( 'post', 'top', $sidebar, $cols ); ?>
    
    <?php $wpaztemplate = get_post_meta($post->ID, 'wpaztemplate', true);
    
    if ($wpaztemplate=="No Template" || $wpaztemplate=="") {
    	
    if ($swp_post_wordlimit_blog!='0'){ $teaser = true; } else { $teaser = false; } ?>
    
    <article role="article" itemscope itemtype="http://schema.org/Article">
    
    <?php get_theme_piece ( 'posttitle' ); ?>
    
    <?php if ( $swp_postmeta_status_blog=="Show"){ get_theme_piece( 'postinfo' ); } ?>
    
    <?php get_theme_piece( 'featuredimage' ); ?>
    
    <div itemprop="articleBody"><?php if ($teaser){ echo teaser($swp_post_wordlimit_blog); } else { the_content(''); } ?></div>
    
    <div class="clear"></div>
    
    <?php get_theme_piece( 'postinfotwo', '', $swp_postmeta_status_blog, $teaser ); ?>
    
    </article>
    
    <?php } else {
    
    $wpaz_word_count = get_theme_mod( 'wpaz_word_count_side', '250'); if ($wpaz_word_count!='' && $wpaz_word_count!=0){ $teaser = true; } else { $teaser = false; } ?>
    
    <section role="article" itemscope itemtype="http://schema.org/Product">
    
    <?php get_theme_piece ( 'posttitle' ); ?>
    
    <?php get_custom_theme_piece( 'featuredimage', 'productblog', $sidebar, $swp_colspan ); ?>
    
    <div class="proddesc" itemprop="description"><?php if ($teaser){ echo teaser($wpaz_word_count); } else { the_content(''); } ?></div>
    
    <?php $wpaz_showstars = get_theme_mod( 'wpaz_showstars', 'No'); if ($wpaz_showstars=="Yes"){ ?><div class="reviewscorewrap"><?php get_custom_theme_piece( 'reviewscore' ); ?></div><?php } ?>
    
    <?php get_custom_theme_piece( 'product-box', 'small' ); ?>
    
    <div class="clear"></div>
    
    </section>
    
    <?php } ?>
    
    <?php get_theme_div( 'post', 'bottom' ); ?>
    
    <?php $swp_count++; endwhile; else: ?>
    
    <?php get_theme_piece( '404content' ); ?>
    
    <?php endif; ?>
    
    </div>
    
    </div>
    
    <?php if ($cols == 'col') { ?>
    <script type="text/javascript"><!--
    jQuery(window).load(function(){
    <?php if (!$swp_responsive) { ?>	
    	jQuery('#masonthis').masonry({
    	columnWidth: <?php echo ${"swp_col".$sidebar."masonrywidth"}; ?>,
    	itemSelector: '.post' 
    	});
    <?php } else { ?>
    fw = jQuery('.fullscreen').width();
    if (fw > 960){
    	jQuery('#masonthis').masonry({
    	columnWidth: <?php echo ${"swp_col".$sidebar."masonrywidth"}; ?>,
    	itemSelector: '.post' 
    	});
    } else if (fw < 960 && fw > 751){
    	jQuery('#masonthis').masonry({
    	columnWidth: <?php echo ${"swp_col".$sidebar."masonrywidth_tablet"}; ?>,
    	itemSelector: '.post' 
    	});
    }
    <?php } ?>
    });
    --></script>
    <?php } ?>
    
    <div class="clear"></div>
    
    <?php get_theme_piece( 'blogtitle', '', $sidebar, 'nocol' ); ?>
    
    <?php dynamic_sidebar('sidebar-8'); ?>
  • it need to be in these part

    <div class="postinner-h1">
    <h1><?php echo single_cat_title(); ?> günstig online kaufen!</h1>
    
    </div></div>
  • I am still lost, cant get the custom field to show :(, need help…

  • Does this work:

    
    <div class="postinner-h1">
    <h1><?php echo single_cat_title(); ?> günstig online kaufen!</h1>
    <?php
    $term = get_queried_object();
    if ( $term ):
    ?>
    <p><?php echo get_field( 'short_description', $term->taxonomy . '_' . $term->term_id ); ?>
    <?php endif; ?>
    </div></div>
    
  • yiepppii, you are a Genius, i was close but you nailed it!!!! THANK YOU

  • Nice, glad to be of service 🙂

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

The topic ‘Taxonomy Term Field in Categories’ is closed to new replies.