Support

Account

Home Forums Front-end Issues Some fields show parent-page value instead of current-page value.

Solved

Some fields show parent-page value instead of current-page value.

  • Hi – I’m having a problem calling one of my custom fields with “the_field”. I’m pulling subpages into a single parent page and using more of a long-page format to display content. All but 2 of the fields come in fine. However, these two display the field content for the parent page. It’s really strange behavior. Please help!

    I have about 6 custom fields. 4 of them are pulling in just fine – for instance the unique icon for each page:

    <?php	
    	if(get_field('page-icon', $page->ID)) { ?>
    	
    	<!-- Icon -->
    	<img class="icon" src="<?php the_field('page-icon', $page->ID) ;?>" /> 
    	
    <?php } ?>

    However, two particular fields do not pull in correctly – the ‘page-link’ custom field and the ‘page-link-title’ field. These are text custom fields where the user inputs a URL and title. They always return the value of the parent-page and NOT the child page, even though the rest of the fields work just fine. It doesn’t matter what I enter into the child-page custom fields, this code returns the value for the parent page.

    For reference, here’s the entire loop in context.

    <?php
    	$mypages = get_pages( array( 
    		'child_of' => $post->ID, 
    		'sort_column' => 'menu_order',
    		'sort_order' => 'asc' )
    	);
    
    	foreach( $mypages as $page ) {		
    		$content = $page->post_content;
    		if ( ! $content ) // Check for empty page
    			continue;
    
    		$content = apply_filters( 'the_content', $content );
    		
    		// get template slug
    		$pagetemplateurl = get_post_meta($page->ID, '_wp_page_template', true);	 	
    	 	$pagetemplateinfo = pathinfo($pagetemplateurl);
    		$pagetemplateslug = basename($pagetemplateurl, '.'.$pagetemplateinfo['extension']);
    
    ?>
    
    	<?php
    	if(get_field('the-image', $page->ID)) { ?>
    		<!-- Photo Feature -->
    		<div class="photo-feature" style="background: url(<?php the_field('the-image', $page->ID) ;?>) no-repeat center center; ">          
                    </div>
    	<?php }	?>
    
    	<div class="page-child <?php echo ($pagetemplateslug); ?>">
    
    	<?php	
    	if(get_field('page-icon', $page->ID)) { ?>
    			
    		<!-- Icon -->
    		<img class="icon" src="<?php the_field('page-icon', $page->ID) ;?>" /> 	
    	<?php } ?>	
    
    	<?php	
    	if(get_field('page-dynamic', $page->ID)) { ?>
    			
    		<!-- Dynamic Content -->
    		<div class="page-dynamic">
    		    	<?php the_field('page-dynamic', $page->ID) ;?>
    		</div> 
    	<?php } ?>
    			
    	<div class="page-content-container">
    		<!-- Title -->   
    		<h2 class="page-title"><?php the_field('page-title', $page->ID) ;?></h2>
    		<?php	    	
    		if(get_field('page-subtitle', $page->ID)) { ?>			
    			<!-- Subtitle -->
    			<h6 class="page-subtitle"><?php the_field('page-subtitle', $page->ID) ;?></h6>
    		<?php } ?>			
    		<hr />			
    		
                    <!-- Content -->
    		<div class="page-content">
    			<?php echo $content; ?>
    		</div>			
    	
    		<?php
    		if(get_field('page-link', $page->ID)) { ?>
    			<!-- The Page Link -->
    			<div class="page-link">
    				<a href="<?php the_field('page-link') ;?>"><?php the_field('page-link-title') ;?></a>
    			</div>
    		<?php } ?>
    	
    	</div> <!-- /page-container -->
    	
    	<div class="clearfix"></div>
    
    </div><!-- /page-child -->
    	
    <?php } ?>

    Is this a bug? What am I doing wrong? I’m calling <?php echo $content; ?> just before the page-link code. Is this somehow ending my loop prematurely? Why are these values defaulting back to the parent-page values?

  • Do you think this has something to do with $content being called? It seems like that’s interrupting my loop and I’m getting the default value.

  • Hi @timshutes

    The code:

    
    <a href="<?php the_field('page-link') ;?>"><?php the_field('page-link-title') ;?></a>
    

    is missing the $post_id parameter! That is why it is not loading from the child page

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

The topic ‘Some fields show parent-page value instead of current-page value.’ is closed to new replies.