• Resolved paupau1

    (@paupau1)


    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, 6 months ago by paupau1.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Hey @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

    (@paupau1)

    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

    Hey @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 ].

    If you are having problems in implementing the solution or facing any other problem/s kindly contact our support forum.

    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.