Support

Account

Home Forums Front-end Issues How to display custom post with acf fields from within repeater in flexible cont

Solved

How to display custom post with acf fields from within repeater in flexible cont

  • Hi.

    I have created custom post called TV programs, added acf field which describe it + title and content.
    This is a list of tv programs.
    Now I would like to list them in the page Golden package, based on the category.

    My setup for this page is type / field name
    Flexible Content Field – kanaly_telewizyjne_na_stronie
    – template / Category – kategoria_kanalu
    — category name – nazwa_kategorii_kanalu
    — Reapeter field – Program’s list – program_list
    — Object field / Canal – canal

    Now i would like to be able to add categories (which works) and then assing canals via Object field.

    I know I am close but I can’t get it to work. It’s like Inception, to many levels 😉

    Checking post data with print_r, shows 2 posts being listed, but there is no acf data.

    Can someone help me ?

    <div class="pageBodyKanaly">
    								<?php // check if the flexible content field has rows of data ?>
    								<?php if( have_rows('kanaly_telewizyjne_na_stronie') ): ?>
    								<ul class="listaKanalow">
    									<?php // loop through the rows of data ?>
    									<?php while ( have_rows('kanaly_telewizyjne_na_stronie') ) : the_row(); ?>
    										<li class="kanalElement">
    											<?php // check current row layout ?>
    											<?php if( get_row_layout() == 'kategoria_kanalu' ):  ?>
    												
    												<div class="kategoriaKanalow">
    													<?php if( get_sub_field('nazwa_kategorii_kanalu')): ?>
    														<h2><?php the_sub_field('nazwa_kategorii_kanalu'); ?></h2>
    													<?php endif; ?>
    												</div>
    												
    												<section class="slider">
    													<?php if( have_rows('programy_lista') ): ?>
    														<div id="kanaly">
    															<ul class="slides">
    															<?php while ( have_rows('programy_lista') ) : the_row(); ?>   
    																<li>
    																	<?php $post_object = get_sub_field('kanal'); ?>
    																	<?php if( $post_object ): ?>
    																		<?php $post = $post_object; setup_postdata( $post ); ?>
    																		<?php print_r($post); ?>
    																		<h2><?php the_title(); ?>"</h2>
    																		<?php wp_reset_postdata(); ?>
    																	<?php endif; ?>
    																</li>
    															<?php endwhile; ?>
    															</ul>
    														</div>
    													<?php endif; ?>
    												</section>
    											<?php endif; ?>
    										</li>
    									<?php endwhile; ?>
    								</ul>
    								<?php else: ?>
    									<?php // no layouts found ?>
    								<?php endif; ?>
    							</div>
  • Hi @mastafu

    I’m sorry I don’t understand what you want. Could you please explain it to me in more detail? If you can provide me some screenshots of the issue and what you want, that would be great. Also, could you please share the screenshot of your field group setup so I can understand the situation.

    Thanks!

  • I want to create page which lists posts wit categories.

    Page has flexible content, which creates categories for selected posts.

    Posts have a acf fields.
    Posts are custom post type.
    So…
    I want to loop post object in repeater in flexible content.

  • I am bit closer to my goal. At this moment I just can’t access acf fields from the posts listed in the page via repeater and flexi content. Weird …

    This is my new code that works.
    Please see attached screens of my setup.

    <ul class="pageBodyKanaly">
    								<?php // check if the flexible content field has rows of data ?>
    								<?php if( have_rows('kanaly_telewizyjne_na_stronie') ): ?>
    								
    									<?php // loop through the rows of data ?>
    									<?php while ( have_rows('kanaly_telewizyjne_na_stronie') ) : the_row(); ?>
    										<li class="kanalElement">
    											<?php // check current row layout ?>
    											<?php if( get_row_layout() == 'kategoria_kanalu' ):  ?>
    												
    												<div class="kategoriaKanalow">
    													<?php if( get_sub_field('nazwa_kategorii_kanalu')): ?>
    														<h2><?php the_sub_field('nazwa_kategorii_kanalu'); ?></h2>
    													<?php endif; ?>
    												</div>
    												
    												<section class="kanalyBody">
    													<?php if( have_rows('programy_lista') ): ?>
    														<ul id="kanaly">
    															<?php while ( have_rows('programy_lista') ) : the_row(); ?>   
    																<li>
    																	<?php
    																		$post_objects = get_sub_field('kanal');
    																		if( $post_objects ): ?>
    																			<ul>
    																			<?php foreach( $post_objects as $post): // variable must be called $post (IMPORTANT) ?>
    																				<?php setup_postdata($post); ?>
    																				<li>
    																					<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    																					<?php the_content(); ?>
    																					<?php if( get_sub_field('klasa_obrazka')): ?>
    																						<h5><?php the_sub_field('klasa_obrazka'); ?></h5>
    																					<?php endif; ?>
    																				</li>
    																			<?php endforeach; ?>
    																			</ul>
    																			<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    																		<?php endif; ?>
    																</li>
    															<?php endwhile; ?>
    															</ul>
    													<?php endif; ?>
    												</section>
    											<?php endif; ?>
    										</li>
    									<?php endwhile; ?>
    							</ul>
  • Hi @mastafu

    Thanks for the screenshots and explanation.

    You need to use get_field() instead of get_sub_field() when you try to get the field from a post object. You can also set the ID for the second parameter of get_field() to make sure it gets the right value. It should be something like this:

    <?php if( get_field('klasa_obrazka', $post->ID)): ?>
        <h5><?php the_field('klasa_obrazka', $post->ID); ?></h5>
    <?php endif; ?>

    I hope this helps.

  • Thank you that was the case to solve my riddle 🙂

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

You must be logged in to reply to this topic.