I want to load some JavaScript code only when it is posts, and not a home page or pagination! How can I do it with functions.php?


In the active theme, functions.php file or in your plugin,

function load_scripts() {
    if ( is_singular( 'post' ) ) {
        // Load the Javascript
        wp_enqueue_script( string $handle, string $src = '', array $deps = array(), string|bool|null $ver = false, bool $in_footer = false );
add_action( 'wp_enqueue_scripts', 'load_scripts' );
  • thanks but how cn i load my own code? Is 'load_scripts' a file? – Interessante Fakten Jan 8 at 9:10
  • wp_enqueue_script( 'unique-name', 'path/to/your/javascript/file', array( 'jquery' ), '1.0.0', true ); this will load the JS file also add jquery support [if not need to remove the jquery from array] and will appear in footer – Debabrata Karfa Jan 8 at 9:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.