Skip to main content

Ivan Teoh

Something personal yet public

WordPress Theme Development 2 - Minimum Main Index Template

Let's talk about index.php template file. This file is a must for WordPress theme. If any file is missing, WordPress will look for this main template file. Currently, I will use these WordPress themes below as references for learning WordPress theme.

The minimum index.php would be:

52-index-before.php (Source)

if (have_posts()) :
   while (have_posts()) :

But you might want to add something more in your main template file. In the posts loop, title of the post is added. It is good to mention the post is written by who and when. Categories and tags can be added in the posts loop too. Link pages, comments pop up link, comments template, next and previous posts link are also added in this main template file. Basically, this (as below) will be the default index.php for this theme.

52-index-after.php (Source)

 * @package WordPress
 * @subpackage Your_Theme
get_header(); ?>
<?php if (have_posts()) : ?>
    <?php while (have_posts()) : ?>
        <?php the_post(); ?>
        <div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
        <h2><a href="<?php the_permalink() ?>" rel="bookmark"
          title="Permanent Link to <?php the_title_attribute(); ?>">
          <?php the_title(); ?></a></h2>
        <?php the_content(__('Read more...')); ?>
        <p><?php wp_link_pages(); ?></p>
        <p><?php _e('Posted by'); ?> <?php the_author(); ?> @
          <?php the_time('F jS, Y'); ?> <?php _e('in'); ?>
          <?php the_category(','); ?> <?php the_tags(__('with '), ', '); ?></p>
        <p><?php comments_popup_link(__('No Comments &#187;'),
          __('1 Comment &#187;'), __('% Comments &#187;')); ?>
          <?php edit_post_link(__('Edit This'), '&#8212;'); ?></p>
        <?php comments_template(); ?>
    <?php endwhile; ?>
    <p><?php next_posts_link(__('&laquo; Older Posts')); ?></p>
    <p><?php previous_posts_link(__('Newer Posts &raquo;')); ?></p>
<?php else : ?>
<h2><?php _e('Not Found'); ?></h2>
<p><?php _e('Sorry, but you are looking for something that isn&#39;t here.'); ?>
<?php endif; ?>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

There are different between __( ) and _e( ). __( ) is used when the message is passed as an argument to another php function. Whereas _e( ) is used for writing the message directly to the page. More detail can be found in Translating WordPress.


Comments powered by Disqus