[WordPress] How to conditionally branch and post banners such as advertisements for each category

[WordPress] How to not create author/poster archives

I wrote an article like this before.

[WordPress] How to replace the more tag in an article with an advertisement banner

I had implemented it as mentioned in that article, but I thought it would be better to conditionally branch by category and post a banner that matches that category, so I tried it out.

Below is the memo.

How to conditionally branch and post banners such as advertisements for each category

The following is an example of replacing the more tag in an article.
Write it in functions.php.

add_filter('the_content', 'adMoreReplace'); function adMoreReplace($contentData) { $_categories = get_the_category(); foreach ($_categories as $_category) { if($_category->cat_name == "Category A"){ $ adTags = <<< EOF //Enter the content you want to display in category A here EOF; } else if($_category->cat_name == "Category B"){ $adTags = <<< EOF //In category B Enter the content you want to display here EOF; } else{ $adTags = <<< EOF //Enter the content you want to display in other categories here EOF; } break; } $contentData = preg_replace('/ <span id="more-[0-9]+"><\/ span>/', $adTags, $contentData); $contentData = str_replace('<p></p> ', '', $contentData); $contentData = str_replace(' <p><br />', '</p> <p>', $contentData); return $contentData; }</p></span>

Make a conditional branch as shown above and write any code such as Google AdSense in the commented out part.

Afterword

I'm glad that it worked as intended, but if there are multiple registered categories, I'm having trouble deciding which one to prioritize.

I think I would like to write another article about that.

  • Add this entry to Hatena Bookmarks

At "naouniverse.com", the administrator writes about things, things, and the world that interest him.
Gadgets, cameras, design, programming, L'Arc~en~Ciel, etc...