WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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


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

share|improve this question

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' );
share|improve this answer
Gotcha - but this would be same for all pages. Do you know of any plugins that do this and allow user to put different html in on each page via normal edit functionality? – Ryan May 25 '12 at 13:56
To do anything on a per-post basis, you'll need to define custom post meta data, to be used in your script output. – Chip Bennett May 25 '12 at 14:03
up vote 0 down vote accepted

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); ?>  
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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