Skip to content

Instantly share code, notes, and snippets.

@stefanRepac
Last active October 9, 2019 11:36
Show Gist options
  • Select an option

  • Save stefanRepac/8d65d9bf9b5ca1ba33ccaa276e7206af to your computer and use it in GitHub Desktop.

Select an option

Save stefanRepac/8d65d9bf9b5ca1ba33ccaa276e7206af to your computer and use it in GitHub Desktop.
Wordpress ACF repeater - Get content by row and put in the_content() - paragraph counter
<?php
$rows = get_field('sidebar_commercials', 'option' ); // get all the rows
// $rand_row = $rows[ array_rand( $rows ) ]; // get a random row
$row_count = count(get_field('sidebar_commercials', 'option')); // Count
if ($row_count >= 1) {
$first_baner = $rows[0];
$baner_image_first = $first_baner['widget_commercial_image' ]; // get the sub field value
$baner_url_first = $first_baner['widget_commercial_url' ]; // get the sub field value
$image_url_first = $baner_image_first['sizes']['large'];
$image_alt_first = $baner_image_first['alt'];
$row_1 = '<div class="inline-baner">';
$row_1 .= '<a href="' . $baner_url_first . '">' ;
$row_1 .= '<img src="' . $image_url_first . '" alt="' . $image_alt_first . '">';
$row_1 .= '</a>';
$row_1 .= '</div>';
$banerAfter[3] = $row_1; //display after the third paragraph
}
if ($row_count >= 2) {
$second_baner = $rows[1];
$baner_image_second = $second_baner['widget_commercial_image' ]; // get the sub field value
$baner_url_second = $second_baner['widget_commercial_url' ]; // get the sub field value
$image_url_second = $baner_image_second['sizes']['large'];
$image_alt_second = $baner_image_second['alt'];
$row_2 = '<div class="inline-baner">';
$row_2 .= '<a href="' . $baner_url_second . '">' ;
$row_2 .= '<img src="' . $image_url_second . '" alt="' . $image_alt_second . '">';
$row_2 .= '</a>';
$row_2 .= '</div>';
$banerAfter[6] = $row_2; //display after the third paragraph
}
if ($row_count >= 3) {
$third_baner = $rows[2];
$baner_image_third = $third_baner['widget_commercial_image' ]; // get the sub field value
$baner_url_third = $third_baner['widget_commercial_url' ]; // get the sub field value
$image_url_third = $baner_image_third['sizes']['large'];
$image_alt_third = $baner_image_third['alt'];
$row_3 = '<div class="inline-baner">';
$row_3 .= '<a href="' . $baner_url_third . '">' ;
$row_3 .= '<img src="' . $image_url_third . '" alt="' . $image_alt_third . '">';
$row_3 .= '</a>';
$row_3 .= '</div>';
$banerAfter[9] = $row_3; //display after the third paragraph
}
$content = apply_filters( 'the_content', get_the_content() );
$content = explode("</p>", $content);
$count = count($content);
for ($i = 0; $i < $count; $i++ ) {
if ( array_key_exists($i, $banerAfter) ) {
echo $banerAfter[$i];
}
echo $content[$i] . "</p>";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment