Skip to main content
All CollectionsAdvanced
Add a single-post class to your individual blog posts
Add a single-post class to your individual blog posts

In rare instances, it is necessary to add some code to your site to make sure our ads appear correctly.

Eric Hochberger avatar
Written by Eric Hochberger
Updated over 2 years ago

At Mediavine, we rely on your HTML being well formatted in order to figure out where to insert our ads.

For the most part, Wordpress does a pretty fantastic job of creating some easy-to-read HTML. Unfortunately, when a blog features posts on their homepage (as many do), those are marked up the exact same way as the individual article pages, even if you’re running the posts in “excerpt” mode on the homepage.

Our problem is this can often “trip up” our script wrapper and results in post ads appearing on the homepage. The fix? Let’s mark your individual posts with a special class that will differentiate them from your homepage and let us target them.

So how do you add a custom post class to all individual / single posts? 

Look no further than the incredibly powerful Wordpress filters. 

Warning, if this is scary, please consult a Wordpress programmer! Also, Genesis and many other frameworks do this by default, so only do this if we tell you!

  1. Edit your (child if you have one) theme's functions.php. You should be able to find this file under Appearance > Editor by choosing functions.php in the right side.

  2. Anywhere inside your functions.php, you just need to add this code.

function mediavine_add_singlepost_class( $classes ) { 
  if ( is_single() ) { array_push( $classes, 'single-post' ); }
  return $classes;
}
add_filter( 'post_class', 'mediavine_add_singlepost_class' );


Did this answer your question?