Home › Forums › Add-ons › Repeater Field › Display repeater fields in child pages
I have a list menu using the Repeater field type (see the code below) and each list has a link to an anchor ID within the page.
I also want to display the same menu in the child pages so that each list will link to an anchor ID in the parent page.
How can I accomplish this?
<?php if(get_field('content-container')): ?>
<ul>
<?php while(has_sub_field('content-container')): ?>
<li><a href="<?php the_permalink() ?>#<?php the_sub_field('anchor'); ?>"><?php the_sub_field('header'); ?></a></li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
Hi @sasori390,
I think this should just about do it.. this can be used outside of the loop which I assume you are doing. If not you could probably remove the global $post.
<?php
global $post;
if($post->post_parent){
$postid = $post->post_parent;
}else{
$postid = $post->ID;
}
?>
<?php if(get_field('content-container', $postid)): ?>
<ul>
<?php while(has_sub_field('content-container', $postid)): ?>
<li><a href="<?php the_permalink() ?>#<?php the_sub_field('anchor'); ?>"><?php the_sub_field('header'); ?></a></li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
Thanks Jonathan for your response.
Just tried your code and I got the list displayed just the way I want. The only problem is the <?php the_permalink() ?> displays the current url, not the parent url. Any clue?
Great,
Yes since you do not output the repeater as posts get_permalink() retrieves the current pages link. in order to change this you can do:
the_permalink($postid);
then it’ll fetch either current page or parent page (if there is one). Just like the_field and get_field.
Hi Jonathan, I still cannot get this working for some reason. the_permalink($postid); still displays the current page url instead of the parent.
Sorry I’m no php expert, I’d appreciate if you could tell me what’s wrong. Here is the entire code of the actual template.
<?php
/*
Template Name: Page CEO Massage
*/
?>
<?php get_header(); ?>
<section id="slider">
<div id="page-title-container">
<h1 id="page-title"><?php
echo empty( $post->post_parent ) ? get_the_title( $post->ID ) : get_the_title( $post->post_parent );
?></h1>
</div>
</section>
<section id="content">
<div id="col-lt">
<?php
global $post;
if($post->post_parent){
$postid = $post->post_parent;
}else{
$postid = $post->ID;
}
?>
<?php if(get_field('content-container', $postid)): ?>
<ul>
<?php while(has_sub_field('content-container', $postid)): ?>
<li><a href="<?php the_permalink($postid); ?>#<?php echo preg_replace('/[^A-Za-z0-9]/', "", get_sub_field('header')); ?>"><?php the_sub_field('header'); ?></a></li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
</div>
<div id="col-rt" class="top_no_padding">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<p><?php the_field('ceo_message'); ?></p>
<?php endwhile; endif; ?>
</div>
</section>
<?php get_footer(); ?>
lol sorry, I wrote get_permalink in the text and the_permalink in the code..
it should be
echo get_permalink($postid);
Np 🙂
for future reference, get_permalink() can take one parameter which is a post id while the_permalink does not.. that is why it didnt work.
The topic ‘Display repeater fields in child pages’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
🚨 The 2023 ACF Annual Survey closes tomorrow! This is your last chance to complete the survey and help guide the evolution of ACF. https://t.co/0cgr9ZFOJ5
— Advanced Custom Fields (@wp_acf) May 18, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.