I’ve been doing some affiliate marketing on one of the sites I manage which required some ad code to be inserted mid-post

I also needed to be able to insert this ad code on a specific tree or branch of pages, e.g. sub-pages and their immediate parent (ancestors). Luckily CSS Tricks has a useful function to work this out which can be reused elsewhere in your theme.

The next stage was to work out how to actually insert my ad code (using a premium plugin called WP Pro Advertising) in the middle of a post or a page. To do this WPSE gave me the following function

 

 

 

Save

Save