acf_repeater_shortcode
function acf_repeater_post_shortcode($atts) {
$atts = shortcode_atts(array(
'field' => '', // The repeater field name
), $atts, 'acf_repeater_post');
if (empty($atts['field'])) {
return 'ACF field not specified.';
}
if (!function_exists('have_rows')) {
return 'ACF plugin not active.';
}
ob_start(); // Start output buffering to avoid issues with echoing content
if (have_rows($atts['field'])) {
echo '<h5>関連製品</h5>';
echo '<ul class="btn-links">';
while (have_rows($atts['field'])) {
the_row();
$rpost_ids = get_sub_field('related_product');
if (is_array($rpost_ids)) {
foreach ($rpost_ids as $rpost_id) {
$post_title = get_the_title($rpost_id);
$post_link = get_permalink($rpost_id);
echo '<li><a href="' . esc_url($post_link) . '">' . esc_html($post_title) . '</a></li>';
}
}
}
echo '</ul>';
}
return ob_get_clean(); // Return buffered content
}
add_shortcode('acf_repeater_post', 'acf_repeater_post_shortcode');