Reputation: 11
We have promotional text (text A) that we want to embed into in article (text B) at specific different points, e.g. at 25% or 50% etc. The problem is, after adding it, text A is not displaying in text B. The code was done by a developer who we can't reach. It's a WordPress snippet code in PHP. Not sure if this explanation makes sense, feel free to ask clarifying questions.
Here is the coding that we have: add_filter( 'the_content', 'move_custom_content_in_the_main_loop', 1 );
function move_custom_content_in_the_main_loop( $content ) { // Only proceed if it's a single post and the main query. // Note: Removed in_the_loop() check as it may not behave as expected with Elementor. if ( is_singular() && is_main_query() && get_the_ID() == 21197 ) { // Retrieve ACF field values. $acf_value = get_field( 'kies_promosie' ); $acf_value_2 = get_field( 'kies_promosie_2' ); $acf_value_3 = get_field( 'kies_promosie_3' );
// Split the content into paragraphs.
$paragraphs = preg_split( '/<\/p>/', $content );
$total_paragraphs = count($paragraphs);
$insertions = 0;
// Ensure there are paragraphs to work with.
if ($total_paragraphs > 0) {
// Insertions for each ACF field if they have values.
if ( $acf_value ) {
$first_position = max(round(0.25 * $total_paragraphs) - 1 + $insertions, 0);
array_splice( $paragraphs, $first_position, 0, '<p>' . do_shortcode( '[elementor-template id="8471"]' ) . '</p>' );
$insertions++;
}
if ( $acf_value_2 ) {
$second_position = max(round(0.5 * $total_paragraphs) - 1 + $insertions, 0);
array_splice( $paragraphs, $second_position, 0, '<p>' . do_shortcode( '[elementor-template id="16315"]' ) . '</p>' );
$insertions++;
}
if ( $acf_value_3 ) {
$third_position = max(round(0.75 * $total_paragraphs) - 1 + $insertions, 0);
array_splice( $paragraphs, $third_position, 0, '<p>' . do_shortcode( '[elementor-template id="16316"]' ) . '</p>' );
}
// Reconstruct the content.
$content = implode( '</p>', $paragraphs );
}
}
return $content;
}
add_filter( 'the_content', 'move_custom_content_in_the_main_loop', 1 );
function move_custom_content_in_the_main_loop( $content ) { // Only proceed if it's a single post and the main query. // Note: Removed in_the_loop() check as it may not behave as expected with Elementor. if ( is_singular() && is_main_query() && get_the_ID() == 21197 ) { // Retrieve ACF field values. $acf_value = get_field( 'kies_promosie' ); $acf_value_2 = get_field( 'kies_promosie_2' ); $acf_value_3 = get_field( 'kies_promosie_3' );
// Split the content into paragraphs.
$paragraphs = preg_split( '/<\/p>/', $content );
$total_paragraphs = count($paragraphs);
$insertions = 0;
// Ensure there are paragraphs to work with.
if ($total_paragraphs > 0) {
// Insertions for each ACF field if they have values.
if ( $acf_value ) {
$first_position = max(round(0.25 * $total_paragraphs) - 1 + $insertions, 0);
array_splice( $paragraphs, $first_position, 0, '<p>' . do_shortcode( '[elementor-template id="8471"]' ) . '</p>' );
$insertions++;
}
if ( $acf_value_2 ) {
$second_position = max(round(0.5 * $total_paragraphs) - 1 + $insertions, 0);
array_splice( $paragraphs, $second_position, 0, '<p>' . do_shortcode( '[elementor-template id="16315"]' ) . '</p>' );
$insertions++;
}
if ( $acf_value_3 ) {
$third_position = max(round(0.75 * $total_paragraphs) - 1 + $insertions, 0);
array_splice( $paragraphs, $third_position, 0, '<p>' . do_shortcode( '[elementor-template id="16316"]' ) . '</p>' );
}
// Reconstruct the content.
$content = implode( '</p>', $paragraphs );
}
}
return $content;
}
Upvotes: 1
Views: 9