Support

Account

Home Forums ACF PRO How to get taxonomies loop in single post ?

Solved

How to get taxonomies loop in single post ?

  • Hello,
    I’m afraid my question is stupid, but I can’t figure out how to display (multiple) categories in a “single-post” template !
    My post type is formation, i’m in the single page template and i want to display all the current post name of the formers. I have a CPT ‘intervenants’ wich is a taxonomy for ‘formations’ (as well a CPT). I put this code and it display all the formers (‘intervenants’) but i want only the results for my current (single) post. How should i do ?

    <?php
    $terms1 = get_terms("intervenants");
    
    if( $terms1 )
    {
    	foreach( $terms1 as $term1 )
    
    	{
    		$value1 = get_field('prenom_et_nom','intervenants_'.$term1->term_id);
    		
    		 echo $value1;
    		
    	}
    
    };?>

    This is the full code of my single post php if useful :

    <?php
    /*
    ** single.php
    ** mk_build_main_wrapper : builds the main divisions that contains the content. Located in framework/helpers/global.php
    ** mk_get_view gets the parts of the pages, modules and components. Function located in framework/helpers/global.php
    */
    
    get_header(); ?>
    <div id="theme-page" class="master-holder  clearfix" itemscope="itemscope" itemtype="https://schema.org/Blog">
    <div class="mk-main-wrapper-holder">
    <div id="singforma" class="theme-page-wrapper mk-main-wrapper mk-grid full-layout false">
    <?php Mk_Static_Files::addAssets('mk_blog');
    
    $blog_style = 'blog-style-'.mk_get_blog_single_style();
    $blog_type = 	'blog-post-type-'.mk_get_blog_single_type();
    $holder_class = $blog_type . ' ' .$blog_style;
    $post_type =  get_post_type();
    $startdate = get_field( "date_debut" );
    $enddate = get_field( "date_de_fin" );
    $commune = get_field( "commune" );
    $cp = get_field ("code_postal");
    $termsf = get_the_terms( get_the_ID(), 'famille');
    if( !empty($termsf) ) { 
    $famille = array_pop($termsf);}
    $terms = get_the_terms( get_the_ID(), 'produit');
    if( !empty($terms) ) { 
    $term = array_pop($terms);}
    $priceinter = get_field('tarif_inter', $term ); 
    $prerequis = get_field('prerequis', $term );
    $public = get_field('public', $term );
    $objectif = get_field('objectif', $term );
    $moyenpeda = get_field('moyens_pedagogiques', $term );
    $programme = get_field('programme', $term );
    $duration = get_field('duree_de_la_formation', $term );
    $termsi = get_the_terms( get_the_ID(), 'intervenants');
    if( !empty($termsi) ) { 
    $termi = array_pop($termsi);}
    $post_objects = get_field('prenom_et_nom', $termi );
    $interpres = get_field('presentation', $termi ); ?>
    <div class="singformpage"><div class="singheadinf"><?php echo '<span class="sdatef"><i class="far fa-calendar-alt"></i><br/>'."Du ".$startdate . " au ".$enddate.'</span>'?>
    <?php if ($commune==""){echo "";} else {echo '<span class="splacef"><i class="fas fa-map-marker-alt"></i><br/>'.$commune." (".$cp.")".'</span>';}?>
    <?php if ($duration=="") {echo "";} else {echo '<span class="sdurationf"><i class="fas fa-stopwatch"></i><br/>'.$duration.'</span>';} ?></div>
    <?php if ($prerequis=="") {echo "";} else {echo '<h2 class="titsection">'."Prérequis".'</h2><p class="singsection">'.$prerequis.'
    ';} ?>
    <?php if ($public=="") {echo "";} else {echo '<h2 class="titsection">'."Public concerné".'</h2><p class="singsection">'.$public.'
    ';} ?>
    <?php if ($objectif=="") {echo "";} else {echo '<h2 class="titsection">'."Objectif".'</h2><p class="singsection">'.$objectif.'
    ';} ?>
    <?php if ($moyenpeda=="") {echo "";} else {echo '<h2 class="titsection">'."Support pédagogique".'</h2><p class="singsection">'.$moyenpeda.'
    ';} ?>
    <?php if ($programme=="") {echo "";} else {echo '<h2 class="titsection">'."Programme".'</h2><p class="singsection programme">'.$programme.'
    ';} ?>
    <?php
    $terms1 = get_terms("intervenants");
    
    if( $terms1 )
    {
    	foreach( $terms1 as $term1 )
    	
    	{
    		$value1 = get_field('prenom_et_nom','intervenants_'.$term1->term_id);
    		
    		 echo $value1;
    		
    	}
    };?>
    <?php
    $productname=get_field('nom_du_produit');
    $posts = get_posts(array(
    	'posts_per_page'	=> -1,
    	'post_type'			=> 'formations',
    	'meta_key'		=> 'nom_du_produit',
    	'meta_value'	=> $productname
    ));
    
    if( $posts ): ?>
    	
    	<ul>
    		
    	<?php foreach( $posts as $post ): 
    		
    		setup_postdata( $post );
    		
    		?>
    		<li>
    			<a href="<?php the_permalink(); ?>"><?php the_title(); echo get_field( "date_debut" );?></a>
    		</li>
    	
    	<?php endforeach; ?>
    	
    	</ul>
    	
    	<?php wp_reset_postdata(); ?>
    
    <?php endif;
    ?>
    </div>
    <div class="sidebforma"><?php echo do_shortcode( '[mk_custom_sidebar sidebar="sidebar-18"]' ); ?></div>
    </div></div></div>
    <?php get_footer();
  • If you want to retrieve the terms for the current post you probably want get_the_terms() (inside the post loop) rather than get_terms().

  • It works with get_the_terms($post_id, 'intervenants');
    Thank you very much !

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

The topic ‘How to get taxonomies loop in single post ?’ is closed to new replies.