Here i would like share how to put Adsense code or any other ads code inside the WordPress post content 1. Go to Appearance –> Editor and select functions.php 2. Copy and past the following code in functions.php and save it function inject_ad_text_after_n_chars($content) { // only do this if post is longer than 1000 characters $enable_length = 1000; // insert after the first </p> after 500 characters $after_character = 500; if (is_single() && strlen($content) > $enable_length) { $before_content = substr($content, 0, $after_character); $after_content = substr($content, $after_character); $after_content = explode('</p>', $after_content); $text = ' <!-- PUT YOUR AD HERE --> '; array_splice($after_content, 1, 0, $text); $after_content = implode('</p>', $after_content); return $before_content . $after_content; } else { return $content; } } add_filter('the_content', 'inject_ad_text_after_n_chars'); PHP: 3.After adding the above code replace <!– PUT YOUR AD HERE –> with your adsense or any advertisement code.
Great tip ! What if I want to show code on my home page, where I can't use more than three ad blocks, since google doesn't allow more than 3 link ad blocks !
As the other poster pointed out, this may not be a good idea for AdSense. Terms may not allow it (I don't know if they do or not). If you are bent on placing ads on every post on the home page, then change the conditional if statement where it checks for is_single to: if ( [B](is_single() || is_home())[/B] && strlen($content) > $enable_length) { Code (markup): Note that I included an OR conditional within containing parenthesis. This check is done before applying the AND on the end.
I have only 1 ad on the index php so it should be a problem, but when i insert the code you give me it won's show up on theCategories, can you help me with that?
Add more conditionals to the OR clause of that line. You have things like is_category(), is_page(), etc. Not that hard to figure out. Search for conditional tags at the WordPress Codex.
No problem. Rep me! Would want to add, that will only work if you are displaying the entire post on those pages. If you are using "truncated" the_content() or the_excerpt() inside of those loops, then the ad will not show. You can show ads by keeping count of the items that are displayed, and showing ads every 3rd item (for example).