Can you create a very basic WordPress theme, based on Bootstrap?

  1. Create a new folder in the wp-content/themes/ directory of your WordPress installation. Give this folder a unique name that describes your theme (e.g. my-bootstrap-theme).
  2. In this new folder, create a new file called style.css. This file is required for WordPress to recognize your theme. Add the following content to this file, replacing the information in brackets with your own information:
    /*
    Theme Name: [Theme Name]
    Theme URI: [Theme URL]
    Author: [Author Name]
    Author URI: [Author URL]
    Description: [Theme Description]
    Version: [Theme Version]
    License: [Theme License]
    License URI: [Theme License URL]
    Tags: [Theme Tags]
    */
    
  1. Next, create a new file called index.php in your theme folder. This is the template file that WordPress will use to display your homepage. You can copy the following code and paste it into this file:
    <?php get_header(); ?>
    
    <div class="container">
      <div class="row">
        <div class="col-md-8">
          <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
          <article>
            <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
            <p class="meta"><?php the_date(); ?> by <?php the_author(); ?></p>
            <?php the_excerpt(); ?>
          </article>
          <?php endwhile; else : ?>
          <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
          <?php endif; ?>
        </div>
        <div class="col-md-4">
          <?php get_sidebar(); ?>
        </div>
      </div>
    </div>
    
    <?php get_footer(); ?>

    This code sets up a container with two columns: the main content area on the left and a sidebar on the right. It also includes a loop to display the latest posts.

  1. Create a new file called single.php in your theme folder. This template file is used to display individual posts. You can copy the following code and paste it into this file:
    <?php get_header(); ?>
    
    <div class="container">
      <div class="row">
        <div class="col-md-8">
          <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
          <article>
            <h2><?php the_title(); ?></h2>
            <p class="meta"><?php the_date(); ?> by <?php the_author(); ?></p>
            <?php the_content(); ?>
          </article>
          <?php endwhile; endif; ?>
          <?php comments_template(); ?>
        </div>
        <div class="col-md-4">
          <?php get_sidebar(); ?>
        </div>
      </div>
    </div>
    
    <?php get_footer(); ?>

    This code sets up a container with two columns: the main content area on the left and a sidebar on the right. It also includes a loop to display the post content and comments.

  1. Create a new file called page.php in your theme folder. This template file is used to display individual pages. You can copy the following code and paste it into this file:
    <?php get_header(); ?>
    
    <div class="container">
      <div class="row">
        <div class="col-md-8">
          <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
          <article>
            <h2><?php the_title(); ?></h2>
            <?php the_content(); ?>
          </article>
          <?php endwhile; endif; ?>
          <?php comments_template(); ?>
        </div>
        <div class="col-md-4">
          <?php get_sidebar(); ?>
        </div>
      </div>
    </div>
    
    <?php get_footer(); ?>

    This code sets up a container with two columns: the main content area on the left and a sidebar on the right. It also includes a loop to display the page content and comments.

  1. Create a new file called header.php in your theme folder. This file contains the header section of your theme, including the site’s logo, navigation menu, and any other elements you want to include. You can copy the following code and paste it into this file:
    <!DOCTYPE html>
    <html <?php language_attributes(); ?>>
    <head>
      <meta charset="<?php bloginfo('charset'); ?>">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <title><?php wp_title(''); ?></title>
      <?php wp_head(); ?>
    </head>
    <body <?php body_class(); ?>>
      <div class="container">
        <header>
          <a href="<?php echo home_url(); ?>"><img src="<?php echo get_template_directory_uri(); ?>/img/logo.png" alt="Site Logo"></a>
          <?php wp_nav_menu(array('theme_location' => 'primary-menu')); ?>
        </header>
      </div>

    This code sets up the basic structure of the header section, including the site’s logo and navigation menu. It also includes the necessary WordPress functions for dynamic content.

  1. Create a new file called footer.php in your theme folder. This file contains the footer section of your theme, including any copyright information, site credits, and other elements you want to include. You can copy the following code and paste it into this file:
      <div class="container">
        <footer>
          <p>&copy; <?php echo date('Y'); ?> <?php bloginfo('name'); ?>. All rights reserved.</p>
          <p>Powered by <a href="http://wordpress.org/">WordPress</a>.</p>
        </footer>
      </div>
      <?php wp_footer(); ?>
    </body>
    </html>

    This code sets up the basic structure of the footer section, including any copyright information and site credits. It also includes the necessary WordPress functions for dynamic content.

  1. Create a new file called sidebar.php in your theme folder. This file contains the sidebar section of your theme, including any widgets or other elements you want to include. You can copy the following code and paste it into this file:
    <aside>
      <?php dynamic_sidebar('primary-sidebar'); ?>
    </aside>

    This code sets up the basic structure of the sidebar section and includes the necessary WordPress functions for dynamic content.

  1. Create a new file called functions.php in your theme folder. This file contains any custom functions or settings for your theme. You can copy the following code and paste it into this file:
    <?php
    
    function my_bootstrap_theme_setup() {
      register_nav_menus(array(
        'primary-menu' => __('Primary Menu', 'my-bootstrap-theme'),
      ));
      add_theme_support('post-thumbnails');
      add_image_size('featured-image', 800, 400, true);
      register_sidebar(array(
        'name' => __('Primary Sidebar', 'my-bootstrap-theme'),
        'id' => 'primary-sidebar',
        'description' => __('Widgets in this area will be shown on all posts and pages.', 'my-bootstrap-theme'),
        'before_widget' => '<section>',
        'after_widget' => '</section>',
        'before_title' => '<h3>',
        'after_title' => '</h3>',
      ));
    }
    add_action('after_setup_theme', 'my_bootstrap_theme_setup');
    
    function my_bootstrap_theme_scripts() {
      wp_enqueue_style('bootstrap', get_template_directory_uri() . '/css/bootstrap.min.css');
      wp_enqueue_style('my-style', get_template_directory_uri() . '/style.css');
      wp_enqueue_script('jquery');
      wp_enqueue_script('bootstrap', get_template_directory_uri() . '/js/bootstrap.min.js', array('jquery'), '4.0.0', true);
    }
    add_action('wp_enqueue_scripts', 'my_bootstrap_theme_scripts');
    
    ?>

    This code sets up the primary menu, post thumbnails, and sidebar widgets for your theme. It also loads the necessary CSS and JavaScript files for Bootstrap and your custom styles.

  1. Congratulations! You have now created a basic WordPress theme using Bootstrap. You can now activate your theme in the WordPress dashboard and start customizing it to your needs.

