3

I'm developing a custom WordPress theme from scratch and now I faced a problem with loading css and js files. I have searched everywhere to see how can I be able to load those files and I did this on my functions.php file:

    <?php 
function catalog(){
    wp_enqueue_style('bootstrap', get_template_directory_uri() . 'css/bootstrap.min.css');
    wp_enqueue_style('style', get_stylesheet_uri());
    wp_register_script( 'custom-script', get_template_directory_uri() . '/js/bootstrap.min.js', array( 'jquery' ) );
    wp_register_script( 'custom-script', get_template_directory_uri() . '/js/imagesloaded.pkgd.min.js', array( 'jquery' ) );
    wp_register_script( 'custom-script', get_template_directory_uri() . '/js/jquery.js', array( 'jquery' ) );
    wp_register_script( 'custom-script', get_template_directory_uri() . '/js/masonry.pkgd.min.js', array( 'jquery' ) );
    wp_register_script( 'custom-script', get_template_directory_uri() . '/js/offcanvas.js', array( 'jquery' ) );
    wp_enqueue_style('style', get_stylesheet_uri());
}
add_action('wp_enqueue_scripts','catalog');
register_nav_menus(array(
    'primary' => __('Primary Menu'),
    'footer' => __('Footer Menu'),
));
?>

So I don't know whats really going wrong here cause I've checked everything and it looks fine. Here's my folder structure:

theme_folder
            css
                  bootstrap.min.css
            images
            js
                  bootstrap.min.js
                  imagesloaded.pkgd.min.js
                  jquery.js
                  masonry.pkgd.min.js
                  offcanvas.js

But after all this I get this as result which means they have not beed loaded:

print screen

1
  • Correct me if I am wrong as its been quite sometime sense I worked with WordPress theme's wouldn't you want to use wp_register_script then run wp_enqueue_script? Again this is only off the top of my head, I could be totally wrong. Commented Dec 26, 2016 at 7:20

2 Answers 2

2

Problem You are register a script but not enqueue.

Solution: If you are register then you have to enqueue. wp_enqueue_style() - For css enqueue wp_enqueue_script()- For JS enqueue

Updated code

 <?php 
function catalog(){
    wp_enqueue_style('bootstrap', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css');
    wp_enqueue_style('style', get_stylesheet_uri());
    wp_enqueue_script( 'custom-script-bootstrap', get_template_directory_uri() . '/js/bootstrap.min.js', array( 'jquery' ) );
    wp_enqueue_script( 'custom-script-imgesload', get_template_directory_uri() . '/js/imagesloaded.pkgd.min.js', array( 'jquery' ) );
    wp_enqueue_script( 'custom-script-jquery', get_template_directory_uri() . '/js/jquery.js', array( 'jquery' ) );
    wp_enqueue_script( 'custom-script-masonry', get_template_directory_uri() . '/js/masonry.pkgd.min.js', array( 'jquery' ) );
    wp_enqueue_script( 'custom-script-offcanvas', get_template_directory_uri() . '/js/offcanvas.js', array( 'jquery' ) );
}
add_action('wp_enqueue_scripts','catalog');
register_nav_menus(array(
    'primary' => __('Primary Menu'),
    'footer' => __('Footer Menu'),
));
?>

Recommendation

Always Prefer CDN for css and js like jquery,bootstrap etc.

Sign up to request clarification or add additional context in comments.

6 Comments

You should also use different names, not the same custom-script in all wp_enqueue_script() calls.
Yes why not it more preferable
Else only the last javascript file with that name will be enqueued
please check again.
don't remove that style.css.This is theme not plugin.
|
0

Try to something like this.

add css file in your template like below...

<link rel="stylesheet" href="<?php bloginfo('template_directory');?>/css/bootstrap.min.css">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.