iDevie
October 2019
M T W T F S S
« Sep    
 123456
78910111213
14151617181920
21222324252627
28293031  

Categories


Tutorial Details
Software: WordPress
Difficulty: Beginner
Completion Time: 1 hour 

Part 1
Part 2
Part 3
Part 4

In the first three parts of this series, you learned how to prepare static HTML for WordPress and to create a theme by splitting your HTML file into a set of template files and editing the stylesheet. You then uploaded your theme to WordPress and activated it.

The theme still isn’t displaying any content you add via the WordPress admin however; to do that you need to add a loop to your template files.

At the moment your theme only has one main template file – index.php – so you’ll add a loop to that.


What You’ll Need


1. Adding a Page in WordPress

I’m going to assume that you already know how to use the WordPress admin to add a page. I’m going to create a page called ‘Home‘, add some dummy content to it and then edit the ‘Reading Settings‘ in WordPress so that this is the home page, rather than the blog post listing.

I’ll use the content from my static site as the content for my new page, except I won’t add the first, full-width image to the content – you’ll learn how to add an image like this to your theme as a featured image in Part 10 of this series.

So go ahead and create a page, include some images if you like, and edit the ‘Reading Settings‘ page so your new page is the home page.


2. Adding a Loop

Having created your new page, visit your site’s home page again. You’ll find that nothing has changed – WordPress isn’t displaying the content of your page. That’s because you need to add a loop to tell it to do so. The loop will pull in the page content from the database and is what makes WordPress work.

Open your index.php file. After the opening of the .content div and before the opening

<article>

tag, add the following:

 <?php while ( have_posts() ) : the_post(); ?>

Now after the closing </article> tag, add:

 <?php endwhile; ?>

The first piece of code you added starts the loop. It checks if there is a post or page to display and then opens up the first post or page.

If you’re on an archive page, it loops through all the relevant posts, either the latest posts on the main blog page or the posts in a given category on a category page.

The second piece of code ends the loop so that WordPress can move on to displaying content such as the sidebar and footer.


3. Classes and IDs for the Article

The opening

<article>

tag can have classes and an ID which are automatically generated by WordPress. You can then use these at a later date to target CSS on that post or page if you wish to do so.

Find the opening

<article>

tag:

 <article class="post" id="01">

Edit it so it reads:

<article class="<?php post_class(); ?>" id="post-<?php the_ID(); ?>">

The two functions you’ve added are:

So you can use the ID to target a specific post with CSS, and the class to style all posts in a certain category the same way, for example.


4. Adding the Page or Post Title in the Loop

The next thing to display on your post or page is the title. In the existing code, this is a static title inside an

<h2>

 

tag. Find the line of code which reads:

 <h2 class="entry-title">This is the title of a post or page</h2>

Edit it so it now reads:

<h2 class="entry-title">
<a title="<?php printf( esc_attr__( 'Permalink to %s', 'compass' ), the_title_attribute( 'echo=0' ) ); ?>" href="<?php the_permalink(); ?>" rel="bookmark"> 	
<?php the_title(); ?> </a></h2>

This adds two things:


5. Adding Post Metadata

The first section element inside the loop is for post metadata – specifically the date and author of the post.

Find this line of code (or all of the code inside that first section element: it may be different in your theme):

 Posted on 5 November by Rachel McCollin

Replace it with:

 Posted on <?php the_date(); ?> by <?php the_author(); ?>

You’ve added two template tags:


6. Adding the Post Content

The most important thing is to make sure the post or page content is displayed and you do this using one simple template tag – the_content().

Find the section with the class .entry-content and delete its contents. Replace these with the the_content() tag so the whole section looks like this:

 <section class="entry-content"><?php the_content(); ?></section><!-- .entry-content -->

7. More Post Metadata

In my design, there is more post metadata after the post or page content. This is optional, but here I’ll use it to display a list of categories associated with the post. You might choose to miss this out of your theme depending on your design and your use of categories or tags.

Delete the contents of the final section with the .entry-meta class and replace them so the whole section reads:

 <section class="entry-meta"><?php if ( count( get_the_category() ) ) : ?> 	
<span class="cat-links"> 		
Categories: <?php echo get_the_category_list( ', ' ); ?> 	
</span> <?php endif; ?></section><!-- .entry-meta -->

 

It’s worth taking a moment to work through this code as it’s the longest snippet of PHP you’ve added so far.

Finally, save your index.php file, return to your browser and refresh the home screen. This should have changed slightly, as shown below:

creating-wordpress-theme-from-static-html-site-loop-added

As you can see, the following items are shown:

Note that the featured image isn’t displayed yet – you’ll fix that in Part 10 of this series.


Summary

Your theme is now well on the way to being a fully functioning theme. It has a loop in place to display content added via the WordPress admin as well as a set of template files. The next step is to edit the header.php file to include an essential action hook and some more template tags.

Download


Resources


Wptuts+


Comments 0
There are currently no comments.