• Hi there,

    I have javascript loading on my site before the </body>. The issue is that this is loading across all pages. How do I load this on specific pages only?

    Thanks
    A

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    How are you enqueuing the javascript?

    An expedient way to do this would be in a page template.
    You need to be using a child theme. Copy a basic page template from the parent theme into your child theme directory. Do your customisations there. Nominate this as the template to be used on your special pages.
    I am sure that there are more elegant ways to do this, but this will get you there.

    Moderator bcworkz

    (@bcworkz)

    Assuming you enqueue from the “wp_enqueue_scripts” action, you might be able to conditionally check against get_queried_object_id() returned value. This works well for single posts and pages. It also returns term object IDs for term archive requests, so there could be some ambiguity in some cases. If that’s a problem, get the entire queried object instead and check with is_a() as well. There will not be any value for the blog posts listing like the default home page.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How do I load Javascript on specific pages only?’ is closed to new replies.