Output HTML only on individual post view

I am looking for a way to output some html (javascript actually) on a post view only and not on a main page, tag, category view (where WP outputs several posts).

E.g it would be output on this page

Read More

blog.yourdomain.com/2012/05/some-post/

But not on
blog.yourdomain.com/
blog.yourdomain.com/category/xxx/
blog.yourdomain.com/tag/yyy/
etc.

The HTML to be output is different for each post

Related posts

Leave a Reply

2 comments

  1. If you want to load your js only on page/post view you can do it like this:

    function myplugin_enqueue_scripts() {
        global $wp_query;
        if ( $wp_query->is_single ) {
            wp_enqueue_script( 'my_js', ... );
        }
    }
    add_action( 'wp_enqueue_scripts', 'myplugin_enqueue_scripts' );
    
  2. Eugene’s answer was a little over complex for my needs so here’s what I did (though +1 to Eugene as it got me looking in the right places)

    I first added a custom field called singlePostHTML to the posts that I wanted to add script to – put the script in the value.

    WordPress – Using Custom Fields

    I then edited single.php (as this template is used only when you’re only displaying a single post, rather than multiple posts on home page or archive/category/search views).

    WordPress – template hierarchy

    And added the following

    <?php echo get_post_meta($post->ID, 'singlePostHTML',true); ?>