Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development

  • Print
  • + Share This
This chapter is from the book

The Next Half Hour

The theme is still quite bare but that’s OK—we’re going to add to it right now. By default, all WordPress installs start with one post, one category, one page, and one comment. It makes sense at this point to go in and add a few extra pages, posts, and other content to make it easier to test the theme’s functionality.

Now that we have some content to work with, let’s identify some WordPress theme basics. A typical website, WordPress–powered or not, will have branding, navigation, and site content, and all of these will be written in HTML.

If you head over to http://wdgwp.com/downloads you can download a very simple HTML file that has some basic markup and content we can use to create our theme. Copy and paste this file’s contents into the index.php.

There’s no need to reactivate the theme each time you change the template files. Refreshing the page will show your changes. Now that you’ve uploaded the index.php, let’s visit the front of the site. You’ll see the content in place (remember, it won’t be pretty just yet). Technically, we have a working web page at this point, but we still haven’t made anything dynamic.

The file has elements you’re familiar with: title, navigation list, headings, and text within HTML tags. Next we’ll replace the content, such as “Jesse Friedman | Developer,” with dynamic PHP calls.

So, the document title “Jesse Friedman | Developer” will be replaced with a call to display the site title and description. Again, for now this is all placeholder text. Once you replace this content with dynamic calls, the content will automatically be replaced with content from your WordPress installation.

The navigation list will be powered by a simple menu, which we’ll define shortly. After that you’ll see several posts displaying only the title and content with a link to the full article, all of which will be replaced by the infamous WordPress Loop.

Now that we’ve defined the content, we can get to work replacing it all with dynamic calls. Let’s start at the top of document in the <head> section and work our way down. In the head we have to make some minor tweaks. The <title> tag defines the title of the page you’re currently viewing in the browser window.

<title>

The HTML we copied from the supplied HTML document currently looks like this:

<title>Jesse Friedman | Developer</title>

The first thing to realize about converting static content to WordPress calls is that we’re simply calling PHP functions that will be replaced by content. It’s easy to learn WordPress calls without really having a full understanding of PHP. This is why it’s easy for web designers to build WordPress themes without really knowing that they’re writing PHP.

To make the title dynamic we’ll delete the content between the <title> tags and replace it with the bloginfo() function.

<title><?php bloginfo(); ?></title>

Any and all PHP functions, scripts, or code in general must start and end with <?php ?>. In some cases you can get away without the closing ?> but for now let’s keep it in place. The bloginfo() function requires a parameter so it knows what you’re asking for. Once it receives that parameter it will “echo” it. Below is a list of parameters that bloginfo() accepts and what they will return:

name = Site Title
description = Site Description
admin_email = admin@example.com

url = http://example/home (however you should use home_url('/') function
        instead)
wpurl = http://example/home/wp (however you should use site_url('/') function
        instead)

stylesheet_directory = location of theme files in wp-content
stylesheet_url = http://example/home/wp/wp-content/themes/child-theme/
        style.css
template_directory = http://example/home/wp/wp-content/themes/parent-theme
template_url = http://example/home/wp/wp-content/themes/parent-theme

atom_url = http://example/home/feed/atom
rss2_url = http://example/home/feed
rss_url = http://example/home/feed/rss
pingback_url = http://example/home/wp/xmlrpc.php
rdf_url = http://example/home/feed/rdf

comments_atom_url = http://example/home/comments/feed/atom
comments_rss2_url = http://example/home/comments/feed

charset = UTF-8
html_type = text/html
language = en-US
text_direction = ltr
version = 3.1

Now all we have to do is enter the parameter and we’ll be done. The parameter goes between the parentheses in single quotes.

<title><?php bloginfo( 'name' ); ?></title>

The above code displays the site name set in the General Settings section of the WordPress admin.

Let’s hop over to the front end of the site and refresh it. The title in the browser window will now mirror what you’ve entered into the settings.

<style>

The next thing to do is link the stylesheet in the header. Since the theme can be used on any domain, don’t try to link to the stylesheet through an absolute link. Instead, replace its location with a WordPress call like we did above. Currently, the style.css call looks like this:

<link rel="stylesheet" type="text/css" href="style.css" >

