Then add the following code into header.php to pickup the custom field if it was a single post and the custom field was present.
< ?php if( is_single() and $singlePostJs = get_post_meta($post->ID, 'single-post-js', true) ) echo $singlePostJs; ?>
That’s it! It’s working!
The following jQuery code has been included for this post only. The reason for including the script tags inside the custom field and not in the PHP is that I can also use this field for CSS, but should probably create another custom field to manage that. Your choice.
Just do a quick check on other posts and pages without the custom field and check they are not affected by the new field. Hope this helped you!
Sam Deering has 15+ years of programming and website development experience. He was a website consultant at Console, ABC News, Flight Centre, Sapient Nitro, and the QLD Government and runs a tech blog with over 1 million views per month. Currently, Sam is the Founder of Crypto News, Australia.