Jonnie Grieve Digital Media: Blog

Home
by on 17th May, 2024 - 9:45am (0)

Blog: Working with Post Authorship and Custom Page Templates in WordPress (More Posts)

In Blog 5, we will explore how to work with Post author tags and utilise the author template. Additionally, we will discuss custom page templates and explain why they are sometimes useful in replicating subtle differences in your page templates.

Let’s start by focusing on Authorship in WordPress. Whenever you use an author tag in WordPress it generates the username of the user who wrote and published a particular blog. And it puts that in a hyperlink.

Clicking on that hyperlink tells WordPress to look for the author.php template.

Add this template to your project at the root of your theme.

author.php

For now, I’m just going to bring in the header and the footer and mark the template as being served by the author template.

<?php require "header.php"; ?>

<?php echo "author.php"; ?>

<?php require "footer.php" ?>

Because right now we have to go back to the single.php template and work on adding the link to the author template.

In the Single Post template find the place in your design where you’ve put the author’s username. e.g.

<h4 class="post-authorship"> By <a href="#">username</a> ... </h4>

You probably used a placeholder username in your HTML pages. We can replace this placeholder and add the username dynamically by using the following method.

<h4 class="post-authorship"> 
    <?php the_author_posts_link(); ?> By <a href="#">username</a> ... 
</h4>

Unfortunately, there’s a problem. I’ve added the method but it doesn’t appear on the screen.

<div class="post_header">

<h2 class="post_title">Post Title: <?php the_title(); ?></h2>

<h4 class="post_authorship">By <?php echo the_author_posts_link(); ?> <a href="#">username</a> 08th May 2024 (00:00pm) (comments - <span id="num_comments">0</span>) </h4>

</div>

So I’m still seeing the original link, which I left in the code for now, but nothing more than that.

It’s at times like this when unexpected things happen that it’s good to look at the official documentation to see why this might be.

Sometimes it’s a simple thing like a missing parameter or a misplaced string. You’ll not always find the solution straight away but if you know where to look, there’s information in the documentation that confirms that you need to use the ID parameter in author methods to generate the link. That is unless you’re using the WordPress Loop.

The Loop is the method that we’ll use.

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

<h4 class="post_authorship">By <?php the_author_posts_link(); ?> . . . </h4>

<?php endwhile; else : ?>

<?php endif; ?>

With the author posts link method in place wrapped inside the WordPress Loop, we should now see the dynamic username and be able to follow the hyperlink through to the Author template.

So let’s go back to the Author Template. The author URL you click on takes you to the Template with an added URL in the bar for the username.

e.g /author/jonniegrieve/

So when we put the Author method on the template like this…

<?php the_author(); ?>

That will as an example simply output “jonniegrieve”. So we can use the template to say something like

<p>A list of posts by <?php the_author(); ?></p> // author.php

This all takes place inside an element with the class of .post_authorship.

Date and Time Methods

The Date and Time methods are the tools we use to get dynamic information about the Date and Time the associated blog is posted.

<?php the_date();?>
<?php the_time(); ?>

These methods take a set of string parameters allowing us to apply any combination of date and time formatting we like. They take a timestamp from a Page or Post and convert that timestamp to a readable string.  The Parameters we can use are listed here.

Example.

<?php the_time('Yi'); ?>

These 2 methods seem to do the same thing. They’re both capable of taking date and time parameters.

So the question is why would you use the_time(); for date values and not the_time();? The answer is that the_time(); only displays the date once per day within the WordPress Loop. This means if multiple posts were published on the same day, the_time(); would only show the date for the first post of that day.

Now, for us that means we should be able to use the_time(); in home.php and single.php templates without any issues with disappearing data.

How to find the correct string values to use…

The string format I’m using for dates and times goes like this.

08th May 2024 (00:00pm)

This requires the following parameters:

j for numeric day of the month with no leading zeros - (8)
S for the suffix of the day of the month - (th)
F for the full Textual Month - (May)
Y for numeric year - (2024)
G for 24 hour hours format with leading zeros - (00)
i for minutes with leading zeros - (00)
a for lowercase - (pm/am)

Let’s pass those parameters in.

<?php the_time('jS F Y Gi a'); ?>

Notice how these are separated by empty spaces which are counted as strings in the output. We can clean this up even further with special characters.

<?php the_time('nS F - Y (G:ia)'); ?>

With this in place, we can format the author template to list all the titles and excepts from every post associated with a given user.

<?php echo "author.php"; ?>

<p>Posts by: <strong><?php the_author(); ?></strong></p>

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

<h3><?php the_title(); ?></h3>

<p><?php the_excerpt(); ?></p>

<?php endwhile; else : ?>

Custom Page Templates

In WordPress, page.php templates deliver the content from the file, and therefore the same page design. But sometimes you need to have pages that are distinct from others with a unique design and purpose.

Here’s how you can achieve that.

For my example, I’m going to create a template called

page-template.php

This marks it out as a Page Post Type with a unique name.

WordPress has to know which Page is to be assigned a specific Template. This process starts with a comment header with a Parameter called Template Name. The identifier for this template is “Template Examples”.

/*
  Template Name: Template Examples
*/

Now go to the Admin area. Find a page that you want to assign to “page-template.php”.

Select the page in the admin area and look for Template Examples in the Template Dropdown box

Select a Page to edit -----> Page -----> Template -----> Select a template in dropdown box. e.g. "Template Examples".

You can have as many custom templates as you need.

In conclusion

We’ve added more enhancements to the single.php template and got the Author Template up and running. We’ve also demonstrated how to work with different designs with unique templates for some of your Pages in WordPress.

In the next blog, we’ll create category and archive templates and get the post list pagination up and running on all the templates that need them.

This post has been assigned to the following categories

    Leave a Reply

    Your email address will not be published. Required fields are marked *