Site logo

Archived topic

adding adsense inside the posts

19 replies · Started by dassana on September 5, 2018

Viewing posts 1–15 of 20

hi

i want to add adsense code inside the post article after the recipe card (as mine is a recipe website) and after 2nd paragraph of each post. i am not sure how to go about doing this. i did use the following code on the create element section, but it does not work.

function showads() {
return 'adsense code here';
}
add_shortcode('adsense', 'showads');

i cannot use the hooks mentioned as i have specific requirements about placing the ads. let me know how to go about doing this. i do not want to use any plugin for this.

i am still working on my website and have not cached it. so you won't be able to see the errors. but the cached website is using genesis theme and you can see the adsense ads in the posts. sharing a recipe link below.

thanks tom for the reply. have managed to add the ads. i have another query regarding center aligning featured image.

Hi there,

How are you adding the featured image after the first paragraph? Are you using a plugin? Or are you just adding the image inside your content?

i am using the code snippets plugin to add the code.

Try this as your function:

get_the_post_thumbnail( $post->ID, 'post-single', array( 'class' => 'aligncenter' ) )

i added the code given by you, but its giving me a fatal error.

Can you share the full code you're adding?

thanks tom. i figured out what i was doing wrong. the code works perfectly fine. thanks again.

i forgot to ask one more query. after the featured image there is no space below where the second paragraph starts. it immediately begins after the featured image. how do i add an extra space below the featured image?

hi tom, i am trying to add featured image after the paragraph, but the image gets displayed twice - each after the paragraphs. code pasted below. also do let me know how to give a space after the featured image as mentioned in my earlier query in this thread.

tom, i don't want to add ads. i want to add featured image after second paragraph.

Ads are just the example they're using. You'd replace the ad code with your featured image code:

function prefix_insert_after_paragraph2( $ads, $content ) {
    if ( ! is_array( $ads ) ) {
        return $content;
    }

    $closing_p = '</p>';
    $paragraphs = explode( $closing_p, $content );

    foreach ($paragraphs as $index => $paragraph) {
        if ( trim( $paragraph ) ) {
            $paragraphs[$index] .= $closing_p;
        }

        $n = $index + 1;
        if ( isset( $ads[ $n ] ) ) {
            $paragraphs[$index] .= $ads[ $n ];
        }
    }

    return implode( '', $paragraphs );
}

add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
    if ( is_single() && ! is_admin() ) {
        $content = prefix_insert_after_paragraph2( array(
            // The format is: '{PARAGRAPH_NUMBER}' => 'AD_CODE',
            '1' => '',
            '2' => get_the_post_thumbnail( get_the_ID(), 'post-single', array( 'class' => 'aligncenter' ) ),
        ), $content );
    }

    return $content;
}

thanks tom. i am not a techie. i replaced featured image code with the ad code in the filter function.
the entire code reads now as pasted below. do i need to change anything? the code after making the changes worked.

This archived topic is closed to new replies.