Support » Fixing WordPress » Add specific javascript code to a wordpress page

  • Hi,

    I have found some code I want to use to a single page in my wordpress site, but I cannot find the way to do it. I am not a developer, but a newbie.

    I have html and css ready, but I don’t know how to run this javascript code to a page with id 2922.

    $(function () {
        var parent = $("#shuffle");
        var divs = parent.children();
        while (divs.length) {
            parent.append(divs.splice(Math.floor(Math.random() * divs.length), 1)[0]);
        }
    });

    Thanks for your help,
    Thodoris

Viewing 1 replies (of 1 total)
  • Moderator Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Use this plugin to hold your JS modifications: https://wordpress.org/plugins/custom-css-js/

    If you have a decent theme, it will spit out classes in the <body> element. One class should be unique to the page, e.g. ‘page-id-2922’.

    So you can wrap a condition around your code to check if it’s that page…

    function nameThisProperly() {
        var parent = $("#shuffle");
        var divs = parent.children();
        while (divs.length) {
            parent.append(divs.splice(Math.floor(Math.random() * divs.length), 1)[0]);
        }
    }
    
    if ($('body').hasClass('page-id-2922')) {
        nameThisProperly();
    }
Viewing 1 replies (of 1 total)
  • The topic ‘Add specific javascript code to a wordpress page’ is closed to new replies.