Forums

Plugin to Add Custom Javascript to an individual page? (2 posts)

  1. wflanagan
    Member
    Posted 2 years ago #

    Hey all,

    I'm looking to a bit of javascript to certain pages on my blog. I don't want it on all pages (like Google Anaytics). I only want it on certain pages (only on my review pages).

    I haven't found a plugin that simply inserts code into a page, individually configurable by page.

    Can anyone point me in the right direction?

    Thanks!

    William

  2. stvwlf
    Member
    Posted 2 years ago #

    Hi - You can do it without a plugin using a few lines of PHP code in your theme's header.php file

    I don't know how you distinguish your review pages from other pages. If reviews is a category with a category ID of 16, and you want the javascript displayed only on that category page, and you declare your javascript in the HEAD section, you can put this in the HEAD section:

    <?php if ( is_category('16') ) { ?>
    <script type="text/javascript">
    ... script here ...
    </script>
    <?php } >

    The if statement can be extended to include more than one category. PHP will only include that javascript on the webpage when the if statement condition is true. There are ways to make it more elegant, but this will work, and get you going.

Topic Closed

This topic has been closed to new replies.

About this Topic