The client will be entering information for the year a report was produced (eg. 2010). I want to take that information and calculate if it was made in the last 3 years so I can display it in a 'recent documents' section. Any other report that doesn't match the result will be sent to an archives section. Am I thinking through this correctly?
<?php if( have_rows('recent_documents') ): ?>
<ul class="list-unstyled">
<?php while( have_rows('recent_documents') ): the_row(); ?>
<?php if ( have_rows('new_document') ): ?>
<?php while ( have_rows('new_document') ): the_row(); ?>
<?php $year = the_sub_field('year'); ?>
<?php if ( $year > date("Y",strtotime("-1 year"))) : ?>
<li>
<a href="<?php the_sub_field('file'); ?>" target="blank"><?php the_sub_field('year'); ?> <?php the_sub_field('report_type'); ?><span><i class="fas fa-download"></i></span></a>
</li>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; endwhile; ?>
</ul>
<?php endif; ?>
The client will be entering information for the year a report was produced (eg. 2010). I want to take that information and calculate if it was made in the last 3 years so I can display it in a 'recent documents' section. Any other report that doesn't match the result will be sent to an archives section. Am I thinking through this correctly?
<?php if( have_rows('recent_documents') ): ?>
<ul class="list-unstyled">
<?php while( have_rows('recent_documents') ): the_row(); ?>
<?php if ( have_rows('new_document') ): ?>
<?php while ( have_rows('new_document') ): the_row(); ?>
<?php $year = the_sub_field('year'); ?>
<?php if ( $year > date("Y",strtotime("-1 year"))) : ?>
<li>
<a href="<?php the_sub_field('file'); ?>" target="blank"><?php the_sub_field('year'); ?> <?php the_sub_field('report_type'); ?><span><i class="fas fa-download"></i></span></a>
</li>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; endwhile; ?>
</ul>
<?php endif; ?>
Share
Improve this question
edited Oct 18, 2018 at 22:22
fridayikon
asked Oct 18, 2018 at 21:52
fridayikonfridayikon
34 bronze badges
2 Answers
Reset to default 0First thing to fix is the_sub_field('year')
, as it will print out your year string instead of passing it to your variable. Instead, I think you want
$year = get_sub_field('year');
And, to answer your question, it's easy to convert a string to an int by passing (int)
in front of the item.
$year = (int)get_sub_field('year');
Advanced Custom Fields (ACF) support forums… You can filter a field "type" to cast to and then return a specific data type.
function strict_type_acf_numeric_field($value, $post_id, $field): int
{
$value = (int) $value;
return $value;
}
add_filter('acf/format_value/type=range', '926i_strict_type_acf_numeric_field', 10, 3);
add_filter('acf/format_value/type=number', '926i_strict_type_acf_numeric_field', 10, 3);
reference link