[Resolved] Adding JavaScript on specific posts only.

Home Forums Support [Resolved] Adding JavaScript on specific posts only.

Home Forums Support Adding JavaScript on specific posts only.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #630156
    Jay

    My previous theme had an area called “Scripts” below the editor on every post or page that would allow me to add JavaScript or whatever else into the header or before the closing body tag for that specific post/page only rather than have it load site-wide.

    Is there a way to do something like this in GeneratePress, preferably without using a plugin?

    #630195
    Jay

    Just did some more searching around and found Tom’s post here: https://generatepress.com/forums/topic/using-custom-javascript-in-a-page/#post-186101

    I think this might work perfectly for what I need. My only questions would be…

    1. This was posted a couple of years ago. Is it still the way to do it?
    2. I’d want to do this for posts rather than pages. So instead of “is_page” would it need to be “is_post” or “is_single”?
    3. To add the same script to multiple pages, would I just need to use commas? For example: <?php if ( is_post( ‘100, 101, 102’ ) ) : ?>

    #630206
    Leo
    Staff
    Customer Support

    Hi there,

    1. Yes 🙂

    2. & 3. This should help: https://codex.wordpress.org/Conditional_Tags#A_Single_Post_Page

    I should also mention that 2 & 3 won’t be needed once GPP 1.7 comes out. It’s currently in beta 3 already so pretty stable I think. It might save you some work if you want to try it out:
    https://docs.generatepress.com/article/beta-testing/

    #630217
    Jay

    Just got the beta version and you are correct. This is fantastic!

    Thank you!

    #630268
    Leo
    Staff
    Customer Support

    No problem!

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.