Creating the index template

Return to your HTML template file, copy the code located between the two main content comment tags, and then paste it into the index.php template for your WordPress theme.

In order for WordPress to assemble your template files into a complete web page, you need to include template tags to call the other sections of the site. The first section that you need to call is the header. To do this, enter the following template tag at the beginning of index.php.

The sidebar.php and footer.php templates also need to be called. To do this, enter these template tags at the bottom of index.php.

<?php get_sidebar(); ?> <?php get_footer(); ?>

With those in place, it's now time to concentrate on adding The Loop to index.php. In WordPress, The Loop is responsible for displaying content on your website. Without it, your site would display the header, sidebar, and footer with nothing in between.

The Loop contains several lines of code that perform many different functions. Once again, each section will be added individually, so that you will have a clear understanding of the action that each piece of code performs.

Find the section of your site where your content normally appears. Then, place this code, which is responsible for beginning The Loop, just after the <div> tag that begins your content area.

The next template tag that you're about to place will be responsible for displaying a linked title to the content published on your site. So, locate the area where your content heading appears. It will, most likely, be surrounded by headings tags. Once you find it, remove the placeholder headline and then replace it with the following template tags. If the headline was surrounded by heading tags, then make sure that these template tags are encapsulated in them too.

<div id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></

Now you need to add code so that the area known as the post metadata section will be displayed. It's important that the postmeta data is added to your template because this section typically includes information on the author, category, post date, and comment count, as well as a link to edit the post if you're logged in. This information is displayed by placing the following code into your template.

Posted in <?php the_category(', ') ?> by <?php the_author() ?> on <?php the_time('F jS, Y') ?> | <?php edit_post_link(,Edit', '

| '); ?><?php comments_popup_link('No Comments', '1 Comment', '% Comments'); ?>

Next is the template tag responsible for displaying your page's content. This should be placed in the section that currently contains the placeholder content for your site. If this placeholder content is surrounded by <div> tags, or any other tags, then be sure to encapsulate this template tag in them as well so that the appropriate styles will be applied.

<?php the_content('Continue Reading...'; ?> Finally, enter the following line to end The Loop: <?php endwhile; ?>

The Loop may have ended but that doesn't mean that you're finished building the index.php template just yet. That's because a few more lines of code still need to be added for index.php to be complete.

If your blog is set to display a certain number of posts per page, then your visitors will need a way to navigate between those pages so that they can access all of your content. Add the following template tag to index.php and navigation links will appear on your index, category, archive, and search pages.

Should a visitor arrive at a post or page that doesn't exist (404 error), you will need a way for WordPress to handle the situation. You can also make things more convenient for visitors by adding a search box, so that they can attempt to locate the content they were hoping to find. This can be done with the following lines of code. Just add this code to index.php and it will act as a combination 404/search page should the need arise.

<?php else : ?> <h2>Not Found</h2>

<p><?php _e("Sorry, no posts or pages could be found. Why not search for what you were trying to find?"); ?></p> <?php get_search_form(); ?>

With that code in place, visitors who navigate to a non-existent page will now see a screen similar to the one shown in the following screenshot:

Not Found

Sorry, no posts or pages could be found. Why not search for what you were trying to find? Search for: | Search "|

At this point, you just need to add the next line of code to index.php and then this template will be completely finished.

Now, save index.php, so that you can begin work on your theme's single.php template.

Was this article helpful?

0 0
Creating And Writing Your Blog

Creating And Writing Your Blog

The success of a blog lies in the content. It is the content and the key words that consistently attract potential customers. Great blog content literally means something different on every blog. Great blog content is researched and not impulsive. You cannot treat the blog as an experimental playground and expect it to work for you. It is designed to mean business and make you presence felt online and you have to ensure it performs the way you want it to.

Get My Free Ebook


Post a comment