Support

Account

Home Forums Front-end Issues Text custom fields don't work after 'Recent Posts' code

Solved

Text custom fields don't work after 'Recent Posts' code

  • In my code I have some Text custom fields, then an area to display the 3 most recent blog posts, and then some more Text custom fields. The custom fields that come before the recent posts function properly, but the ones after it do not display anything. I know that the recent posts code is somehow the problem, because when I remove it all of the Text custom fields work properly. Below is the code for how I’m displaying the 3 most recent blog posts within Bootstrap grid containers – why is it messing up the custom fields that come after it and how do I fix it?

    <div class="row">
                
                	<div class="col-md-4 col-sm-4 col-xs-12">          	
                		
    	
    			<?php $posts1 = get_posts('numberposts=1'.$cat_id.'&offset=0'); ?>
    			<?php foreach( $posts1 as $post ) : setup_postdata( $post ); ?>
    
    			<a href="<?php echo get_permalink($post->ID); ?>" class="recentpoststitle">» <?php echo $post->post_title; ?></a><br><br>
    			<?php the_excerpt(); ?><a href="<?php echo get_permalink($post->ID); ?>" >Read more »</a>
    
    			<?php endforeach; ?>
    				            	
                	</div>   
                	
                	<div class="col-md-4 col-sm-4 col-xs-12">            	
                		
    
    			<?php $posts2 = get_posts('numberposts=1'.$cat_id.'&offset=1'); ?>
    			<?php foreach( $posts2 as $post ) : setup_postdata( $post ); ?>
    
    			<a href="<?php echo get_permalink($post->ID); ?>" class="recentpoststitle">» <?php echo $post->post_title; ?></a><br><br>
    			<?php the_excerpt(); ?><a href="<?php echo get_permalink($post->ID); ?>" >Read more »</a>
    
    			<?php endforeach; ?>
    				            	
                	</div>
                	
                	<div class="col-md-4 col-sm-4 col-xs-12">           	
                		
    
    			<?php $posts3 = get_posts('numberposts=1'.$cat_id.'&offset=2'); ?>
    			<?php foreach( $posts3 as $post ) : setup_postdata( $post ); ?>
    
    			<a href="<?php echo get_permalink($post->ID); ?>" class="recentpoststitle">» <?php echo $post->post_title; ?></a><br><br>
    			<?php the_excerpt(); ?><a href="<?php echo get_permalink($post->ID); ?>" >Read more »</a>
    
    			<?php endforeach; ?>			
                	
                	</div>          
                    
    </div>
  • Are you doing wp_reset_postdata(); after displaying the recent posts?

  • That worked, thanks!

  • I would like to thank the both of you! I had the same problem and was going bonkers trying to figure it out. And it was so simple. I feel like quite the nooblet! 😉

  • It’s the things that do this to us that make us remember them… so, yeah, I’ve been there.

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

You must be logged in to reply to this topic.