Title: Add script only in posts
Last modified: September 27, 2022

---

# Add script only in posts

 *  Resolved [paupau1](https://wordpress.org/support/users/paupau1/)
 * (@paupau1)
 * [3 years, 8 months ago](https://wordpress.org/support/topic/add-script-only-in-posts/)
 * Hi,
    I want to show a script only in post (articles) but when I add the script
   in the post tab, it also show the script in products pages (woocommerce). Do 
   you know how to avoid that ? I want the scriot to be show only in articles. Thank
   you for your time, Regards, PM
    -  This topic was modified 3 years, 8 months ago by [paupau1](https://wordpress.org/support/users/paupau1/).

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [Muhammad Arslan](https://wordpress.org/support/users/wparslan/)
 * (@wparslan)
 * [3 years, 8 months ago](https://wordpress.org/support/topic/add-script-only-in-posts/#post-16063783)
 * Hey [@paupau1](https://wordpress.org/support/users/paupau1/)
 * Sorry for responding late. Right now there’s no such option in our plugin.
    For
   this specific task, you can use WordPress’s default wp_head hook for this purpose.
 * I have also made a snippet for you so you won’t have to do anything but copy 
   and paste it into your child theme’s functions.php file.
 *     ```
       /**
        * Show certain scripts on certain post type/s.
        *
        * @return string The scripts for specific post types.
        */
       function hnf_custom_head_scripts() {
       	if ( 'post' === get_post_type( get_the_ID() ) ) {
       		echo '<script> //Your Code goes here </script>';
       	}
       }
       ```
   
 * Note: Do remember to change the post type according to your need.
 * Let me know if you need any assistance. 🙂
 * Have a nice day!
 *  Thread Starter [paupau1](https://wordpress.org/support/users/paupau1/)
 * (@paupau1)
 * [3 years, 7 months ago](https://wordpress.org/support/topic/add-script-only-in-posts/#post-16069927)
 * Hi,
 * Thank you for the reply, I’m not good at php. I add this script in the function.
   php of the child theme to test and it not show hello world in post, does I need
   to add the id of the posts our something ? The idea is to add it to every posts.
 *     ```
       /**
        * Show certain scripts on certain post type/s.
        *
        * @return string The scripts for specific post types.
        */
       function hnf_custom_head_scripts() {
       	if ( 'post' === get_post_type( get_the_ID() ) ) {
       		echo '<script> Hello world </script>';
       	}
       }
       ```
   
 * Thank you for your time,
    Regards, PM
 *  [Muhammad Arslan](https://wordpress.org/support/users/wparslan/)
 * (@wparslan)
 * [3 years, 7 months ago](https://wordpress.org/support/topic/add-script-only-in-posts/#post-16070299)
 * Hey [@paupau1](https://wordpress.org/support/users/paupau1/)
    You don’t need 
   to add any post id in the script. It might have been a cache issue on your site
   which is why you were not able to see the changes, I have added the above script
   to show a script only on posts and it worked fine.
 * Here I have made a short screencast for you as well: [ [Helping Screencast](https://d.pr/v/4fXvbb)].
 * If you are having problems in implementing the solution or facing any other problem/
   s kindly contact our [support forum](https://wpbrigade.com/contact/).
 * Have a good day.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Add script only in posts’ is closed to new replies.

 * ![](https://ps.w.org/wp-headers-and-footers/assets/icon-256x256.png?rev=2265239)
 * [Insert Headers And Footers](https://wordpress.org/plugins/wp-headers-and-footers/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/wp-headers-and-footers/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/wp-headers-and-footers/)
 * [Active Topics](https://wordpress.org/support/plugin/wp-headers-and-footers/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wp-headers-and-footers/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wp-headers-and-footers/reviews/)

 * 3 replies
 * 2 participants
 * Last reply from: [Muhammad Arslan](https://wordpress.org/support/users/wparslan/)
 * Last activity: [3 years, 7 months ago](https://wordpress.org/support/topic/add-script-only-in-posts/#post-16070299)
 * Status: resolved