Time for action – adding metadata, the timestamp, and author template tags

In our mockup, we have included some metadata—information about each post being displayed. Specifically, we've included the author's name and the column the article is featured in. In WordPress, what a magazine editor might call a column is referred to as a category . To add these, we do the following:

Inside the Loop below the <h2> tag, add the following in place of the existing <p> tag and its contents:

<p class="entry-meta">by <?php the_author_meta('first_name'), ?> <?php the_author_meta('last_name'), ?> in <?php the_category(", ") ?></p>

Save index.php and refresh your browser.

What just happened?

We added two template tags to display metadata about the post:

  • <?php the_author_meta('first_name'), ?> displays the author's first name and, unsurprisingly, <?php the_author_meta('last_name'), ?> displays the author's last name
  • <?php the_category(", ") ?> displays a list of categories assigned to the post, with a comma in between each listing

When we refresh the browser, the metadata is now displayed:

What just happened?

Tip

Help! It's not showing what I expected it to!

If your page isn't showing the name of the author or it's displaying Uncategorized instead of a list of categories, don't worry.

The author name probably isn't displaying, because you haven't added your full name to your user profile. Go to Users | Your Profile in the WordPress admin and add your first and last name, remembering to save your changes.

If a list of categories isn't being displayed, that's simply because you haven't assigned any categories to your post(s). In the post editing screen, assign a category or create one.

Keeping up-to-date with WordPress

In older versions of WordPress you could use the_author_firstname and the_author_lastname template tags. While those tags still work in version 3.4.2, they have been deprecated and the the_author_meta template tag has been introduced.

This meta tag offers more flexibility than the previous template tags, by allowing the theme to take advantage of all the user registration information with a single tag, as well as any additional information a custom plugin may add to the user registration database table. For a list of parameters you can send to this template tag, see https://codex.wordpress.org/Function_Reference/the_author_meta.

Adding to the Loop

Because this is the web and not a paper magazine, there are WordPress features we should take advantage of. We want to take advantage of having people's comments and ideas on the article to help keep it fresh. So, we'll show how many comments have been added to the post.

For now, we'll just display the number of comments for the post instead of displaying all of those comments. In the next chapter, we'll edit the single.php file to display comments when a user is viewing the individual post. For the time being, we'll show how many comments there are and include a link, so that if the user clicks on the number of comments, he or she is taken to the post's individual page.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.2.184