[WordPress] How to replace the more tag in an article with an advertisement banner
One day, when I was looking at various people's blogs, I noticed that there were many blogs that displayed advertisements such as Google AdSense in the middle of their articles.
After thinking about how everyone is doing it and what I should do if I were to implement it on my blog, I came to the conclusion that replacing the more tag would be the easiest to manage.
So, I tried it.
What is more tag?
The more tag is an example of where you want to insert "Read more".
this,
<!--more-->
Replace the part with a banner such as an advertisement.
I wanted to avoid using plugins, so I decided to write it in function.php below.
Replace more tag
Add the following content to functions.php.
<?php add_filter('the_content', 'adMoreReplace'); function adMoreReplace($contentData) { $adTags = <<< EOF //表示させたい内容をここに記載 EOF; $contentData = preg_replace('/<span id="more-[0-9]+"> <\/span>/', $adTags, $contentData); $contentData = str_replace('<p></p> ', '', $contentData); $contentData = str_replace('<p><br /> ', '<p> ', $contentData); return $contentData; } ?>
Write any code such as Google AdSense in the commented out part above.
When viewing on a smartphone, the number and types of ads displayed are changed.
Afterword
It takes courage every time I mess with functions.php, but I'm glad it turned out well.
Oh, of course, if you want to make any changes, not just functions.php, a backup is essential.
[Related]: [WordPress] How to conditionally branch and post banners such as advertisements for each category