All you need to do is replace the text in the href=“ ” with the call to the stylesheet. Since every theme requires a style.css file, you can link to it directly using <?php bloginfo( ‘stylesheet_url’ ); ?>. If you want to link to additional stylesheets, JavaScript, or other files in the theme, you can use <?php bloginfo( ‘stylesheet_directory’ ); ?> followed by the location and name of the file in the theme:

<link rel="stylesheet" type="text/css" href="<?php bloginfo( 'stylesheet_url'

       ); ?>" >

<header>

In the body there are a few elements that need replacing. Again, this results in a very simple theme, so we don’t have sidebars or even a footer in this example. A quick glance at the static content shows that we have to replace the site name in the <header> → <h1> along with the <nav> with a dynamic menu. Following that we have the ten most recent posts, each in its own <article>.

Below is the static content used in the <header>:

<header>
  <h1>Jesse Friedman | Developer</h1>
  <nav>
    <ul>
      <li><a href="">Home</a></li>
      <li><a href="">About Us</a></li>
      <li><a href="">Services</a></li>
      <li><a href="">Portfolio</a></li>
      <li><a href="">Contact Us</a></li>
    </ul>
  </nav>
</header>

Let’s start by replacing the text within the <h1> with dynamic calls as we did above. The first half uses the ‘name’ parameter and the second half of the <h1> is replaced with the site ‘description.’ At this point you should be getting used to replacing HTML static content with dynamic calls. It’s a very straightforward process—don’t let it scare you.

<header>
  <h1><?php bloginfo( 'name' ); ?> | <?php bloginfo( 'description' ); ?></h1>
  <nav>
    <ul>
      <li><a href="">Home</a></li>
      <li><a href="">About Us</a></li>
      <li><a href="">Services</a></li>
      <li><a href="">Portfolio</a></li>
      <li><a href="">Contact Us</a></li>
    </ul>
  </nav>
</header>

Next, we’ll call a menu by its name to replace the list of navigational items. There are lots of parameters you can use to customize this section, but for this example let’s keep it simple.

Menus

The one caveat with menus is that you have to turn them on. To do this, we’ll have to deviate from our index.php file and create a functions.php file. The functions.php file lives in the theme in the same directory as the index.php and style.css. This is important: As with many template files, they must reside in the main theme directory.

Put the code below in your functions.php file.

<?php register_nav_menus(); ?>

Once you’ve implemented this code you’ll see menus in the Appearance section in the admin. If you haven’t already, go into menus and create a new navigation menu. Call it “Main Nav,” add some pages to it, and save it.

Now we’ll replace the <ul> with a function to call the menu by name. Later in the book we’ll look at what this means in greater detail, but for now, just know that we’re calling wp_nav_menu() function and passing it an array of parameters, in this case, the menu name.

<header>
  <h1><?php bloginfo( 'name' ); ?> | <?php bloginfo( 'description' );?> </h1>
  <nav>
    <ul>
      <?php wp_nav_menu( array( 'menu' => 'Main Nav' ) ); ?>
    </ul>
  </nav>
</header>

The above code replaces the static <header> content with dynamic content. Go into General Settings and Menus, change the content, rearrange some nav items, and get used to seeing the content change dynamically.

The Loop

The Loop is one of the more complex elements to learn, but fear not—we’ll cover it in detail now. If you take a look at the static content, you can see that the same structure is repeated over and over: opening tags, title, content, closing tags. In other words, the HTML tags for each post are exactly the same, the only difference is the content.

<article>
  <h2><a href="" title=""><!-- title --></a></h2>
  <p><!-- content --></p>
</article>

So, instead of a repetitive list of umpteen posts with the same structure, our template will have one loop that presents the content dynamically.

