Support

Account

Home Forums General Issues Get terms for current post in single post…

Solved

Get terms for current post 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;
    		
    	}
    };?>
  • If it is useful, i give the full code of my single page (but the problematic part is in my first post!).

    <?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.'</p>';} ?>
    <?php if ($public=="") {echo "";} else {echo '<h2 class="titsection">'."Public concerné".'</h2><p class="singsection">'.$public.'</p>';} ?>
    <?php if ($objectif=="") {echo "";} else {echo '<h2 class="titsection">'."Objectif".'</h2><p class="singsection">'.$objectif.'</p>';} ?>
    <?php if ($moyenpeda=="") {echo "";} else {echo '<h2 class="titsection">'."Support pédagogique".'</h2><p class="singsection">'.$moyenpeda.'</p>';} ?>
    <?php if ($programme=="") {echo "";} else {echo '<h2 class="titsection">'."Programme".'</h2><p class="singsection programme">'.$programme.'</p>';} ?>
    <?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();
  • Please don’t cross-post, you’re just wasting the time of people and clutter the forum. I’ve posted an answer at https://support.advancedcustomfields.com/forums/topic/how-to-get-taxonomies-loop-in-single-post/

  • Ok sorry and thank you again.

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.