Support

Account

Home Forums Front-end Issues Displaying and linking to the post chosen within Flexible Content layout field

Solved

Displaying and linking to the post chosen within Flexible Content layout field

  • I am struggling with being able to link to the post that is chosen within a Flexible Content layout field.

    I have been able to display at the front end the post title and the excerpt. However, any link I make only links to the page that is displaying the ACF fields and not the actual post. I am a tweaker of PHP and not fluent in its use. Below is the relevant section of my code: Help much appreciated.

    <?php if(get_row_layout() == “two_eq_columns”): ?>

    <div class=”two-eq-cols”>
    <?php
    $TheID = get_sub_field(‘left_50-50_column’);
    $TheID = $TheID->ID;
    $page_data = get_page($TheID);
    ?>

    “>
    <?php the_sub_field(‘two_eq_columns’); ?><h2>
    <?php echo $page_data->post_title; ?></h2>

    <?php echo $page_data->post_excerpt; ?>

    </div>

    <div class=”two-eq-cols”>
    <?php
    $TheID = get_sub_field(‘right_50-50_column’);
    $TheID = $TheID->ID;
    $page_data = get_page($TheID);
    ?>

    “>
    <?php the_sub_field(‘two_eq_columns’); ?><h2>
    <?php echo $page_data->post_title; ?></h2>

    <?php echo $page_data->post_excerpt; ?></div>

    <div class=”clearit”></div>

    <?php endif; ?>

    Thanks.
    Janice

  • Sorry, I think the code copied over strangely. Here it is again:

    <?php if(get_row_layout() == “two_eq_columns”): ?>

    <div class=”two-eq-cols”>
    <?php
    $TheID = get_sub_field(‘left_50-50_column’);
    $TheID = $TheID->ID;
    $page_data = get_page($TheID);
    ?>

    “><?php the_sub_field(‘two_eq_columns’); ?><h2><?php echo $page_data->post_title; ?></h2>

    <?php echo $page_data->post_excerpt; ?>

    </div>

    <div class=”two-eq-cols”>
    <?php
    $TheID = get_sub_field(‘right_50-50_column’);
    $TheID = $TheID->ID;
    $page_data = get_page($TheID);
    ?>

    “><?php the_sub_field(‘two_eq_columns’); ?><h2><?php echo $page_data->post_title; ?></h2>

    <?php echo $page_data->post_excerpt; ?></div>

    <div class=”clearit”></div>

    <?php endif; ?>

  • Hi @jantye
    Can you please edit or repost the code in code tags so it’s readable to us nerds 😉

    You’ll find the “code” button in the toolbar above the text input field.

  • <?php if(get_row_layout() == "two_eq_columns"): ?>
    	 
    	<div class="two-eq-cols">
    	<?php 	 				 			
    	$TheID = get_sub_field('left_50-50_column');
    	$TheID = $TheID->ID;
    	$page_data = get_page($TheID);	 			
    	?>
                    
                    
    	 			
    <a href="<?php echo $post_object; ?>">
    <?php the_sub_field(‘two_eq_columns’); ?><h2>
    <?php echo $page_data->post_title; ?></h2></a>
                
    	<?php echo $page_data->post_excerpt; ?>		
    
    	 		</div>
    
    	<div class="two-eq-cols">
    	<?php 	 				 			
    	$TheID = get_sub_field('right_50-50_column');
    	$TheID = $TheID->ID;
    	$page_data = get_page($TheID);
    	?>
    
    <a href="<?php echo $post_object; ?>">
    <?php the_sub_field(‘two_eq_columns’); ?><h2>
    <?php echo $page_data->post_title; ?></h2></a>
                    
    <?php echo $page_data->post_excerpt; ?></div>
    	 		
    	<div class="clearit"></div>
    
    		<?php endif; ?>	

    Thanks Jonathan. Hadn’t spotted that.

  • No problem @jantye ! Now you know 🙂

    I think I understand what your code is supposed to do and you have quite a bit of unnecessary code going on. Here’s a cleaned up version which should also have working links. I have of course not tested the code so there might be errors but hopefully it’ll work right out of the bat!

    
    <?php if(get_row_layout() == "two_eq_columns"): ?>
    	 
    	<div class="two-eq-cols">
    	
    		<?php $page_object = get_sub_field('left_50-50_column'); ?>
    		<a href="<?php echo get_permalink($page_object->ID); ?>">
    			<?php the_sub_field('two_eq_columns'); ?>
    			<h2><?php echo $page_object->post_title; ?></h2>
    		</a>
    		<?php echo $page_object->post_excerpt; ?>		
    
    	 </div>
    
    	<div class="two-eq-cols">
    	
    		<?php $second_page_object = get_sub_field('right_50-50_column'); ?>
    		<a href="<?php echo get_permalink($second_page_object->ID); ?>">
    			<?php the_sub_field('two_eq_columns'); ?>
    			<h2><?php echo $second_page_object->post_title; ?></h2>
    		</a>
    		<?php echo $second_page_object->post_excerpt; ?></div>
    		<div class="clearit"></div>
    		
    	</div>
    
    <?php endif; ?>
    
  • Oh joy! It works. Thank you so much you very clever person.

    Now I can study the code and learn for the future.

    Best wishes.
    Janice

  • You’re very welcome Janice.

    Being a developer is a never-ending learning experience and as long as we keep learning we can be sure we’re on the right path.

    I wish you good luck with your project and feel free to post in the forums if you need any help with ACF.

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

The topic ‘Displaying and linking to the post chosen within Flexible Content layout field’ is closed to new replies.