Site logo

[Resolved] adding adsense inside the posts

Home Forums Support [Resolved] adding adsense inside the posts

Home Forums Support adding adsense inside the posts

  • This topic has 19 replies, 3 voices, and was last updated 7 years ago by David.
Viewing 15 posts - 1 through 15 (of 20 total)
  • Author
    Posts
  • #669585
    dassana

    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.

    #669896
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    If you want to hook something into the middle of your content, you’d likely need to go with a plugin like this: https://wordpress.org/plugins/ad-inserter/

    Let me know 🙂

    #670424
    dassana

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

    #670750
    Tom
    Lead Developer
    Lead Developer

    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?

    #670838
    dassana

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

    #671269
    Tom
    Lead Developer
    Lead Developer

    Try this as your function:

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

    #671283
    dassana

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

    #671579
    Tom
    Lead Developer
    Lead Developer

    Can you share the full code you’re adding?

    #671666
    dassana

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

    #671668
    dassana

    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?

    #671677
    dassana

    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.

    #671950
    Tom
    Lead Developer
    Lead Developer

    This might help: https://stackoverflow.com/questions/48991557/adding-ads-after-first-and-second-paragraph-of-wordpress-post

    You can just leave the code for after the second paragraph empty.

    #671965
    dassana

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

    #671970
    Tom
    Lead Developer
    Lead Developer

    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;
    }
    #671977
    dassana

    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.

Viewing 15 posts - 1 through 15 (of 20 total)
  • You must be logged in to reply to this topic.