0

Trying to use ACF to determine when to insert an ad after # many paragraphs. Right now after 3 paragraphs the ad is inserted.

if ( is_single() && ! is_admin() ) {
    $show_after = get_field('show_after');
    return prefix_insert_after_paragraph( $ad_code, 3, $content );
}

I'm having issues mapping 3 to the variable $show_after. Returns nothing in below code:

if ( is_single() && ! is_admin() ) {
    $show_after = get_field('show_after');
    return prefix_insert_after_paragraph( $ad_code, $show_after, $content );
}

Is there any way I can map the 3 to a variable so the poster can input the paragraph amount?

1
  • This question is not actually about ACF, so you are in luck ;) As by the rules of this site, third-party plugins are offtopic, and should be dealt with in their respective support forums.
    – fischi
    Feb 24, 2016 at 15:34

1 Answer 1

1

You need a post_meta field to let an author decide after how many paragraphs the ad is inserted, if you want to do it like that, and not with a shortcode.

So create a custom field called f711_insert_after_no_p, and use your function like this:

if ( is_single() && ! is_admin() ) {
    $show_after = get_field('show_after');
    return prefix_insert_after_paragraph( $ad_code, get_post_meta( get_the_ID(), 'f711_insert_after_no_p', true ), $content );
}

Be sure to validate the input to only allow integers, or do this in the output.

I think for ACF the code (instead of get_post_meta( get_the_ID(), 'f711_insert_after_no_p', true ) ) would be

get_the_field( 'f711_insert_after_no_p' )
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.