The Loop is widely known among WordPress developers as the engine behind WordPress blogs (http://wdgwp.com/loop). It runs through the markup structure, template tags, and PHP code for every available post (based on your location in a site) and formats and displays them. Any HTML or PHP placed inside the loop is repeated instead of duplicated (included) as many times as the loop runs. In most places, The Loop lists up to ten posts, but this can be changed in the reading settings or in a more advanced solution right in The Loop (we’ll discuss this further later on).

For now, think of The Loop as a PHP while loop (which it is) that calls functions along the way. If you’re on the home page, it will display the ten most recent posts in the blog. If you’re on a category page, it will simply display the ten most recent posts from that category. While what is being displayed changes, The Loop itself does not, because the visitor’s location, or better yet the URL, dictates what will be shown.

Here’s a look at a basic WordPress loop:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
  <!-- content here -->
<?php endwhile; else: ?>
      <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

Let’s break this down:

<?php if( have_posts() ) is utilizing a simple PHP if statement to test whether the have_posts() function will return a value. If it does, then we know we have posts and we move on.

: while( have_posts() ) initiates the PHP while loop using the number returned by the have_posts() function. So if the have_posts() function returns the number 10, then the loop will run ten times. Finally, we call the the_post() function, which retrieves the post data and other things.

The PHP while loop loops all the content and calls all the functions we place inside it. After that we end the loop with <?php endwhile; then call the else: ?> statement, which gives us an opportunity to do something if we don’t have any posts to display.

In this case, the else statement simply echoes, “Sorry, no posts matched your criteria.”

Now that we’ve broken down The Loop, let’s put it back together. We’ll start by replacing <!-- content here --> with static HTML content as seen below:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
  <article>
    <h2><a href="<?php the_permalink(); ?>" title="">This is an Article
        Title</a></h2>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi nulla
        nisi, adipiscing eu laoreet vitae, venenatis vitae velit. Phasellus
        euismod dapibus velit in laoreet. Vivamus ornare justo vehicula felis
        scelerisque non aliquam nisl semper. Curabitur nisl mauris, posuere
        sed imperdiet vel, cursus id dolor. Suspendisse varius consequat
        lorem ac luctus. Maecenas consectetur neque at turpis elementum vitae
        eleifend sem blandit. Nullam auctor, risus nec porta lacinia, ante
        sapien bibendum massa, a semper tortor odio in nunc.</p>
  </article>
 <?php endwhile; else: ?>
     <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
  <?php endif; ?>

Now let’s replace the static content with WordPress calls, starting with the content within the <h2>:

<h2><a href="" title=" "><?php the_title(); ?></a></h2>

The first step was to replace the article title with the_title();. This function displays the post title that we’re currently looping through. Next we’ll link to the article using the the_perma-link(); function:

<h2><a href="<?php the_permalink(); ?>" title=""><?php the_title(); ?></a>
        </h2>

We also need to input something in the title attribute of the <a>. I like to use a mix of static content with the post title. Here I want the title to be “For More Info on <!-- article title --> Click Here”:

<h2><a href="<?php the_permalink(); ?>" title="For More Info on
        <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>

The last thing to do is call the article content. Currently, we’re using a static <p> to house the content. In all likelihood, the content area will be made up of all sorts of content and HTML tags, including images and videos. Anything we put into the content editor will be displayed here when we call the the_content() function:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
  <article>
    <h2><a href="<?php the_permalink(); ?>" title="For More Info on <?php
        the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
    <?php the_content(); ?>
</article>
<?php endwhile; else: ?>
      <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
  <?php endif; ?>

That’s it! We’ve completed our loop and our theme is now functioning and dynamic. The index.php should now look like exactly like this:

<!DOCTYPE html>
<html lang="en">
<head>
  <title><?php bloginfo(); ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="<?php bloginfo(
        'stylesheet_url' ); ?>" >
  </head>
<body>
<header>
  <h1><?php bloginfo( 'name' ); ?> | <?php bloginfo( 'description' ); ?> </h1>
  <nav>
    <ul>
      <?php wp_nav_menu( array(' menu' => 'Main Nav' ) ); ?>
    </ul>
  </nav>
</header>
  <section>
  </section>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
  <article>
    <h2><a href="<?php the_permalink(); ?>" title="For More Info on <?php
        the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
    <?php the_content(); ?>
</article>
<?php endwhile; else: ?>
      <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
  <?php endif; ?>
    </section>
</body>
</html>

Once you understand what each WordPress call does, you can make more sense of the above code. At that point, you’re like Neo from The Matrix, seeing Matrix code rather than people. This is a very clean and easy-to-read template, and the beauty of The Loop is that it displays the right content for each and every page you’re currently viewing. Don’t believe me? Start navigating your site—you’ll see the content change based on the URL and the index.php template page will power everything, whether you’re on the home page, a single post, or even on a Search Results page.

Let’s take a break. In the next chapter, it’ll be time to buckle up because this was the easy stuff.

  • + Share This
  • 🔖 Save To Your Account