March 31, 2015

The most basic Template, ever!

Because I’m not a robot, and I have varied interests outside of WordPress development, I’m constantly looking up even the most basic starting points for my custom theme development.

So, here is a set of snippets of the most basic custom theme code. No HTML and no CSS… this will just put content on the page, and enable custom menus and dynamic side bars.

<?php
/**
 * Template Name: Default Template
 *
 * This is the most generic template file in a WordPress theme
 *
 * @package PACKAGE-NAME
 * @subpackage SUBPACKAGE-NAME
 * @since 2015
 */
?>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
  <!-- Meta -->
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  
  <title>Page Title</title>
  
  <!-- CSS -->
  <?php $dir = get_template_directory_uri(); ?>
  <link href="<?php echo $dir.'/style.css' ?>" rel="stylesheet" type="text/css" />

<!-- YOU NEED wp_head(); TO ALLOW YOUR THEME TO WORK WITH MOST PLUGINS -->
<?php wp_head(); ?>
</head>

<body>

<!-- CUSTOMIZABLE MENU SETUP -->
  <?php 
   $args = array(
    'container_class' => 'main-nav',
    'theme_location' => 'primary-menu',
        'depth'       => 0,
    'sort_column' => 'menu_order, post_title',
    'menu_class'  => 'nav',
    'include'     => '',
    'exclude'     => '2',
    'echo'        => true,
    'show_home'   => false,
    'link_before' => '',
    'link_after'  => '' 
   );
  ?>

<!-- PUT YOUR MENU CODE HERE -->
<?php wp_nav_menu( $args ); ?>

<!-- THE LOOP -->
<?php if ( have_posts() ) : ?>
    <?php
      // Start the loop.
      while ( have_posts() ) : the_post(); ?>

<!-- PUT YOUR CONTENT SPECIFIC CODE HERE -->
<?php get_template_part( 'content', get_post_format() ); ?>

<h1><?php the_title(); ?></h1>
<?php the_content(); ?>

<?php // End the loop.
      endwhile;

// Previous/next page navigation.
      the_posts_pagination( array(
        'prev_text'          => __( 'Previous page', 'themename' ),
        'next_text'          => __( 'Next page', 'themename' ),
        'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'themename' ) . ' </span>',
      ) );

    // If no content, include the "No posts found" template.
    else :
      get_template_part( 'content', 'none' );

    endif;
    ?>

<!--SIDEBAR CODE WILL SUPPORT WIDGETS-->
<?php if ( is_active_sidebar( 'sidebar-1' ) ) : ?>
	<?php dynamic_sidebar( 'sidebar-1' ); ?>
<?php endif; ?>


<!-- YOU NEED THIS TO SUPPORT THE ADMIN BAR AT THE TOP WHEN YOU'RE LOGGED IN -->
<?php wp_footer(); ?>
</body>

</html>
index.php
<?php 
// REGISTER NEW MENUS
function register_my_menus() {
    register_nav_menus(
        array(
            'primary-menu' => __( 'menu-top' ),
	)
    );
}
add_action( 'init', 'register_my_menus' );

// REGISTER SIDE BARS
register_sidebars();
?>
functions.php