• Hi,

    I want to add an html code in every 3 paragraphs, how can i do? I can do it with a plug-in but I don’t want to use it? Is there any code I can add into functions.php to add html code every 3 paragraphs?

    Can you help me?

    • This topic was modified 3 years, 4 months ago by Jan Dembowski.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    Yes. The key is to use get_the_content(), parse out the paragraphs (or blocks), and insert your HTML appropriately or write a filter for the_content. Writing a filter is probably a better option.

    Note that you do not want to edit the core’s functions.php file or the functions.php file that comes with your theme, because an update to either will remove your modifications. It would be better to write a simple plugin instead.

    Thread Starter unnamed

    (@yazarmfa)

    I’m sorry I did not understand.
    How can I add the following code every three paragraphs.
    I want to automatically add it to the posts on my pages.

    <amp-ad width="100vw" height="320"
         type="adsense"
         data-ad-client="ca-pub-xxxxxxxx"
         data-ad-slot="xxxxxxxx"
         data-auto-format="rspv"
         data-full-width="">
      <div overflow=""></div>
    </amp-ad>

    I want to add this code after paragraph 3, paragraph 6, paragraph 9, paragraph 12. 15. 18. 21…

    • This reply was modified 3 years, 11 months ago by unnamed.
    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    If you’re not comfortable coding in PHP, then you should consider hiring a developer. Otherwise, take a look at https://stackoverflow.com/questions/48991557/adding-ads-after-first-and-second-paragraph-of-wordpress-post for something close to what you want. Or, look at this: https://www.ads-software.com/plugins/ad-inserter/

    I can do it with a plug-in but I don’t want to use it?

    Please understand that plugins are simply containers for code that is not in WordPress core. If you want WP to do something that it doesn’t do, the code has to reside somewhere. Putting code into the theme functions.php is a bad practice since you don’t have an easy way to turn it on or off, and switching themes makes it a mess. Since you know of a plugin already, just use it. It has already been tested, and likely has fewer security risks than code that a novice writes.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Adding html code every 3 paragraphs’ is closed to new replies.