Home › Forums › Add-ons › Repeater Field › the_repeater_field not working?
Hi, there is a problem with the following block of code, thinking it may have something to do with the_repeater_field but not sure what. Wondering if someone might be able to see what’s up here?
<div>
<h3>Newsletter</h3>
<ul>
<?php $urlSplits = explode('/', $_SERVER['REQUEST_URI']); ?>
<?php $counter = 0; ?>
<?php
while(the_repeater_field('external-links')):
$tempDate = explode(',', get_sub_field('date'));
$mediaYears[] = $tempDate['1'];
$data[$counter]['year'] = $tempDate['1'];
$data[$counter]['date'] = get_sub_field('date');
$data[$counter]['link'] = get_sub_field('link');
$data[$counter]['title'] = get_sub_field('title');
$counter++;
endwhile;
?>
<?php
$keys = 0;
$mediaYears = array_unique($mediaYears);
foreach ($mediaYears as $value) {
if ($value != '') {
$uniqueYears[$keys] = $value;
$keys++;
}
}
rsort($uniqueYears);
$data = array_reverse($data, true);
if ($urlSplits[3] != '') {
$currentYear = str_replace(' ', '', $urlSplits[3]);
} else {
$currentYear = str_replace(' ', '', $uniqueYears[0]);
}
?>
<?php for($i = count($data); $i >= 0; $i--) { ?>
<?php if ($currentYear == $data[$i]['year']) {?>
<li>
<em><?php echo $data[$i]['date'] ?></em>
<strong><a>" rel="bookmark"><?php echo $data[$i]['title'] ?></a></strong>
</li>
<?php } ?>
<?php } ?>
</ul>
<?php for ($i = 0; $i < count($uniqueYears); $i++) { ?>
<a>"><?php echo $uniqueYears[$i]; ?></a>
<?php } ?>
</div>
the_repeater_field() has been deprecated since ACF version 4 or maybe even version 3, it really should not be used.
You should be using https://www.advancedcustomfields.com/resources/have_rows/
Ok, I changed it to this, didn’t break anything, but the pagination is still an issue, what am I missing here? This was working on WP 5.4 but 5.5 and beyond it is not.
<?php if(get_field('newsletter')) { ?>
<div>
<h3>Newsletter</h3>
<ul>
<?php $urlSplits = explode('/', $_SERVER['REQUEST_URI']); ?>
<?php $counter = 0; ?>
<?php
while ( have_rows('external-links') ) : the_row();
$tempDate = explode(',', get_sub_field('date'));
$mediaYears[] = $tempDate['1'];
$data[$counter]['year'] = $tempDate['1'];
$data[$counter]['date'] = get_sub_field('date');
$data[$counter]['link'] = get_sub_field('link');
$data[$counter]['title'] = get_sub_field('title');
$counter++;
endwhile;
?>
<?php
$keys = 0;
$mediaYears = array_unique($mediaYears);
foreach ($mediaYears as $value) {
if ($value != '') {
$uniqueYears[$keys] = $value;
$keys++;
}
}
rsort($uniqueYears);
$data = array_reverse($data, true);
if ($urlSplits[3] != '') {
$currentYear = str_replace(' ', '', $urlSplits[3]);
} else {
$currentYear = str_replace(' ', '', $uniqueYears[0]);
}
?>
<?php for($i = count($data); $i >= 0; $i--) { ?>
<?php if ($currentYear == $data[$i]['year']) {?>
<li>
<em><?php echo $data[$i]['date'] ?></em>
<strong><a>" rel="bookmark"><?php echo $data[$i]['title'] ?></a></strong>
</li>
<?php } ?>
<?php } ?>
</ul>
<?php for ($i = 0; $i < count($uniqueYears); $i++) { ?>
<a>"><?php echo $uniqueYears[$i]; ?></a>
<?php } ?>
</div>
<?php } ?>
<?php } ?>
You’re going to have to give me more information. What part of it is not working?
The last part (which appears to have been truncated on previous post. It is too late to edit it now)
<?php for ($i = 0; $i < count($uniqueYears); $i++) { ?>
<a href="/news/newsletter/<?php echo str_replace(' ', '', $uniqueYears[$i]); ?>/"><?php echo $uniqueYears[$i]; ?></a>
<?php } ?>
Is pagination via year (2021 2020 2019, etc.) that link to the respective year page. Just trying to paginate by year.
Sorry for the redundant post and edit, I noticed another truncated line in previous post as well, just going to re paste the full block below.
<?php if(get_field('newsletter')) { ?>
<div>
<h3>Newsletter</h3>
<ul>
<?php $urlSplits = explode('/', $_SERVER['REQUEST_URI']); ?>
<?php $counter = 0; ?>
<?php
while ( have_rows('external-links') ) : the_row();
$tempDate = explode(',', get_sub_field('date'));
$mediaYears[] = $tempDate['1'];
$data[$counter]['year'] = $tempDate['1'];
$data[$counter]['date'] = get_sub_field('date');
$data[$counter]['link'] = get_sub_field('link');
$data[$counter]['title'] = get_sub_field('title');
$counter++;
endwhile;
?>
<?php
$keys = 0;
$mediaYears = array_unique($mediaYears);
foreach ($mediaYears as $value) {
if ($value != '') {
$uniqueYears[$keys] = $value;
$keys++;
}
}
rsort($uniqueYears);
$data = array_reverse($data, true);
if ($urlSplits[3] != '') {
$currentYear = str_replace(' ', '', $urlSplits[3]);
} else {
$currentYear = str_replace(' ', '', $uniqueYears[0]);
}
?>
<?php for($i = count($data); $i >= 0; $i--) { ?>
<?php if ($currentYear == $data[$i]['year']) { ?>
<li>
<em><?php echo $data[$i]['date'] ?></em>
<strong><a href="<?php echo $data[$i]['link'] ?>" rel="bookmark"><?php echo $data[$i]['title'] ?></a></strong>
</li>
<?php } ?>
<?php } ?>
</ul>
<!-- PAGINATION LINKS -->
<?php for ($i = 0; $i < count($uniqueYears); $i++) { ?>
<a href="/news/newsletter/<?php echo str_replace(' ', '', $uniqueYears[$i]); ?>/"><?php echo $uniqueYears[$i]; ?></a>
<?php } ?>
</div>
<?php } ?>
<?php } ?>
first find out what $uniqueYears contains before that
echo '<pre>'; print_r($uniqueYears); echo '</pre>';
For each year:
Array
(
[0] => 2010
[1] => 2011
[2] => 2012
[3] => 2015
[4] => 2016
[5] => 2017
[6] => 2018
[7] => 2019
[8] => 2020
[9] => 2021
)
Yea, me neither. So, what happens is if you go to /news/newsletter/2012 it just resolves back to /news/newsletter and only shows the current year. Not quite sure how to troubleshoot this, all I know is it seems that after WP 5.5 people started complaining online about this pagination issue, yet I am coming at this issue a few years late so am not finding a whole lot to go on here.
Do you mean the links are not appearing or the links are not working?
In other words /news/newsletter/2012 is redirected back to /news/newsletter/
This is not an ACF issue. This is something else. I have no idea how links to urls based on a repeater was supposed to work on the site but this is not something that happens without quite a bit of work adding custom permalinks.
Yes, correct. OK, well, I am using this instance in two places, but one place doesn’t have a huge list so I think for that one I will just show all and not paginate to simplify this problem a bit more. If I change the above to
<?php if(get_field('media')) { ?>
<div class="info news">
<ul>
<?php $counter = 0; ?>
<?php while ( have_rows('media') ) : the_row(); ?>
<?php
$data[$counter]['date'] = get_sub_field('date');
$data[$counter]['file'] = get_sub_field('file');
$data[$counter]['title'] = get_sub_field('title');
$counter++;
?>
<?php endwhile; ?>
<?php for($i = 0; $i < count($data); $i++) { ?>
<li>
<em><?php echo $data[$i]['date'] ?></em>
<strong><a href="<?php echo $data[$i]['file'] ?>" rel="bookmark"><?php echo $data[$i]['title'] ?></a></strong>
</li>
<?php } ?>
</ul>
</div>
That works fine, however I would like to reverse the sort so the most current date is at the top of the list and not the bottom. Do you know how I might change this to do that? Thanks for your help.
This seems to work, thoughts?
<?php if(get_field('media')): ?>
<div class="info news">
<ul>
<?php $rows = get_field('media'); ?>
<?php
$articleArray = array();
foreach( $rows as $key => $row ) {
$rows[$key] = $row['id'];
$articleArray[$key]['date'] = $row['date'];
$articleArray[$key]['file'] = $row['file'];
$articleArray[$key]['title'] = $row['title'];
}
?>
<?php $rowCount = count($rows); ?>
<?php for ($i = ($rowCount - 1); $i >= 0; $i--) { ?>
<li>
<em><?php echo $articleArray[$i]['date']; ?></em>
<strong><a href="<?php echo $articleArray[$i]['file']; ?>" rel="bookmark"><?php echo $articleArray[$i]['title']; ?></a></strong>
</li>
<?php } ?>
</ul>
</div>
<?php endif; ?>
You must be logged in to reply to this topic.
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!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.