I found some code online to create content blocks on my WordPress blog. I use a content block at the bottom of the sidebar of my page for a shopping widget. Here's the relevant code from functions.php:

function initialize_content_blocks()
  register_post_type('content-block', array(
    'labels' => array(
      'name' => 'Content Blocks',
      'singular_name' => 'Content Block',
      'add_new_item' => 'Add New Content Block',
      'edit_item' => 'Edit Content Block',
      'new_item' => 'New Content Block',
      'view_item' => 'View Content Block',
      'search_items' => 'Search Content Blocks',
      'not_found' => 'No content_blocks found',
      'not_found_in_trash' => 'No content blocks found in Trash',
      'view' => 'View Content Block'
    'publicly_queryable' => false, 
    'exclude_from_search' => true,
    'public' => true,
    'rewrite' => false, 
    'supports' => array('title', 'editor'),
    'taxonomies' => array()

I added some other code today to attempt to add an advertisement after paragraph two of every post on mobile. Here's that section on functions.php:

if ( $detect->isMobile() ) {
add_filter('the_content', 'wpse_ad_content');

function wpse_ad_content($content)
    if (!is_single) return $content;
    $paragraphAfter = 2; //Enter number of paragraphs to display ad after.
    $content = explode("</p>", $content);
    $new_content = '';
    for ($i = 0; $i < count($content); $i++) {
        if ($i == $paragraphAfter) {
            $new_content.= '<div class="content-ad">';
            $new_content.= '<a href="http://tarynwilliford.com/collaborate/">Advertisement</a>';
            $new_content.= '<script type="text/javascript" src="http://ap.lijit.com/www/delivery/fpi.js?z=287429&u=tarynwilliford&width=300&height=250"></script>';
            $new_content.= '</div>';

        $new_content.= $content[$i] . "</p>";

    return $new_content;

The ad function above works great, but it also inserts my ad after paragraph 2 of the shopping content block in the sidebar. How can I modify the code to target single posts, but exclude content blocks?

I've tried this:

    if (!is_single() || is_singular( 'Content Block' ))  return $content;

(I'm learning, but what I think this means: If this !IS NOT a post OR if it IS a 'Content Block', return the content as-is without the ad)

And this:

    if (!is_single() || 'Content Block' == get_post_type())  return $content;

But neither works.

Could somebody please point me in the direction of what I'm doing wrong? Is it the singular name 'Content Block'? Maybe I'm not reading the code right in my layman's translation above?


Side note: In my troubleshooting, I tried to target everything but the content blocks on purpose by changing the line above to read if (is_singular( 'Content Block' )) return $content; , but that puts the ad on everything, including posts, pages and content blocks. So I'm guessing is_singular ( 'Content Block') isn't doing the trick.

Please help!

1 Answer 1


You want the first parameter ($post_type) of register_post_type() – by default this is used for the "slug", so in your case it's:


The name and singular-name labels you refer to are just for display purposes – see get_post_type_object() for an example.

So this should work (note I've not tested the rest of your advert code):

if (! is_single() || 'content-block' == get_post_type() ) {
  return $content;

NB: The slug in the URL doesn't have to be $post_type, you can change it by specifying the rewrite argument (see the register_post_type() docs again for an example.)

  • Just wanted to confirm--that worked great. Thank you so much! Apr 21, 2015 at 21:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.