Support

Account

Home Forums Add-ons Flexible Content Field Display flexible content on category archive page

Solved

Display flexible content on category archive page

  • Hello,

    I created some flexible content fields to use on pages and category archive pages.

    I have no issues with displaying the fields on pages but I’m having issues displaying the fields on category archive pages.

    The code I’m using is below:

    <?php if( have_rows('page_modules', $here) ):
    	while ( have_rows('page_modules', $here) ) : the_row();	
    		// TOP SLIDER
    			if( get_row_layout() == 'image_slider'):
    				require 'modules/top-slider.php';
    			endif;
    						
    		// IMAGE/VIDEO/TEXT 
    			if( get_row_layout() == 'image_video_text' ): 
    				require 'modules/image-video-text.php';
    			endif;
    
    	endwhile;
    	endif; ?>

    I’ve tried using the following code below on archive.php to display the category flexible content values. The results that are generated are from my home page….

    <?php
    	$categories = get_the_category();
    	$category_id = $categories[0]->cat_ID;
    			
              if( have_rows('page_modules', $category_id) ):
    		while ( have_rows('page_modules', $category_id) ) : the_row();	
    			// TOP SLIDER
    			if( get_row_layout() == 'image_slider'):
    				require 'modules/top-slider.php';
    			endif;
    						
    			// IMAGE/VIDEO/TEXT 
    			if( get_row_layout() == 'image_video_text' ): 
    				require 'modules/image-video-text.php';
    			endif;
    
    		endwhile;
    	endif; ?>

    Any ideas on how I can display flexible content on category archive pages?

    Any help much appreciated

    Thanks

  • The post id for ACF is "{$taxonomy}_{$term_id}"

    
    $object = get_queried_object();
    $post_id = $object->taxonomy.'_'.$object->term_id;
    
  • John,

    Thanks for the reply. Works a treat!

    Thanks again.

    • revive

    • April 16, 2021 at 6:53 am

    I have a similar issue, though with multiple Flexible Content loops on the Category page.

    My goal is to just get the first ‘paragraph’ content type for each post (these are custom post types with specific content defined for them, via ACF.

    So, if I use:

    
    <p class="lead">	
    	<?php if (have_rows('flexible_content_types')): ?>
    
    		<?php while (have_rows('flexible_content_types')): the_row(); ?>
    
    			<?php if (get_row_layout() == 'paragraph_content'): ?>
    				<?php echo truncate( get_sub_field('content' ), 120); ?>
         		<?php break; ?>
    			<?php endif; ?>
    
    		<?php endwhile; ?>
    
    	<?php  else: ?>
    		NO CONTENT
    	<?php  endif; ?>
    </p>
    

    It works, for the FIRST ONE.. then I run another loop:

    
    <?php
    	$featured = get_posts( array(
    		'post_type' 		=> 'post',
    		'numberposts'		=> 2,
    		'tag' 			=> 'featured',
    		'cat'                 => 3,
    		'ignore_sticky_posts' => 1,					
    		'offset'			=> 0
    	));
    		if ( $featured ): ?>
    			<?php foreach( $featured as $post ):  setup_postdata( $post ); ?>
    ... etc.. and run flex content loop here...
    

    and use the same flexible content loop from above.. doesn’t work on the second, third, etc. loops.

    Any ideas? What am I doing wrong here?

    IF I use the recommended code from above:

    $object = get_queried_object();
    $post_id = $object->taxonomy.’_’.$object->term_id;

    Then one of the flexible content loops works, the others do not…

    any suggestions ?

    • revive

    • April 16, 2021 at 6:56 am

    Interestingly, the 1st post, of the SECOND loop for the flexible content DOES show the paragraph.. it’s just any consecutive posts in that get_posts loop, that don’t show the paragraph.

    • revive

    • April 16, 2021 at 7:06 am

    If I remove the <?php break; ?> from the flex content loop, they all work, BUT if there is more than one paragraph content type, they’ll all show

    • revive

    • April 16, 2021 at 7:10 am

    Ok, solved it.
    Changed:

    
    <?php if (get_row_layout() == 'paragraph_content'): ?>
    

    To:

    
    <?php if (get_row_layout() == 'paragraph_content' && get_row_index() == 1): ?>
    

    and removed the break… all works as it should. No need for putting post, or cat, IDs in the fields!

Viewing 7 posts - 1 through 7 (of 7 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.