- This topic has 19 replies, 3 voices, and was last updated 7 years ago by
David.
-
AuthorPosts
-
September 5, 2018 at 10:14 am #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.
September 5, 2018 at 6:27 pm #669896Tom
Lead DeveloperLead DeveloperHi 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 🙂
September 6, 2018 at 9:03 am #670424dassana
thanks tom for the reply. have managed to add the ads. i have another query regarding center aligning featured image.
September 6, 2018 at 7:25 pm #670750Tom
Lead DeveloperLead DeveloperHi 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?
September 6, 2018 at 11:32 pm #670838dassana
i am using the code snippets plugin to add the code.
September 7, 2018 at 8:33 am #671269Tom
Lead DeveloperLead DeveloperTry this as your function:
get_the_post_thumbnail( $post->ID, 'post-single', array( 'class' => 'aligncenter' ) )
September 7, 2018 at 8:47 am #671283dassana
i added the code given by you, but its giving me a fatal error.
September 7, 2018 at 6:47 pm #671579Tom
Lead DeveloperLead DeveloperCan you share the full code you’re adding?
September 7, 2018 at 10:57 pm #671666dassana
thanks tom. i figured out what i was doing wrong. the code works perfectly fine. thanks again.
September 7, 2018 at 11:04 pm #671668dassana
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?
September 7, 2018 at 11:40 pm #671677dassana
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.
September 8, 2018 at 8:51 am #671950Tom
Lead DeveloperLead DeveloperThis 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.
September 8, 2018 at 9:11 am #671965dassana
tom, i don’t want to add ads. i want to add featured image after second paragraph.
September 8, 2018 at 9:20 am #671970Tom
Lead DeveloperLead DeveloperAds 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; }
September 8, 2018 at 9:31 am #671977dassana
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. -
AuthorPosts
- You must be logged in to reply to this topic.