Table of Contents
Step 1: Download Bootstrap
The first step is to download Bootstrap’s latest version from the official website at https://getbootstrap.com/. You can choose to download the complete package, which includes both CSS and JavaScript files, or you can download just the CSS or JavaScript files if you prefer.
Step 2: Add Bootstrap Files to Your Theme
Once you’ve downloaded Bootstrap, you’ll need to add the necessary files to your WordPress custom theme. Create a new folder in your theme directory and name it “bootstrap”. Then, copy the downloaded CSS and JavaScript files into this folder.
Step 3: Enqueue Bootstrap Scripts and Styles
function mytheme_enqueue_scripts() {
wp_enqueue_style( 'bootstrap-css', get_template_directory_uri() . '/bootstrap/css/bootstrap.min.css' );
wp_enqueue_script( 'bootstrap-js', get_template_directory_uri() . '/bootstrap/js/bootstrap.min.js', array( 'jquery' ), '5.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'mytheme_enqueue_scripts' );
Note that these functions should be added to your WordPress theme’s functions.php
file.
This code adds the Bootstrap CSS and JavaScript files to your theme and ensures that they’re loaded in the correct order. It also includes jQuery as a dependency for the Bootstrap JavaScript file.
Step 4: Use Bootstrap Classes in Your Theme
Once you’ve enqueued the Bootstrap files in your WordPress custom theme, you can start using Bootstrap classes in your HTML markup. For example, you can use the “container” class to create a responsive container for your content:
Hello, World!
This is my WordPress custom theme using Bootstrap.
In addition to using Bootstrap classes in your HTML markup, you can also add them to native WordPress PHP functions like wp_nav_menu()
, the_content()
, and the_post_thumbnail()
. To do this, you can use the $args
parameter to add the classes to the output.
For example, to add the “nav-item” class to the menu items in your header navigation, you can use the following code:
wp_nav_menu( array(
'menu_class' => 'navbar-nav',
'container_class' => 'collapse navbar-collapse',
'container_id' => 'navbarNavDropdown',
'menu_id' => 'main-menu',
'walker' => new Bootstrap_Nav_Walker(),
'items_wrap' => ' %3$s
',
'depth' => 3,
'add_li_class' => 'nav-item',
) );
Step 5: Add jQuery Code for Bootstrap Menu
jQuery(function($) {
$(document).ready(function() {
$('.navbar-nav li.dropdown').hover(function() {
$(this).find('.dropdown-menu').first().stop(true, true).delay(250).slideDown();
}, function() {
$(this).find('.dropdown-menu').first().stop(true, true).delay(100).slideUp();
});
$('.navbar-nav li.dropdown > a').click(function() {
location.href = this.href;
});
});
});
Step 6: Add Rows and Columns within the Header and Pages
To add rows and columns within the header and pages, you can use Bootstrap’s grid system. Here’s an example of how to create a row with two columns in your header:
No Comments yet!