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

Home > Articles > Web Design & Development

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.

Peachpit Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from Peachpit and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about Peachpit products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites; develop new products and services; conduct educational research; and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email ask@peachpit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by Adobe Press. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.peachpit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020