What is nav_walker does not exist in my theme?

If nav-walker does not exist in your WordPress theme, it means that your theme is not using a custom nav walker class to modify the output of wp_nav_menu().

In this case, you can create a custom nav walker class to modify the output of the menu.

  1. Open your functions.php file in a code editor.
  2. Add the following code to create a new nav walker class:
    class My_Custom_Nav_Walker extends Walker_Nav_Menu {
    
        // add classes to the UL element
        function start_lvl( &$output, $depth = 0, $args = array() ) {
            $indent = str_repeat( "\t", $depth );
            $output .= "\n$indent<ul class=\"navbar-nav ml-auto\">\n";
        }
    
        // add classes to the LI element
        function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
    		$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
    
    		$class_names = $value = '';
    
    		$classes = empty( $item->classes ) ? array() : (array) $item->classes;
    
    		$classes[] = 'nav-item';
    		$classes[] = 'nav-link';
    
    		$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
    		$class_names = ' class="' . esc_attr( $class_names ) . '"';
    
    		$output .= $indent . '<li' . $class_names . '>';
    
    		// add span tag to menu item content
    		$attributes  = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) . '"' : '';
    		$attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) . '"' : '';
    		$attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) . '"' : '';
    		$attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) . '"' : '';
    
    		$item_output = $args->before;
    		$item_output .= '<a class="nav-link px-lg-3 py-3 py-lg-4"' . $attributes . '>';
    		$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
    		$item_output .= '</a>';
    		$item_output .= $args->after;
    
    		$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
    	}
    }
  1. Save the functions.php file.
  2. Modify the wp_nav_menu() function in your theme to use the custom nav walker class. For example, you can use the following code:
    wp_nav_menu( array(
        'theme_location' => 'primary',
        'depth'          => 2,
        'container'      => false,
        'menu_class'     => 'navbar-nav ml-auto',
        'fallback_cb'    => 'WP_Bootstrap_Navwalker::fallback',
        'walker'         => new My_Custom_Nav_Walker(),
    ) );
    

In this example, the 'walker' argument is set to the My_Custom_Nav_Walker() class, which will modify the output of the menu. The 'menu_class' argument adds a class to the <ul> element of the menu. You can modify the arguments as needed for your theme.

Let's connect

Get in touch if you have a new project or just want to say hi.

© by Torresian Crow Consulting Pty Ltd.