Support

Account

Home Forums Front-end Issues Custom fields placed after a while/endwhile statement aren't showing up.

Solved

Custom fields placed after a while/endwhile statement aren't showing up.

  • I have 2 custom fields (cta_blurb_3 & cta_blurb_2) that come after a custom post type while/endwhile loop. These 2 custom fields don’t show up. If I remove the while/endwhile loop, the 2 custom fields show up. Same thing, if I move the custom fields up before the while/endwhile loop, they’ll show up. I can’t figure out where the conflict is. My code is as follows:

    <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
    					<div id="default-page-top">
                        	<h1><?php the_field('h1'); ?></h1>
                        	<div id="default-blue-box">
                            	<div id="default-top-content">
                            		<div id="default-top-left">
                                	 <?php if( get_field( "h2" ) ): ?>
                                	<h2><?php the_field('h2'); ?></h2>
                                	<?php endif; ?>
                                	<?php if( get_field( "h3" ) ): ?>
                                	<h3><?php the_field('h3'); ?></h3>
                                	<?php endif; ?>
                                	</div><!-- #default-top-left -->
                                	<div id="default-top-right">
                                	<?php the_field('cta_blurb'); ?>
                                	</div><!-- #default-top-right -->
                            	</div><!-- #default-top-content -->
                            </div><!-- #default-blue-box -->
                        </div><!-- #default-page-top -->
    				<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    					<div class="entry-content">
    					<div class="entry-content2">
                        	<?php $args = array( 'post_type' => 'good_impacts', 'posts_per_page' => 6 );
    						$loop = new WP_Query( $args );
    						while ( $loop->have_posts() ) : $loop->the_post();
    						echo '<div id="event-item">';
    						echo '<div id="event-image">';
    						the_post_thumbnail('thumbnail');
    						echo '</div>';
    						echo '<div id="event-right"><h2>';
    						the_title();
    						echo '</h2>';
    						echo '<div id="event-content">';
    						the_content();
    						echo '</div></div></div>';
    						endwhile;?>
    					</div><!-- .entry-content2 -->
                        	<?php if( get_field( "cta_blurb_3" ) ): ?>
                            <div id="page-bottom-cta">
                        	<?php the_field('cta_blurb_3'); ?>
                            </div><!-- #page-bottom-cta -->
                        	<?php endif; ?>
    						<?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'twentyten' ), 'after' => '</div>' ) ); ?>
    						<?php edit_post_link( __( 'Edit', 'twentyten' ), '<span class="edit-link">', '</span>' ); ?>
    					</div><!-- .entry-content -->
                        <div id="default-right-sidebar">
                        <?php if( get_field( "cta_blurb_2" ) ): ?>
                        <?php the_field('cta_blurb_2'); ?>
                        <?php endif; ?>
                        <?php dynamic_sidebar( 'default-ad-area' ); ?>
                        </div><!-- #default-right-sidebar -->
    				</div><!-- #post-## -->
    
    <?php endwhile; // end of the loop. ?>

    Also, all of the other page elements show up just fine. It’s only the custom fields that don’t show up.

    Any ideas/suggestions are appreciated.

    Thanks in advance!
    Jeff.

  • Hi @jeff_deuce
    Without looking at your code, it sounds like the issue is that during the loop you are overriding the global $post object.

    Outside of the loop, you will need to run the wp_reset_postdata function to return the $post to it’s origional value!

    Then your get_field / the_field will work.

    There are some good examples of this in the post_object documentation

    Thanks
    E

  • Thanks for the prompt reply! That was the solution! I would have never figured that one out!
    Jeff.

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

The topic ‘Custom fields placed after a while/endwhile statement aren't showing up.’ is closed to new replies.