admin管理员组文章数量:1303428
HI I use plugin: Advanced Custom Fields
How can I show only 5 posts from the relationship?
This is the code:
<?php $c_lists = get_field( 'c_lists' ); ?>
<?php if ( $c_lists ) : ?>
<?php foreach ( $c_lists as $post_ids ) : ?>
<a href="<?php echo get_permalink( $post_ids ); ?>"><?php echo get_the_title( $post_ids ); ?></a>
<?php endforeach; ?>
<?php endif; ?>
Any help, please
HI I use plugin: Advanced Custom Fields
How can I show only 5 posts from the relationship?
This is the code:
<?php $c_lists = get_field( 'c_lists' ); ?>
<?php if ( $c_lists ) : ?>
<?php foreach ( $c_lists as $post_ids ) : ?>
<a href="<?php echo get_permalink( $post_ids ); ?>"><?php echo get_the_title( $post_ids ); ?></a>
<?php endforeach; ?>
<?php endif; ?>
Any help, please
Share Improve this question asked Feb 9, 2021 at 14:49 davnshidavnshi 32 bronze badges3 Answers
Reset to default 1You could use the key from the foreach
<?php
$c_lists = get_field('c_lists');
if ($c_lists) :
foreach ($c_lists as $key => $post_ids) :
if ($key > 4) break;
?>
<a href="<?= get_permalink($post_ids); ?>"><?= get_the_title($post_ids); ?></a>
<?php
endforeach;
endif;
?>
You can follow such a path
<?php if ( $c_lists ) : ?>
<?php $max = 5; $start = 0; foreach ( $c_lists as $post_ids ) :
$start++;
if( $start >= $max ) break;
?>
<a href="<?php echo get_permalink( $post_ids ); ?>"><?php echo get_the_title( $post_ids ); ?></a>
<?php endforeach; ?>
<?php endif; ?>
Yet another option: you can array_slice the array you pass into the foreach
<?php $c_lists = get_field( 'c_lists' ); ?>
<?php if ( $c_lists ) : ?>
<?php foreach ( $c_lists as array_slice( $post_ids, 0, 5 ) ) : ?>
<a href="<?php echo get_permalink( $post_ids ); ?>"><?php echo get_the_title( $post_ids ); ?></a>
<?php endforeach; ?>
<?php endif; ?>
本文标签: How can I show only 5 posts from the relationship
版权声明:本文标题:How can I show only 5 posts from the relationship? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741761819a2396452.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论