Create an Automatically Updating Review Index by Book Author

Bitchin’ Book Blog consists of tips to help you become a better blogger and have a sleek, professional, and user-friendly website. It is also a collection of WordPress tutorials and code snippets to help enhance your blog. If you have a question about blogging/web design, or want to request a tutorial, you can do so here (it’s completely anonymous)!

Note: This is somewhat of an older method to create a review index. It still works 100%, but if you want a much better, more user-friendly way to do it, consider purchasing the Ultimate Book Blogger Plugin. It comes with this review index already installed, plus a million more features!

Other Review Indexes

This guide is for WordPress users. I’m sure there are similar ways to achieve this for Blogger, but the code in this tutorial uses code and plugins specific to WordPress.

You may also want to check out my other tutorial on how to create an automatically updating review index by book title. This one is just for book authors!

I honestly can’t accept any form of credit for this snippet of code that I’m about to show you. I knew what I had to do, but I’m not an expert with PHP and wasn’t completely sure how to do it. But my amazing boyfriend put this together for me and I’m incredibly grateful! So all coding credit goes to him! Love you, babe ♥

There are a few requirements and things you should know before copying this code:

  • As previously stated, this is intended for WordPress users. Something similar might be achievable in Blogger, but I don’t know.
  • You need the ability to edit your theme files or insert PHP code.
  • This specific code snippet requires that you use the WordPress Custom Fields plugin. Here’s a guide on how to set it up. It’s not required that you have the information displayed, but it is required that the plugin is installed and that you have at least one custom field for “Author.” This field must be filled out in order for books to be indexed.
  • Your reviews must be “tagged” with the author’s name. The way the author’s name is written in the tag must be the same as how it is entered in the Custom Field. (i.e. if it’s tagged as J.K. Rowling, it will NOT work if it’s entered as “J Rowling” in the Custom Field. They must be the same.)
  • Your tag URL structure must be: http://www.yoursitename.com/tags/tag-name-here, where “yoursitename” is your domain name and “tag-name-here” is the name of a tag. If yours is different, you can change the code to accommodate that, but it means you won’t be able to copy and paste the code and have it immediately work.

How it will work

Take a look at my reviews by author index for an example of how it will look. Basically, we’ll be compiling a list of all the authors that appear on your blog for a book you have reviewed. Then, we link to the tag of that author. If you have tagged and labelled your reviews correctly, that tag archive page should show all your book reviews (or other posts) that have been tagged with that author’s name.

This works out well for me since I avoid tagging authors’ names in non-review posts. But if you often tag non-review posts (Waiting on Wednesday, In My Mailbox, other memes, and giveaways), this might not work as well as you’re hoping. It just means that when someone clicks on an author’s name in the index, the results will contain more than just book reviews.

All authors are sorted by last name. So “Robin LaFevers” will appear under “L” for “LaFevers.” And they’re also displayed as Last Name, First Name. So “Robin LaFevers” will appear under “L” as “LaFevers, Robin”. The way the code is written is that it will grab the last word in the Author Custom Field, put that at the beginning, add a comma, and then insert the rest of the name.

1. Create a New Page for the Review Index

You will need a new page for housing this review index. Create a new one and name it appropriately (like “Review Index by Book Author” :P ).

2. The Code

Just like in the last tutorial, there are a few different ways you can insert the code. I chose to create a new page template for my theme called “page-reviews-author.php”. If you do this, you’ll want to copy your page.php template file and paste the code we’ll be using (shown later) directly after this line:

<?php the_content(); ?>

Alternatively you can edit your page.php template file and run this if statement (also after the line above):

<?php if (is_page('Reviews by Author')) {  } ?>

The ‘Reviews by Author’ should be whatever you titled your page that will be displaying this index. Then you could include the code inside the { brackets }

The important thing is that you need this code on your page:

<?php
	// Get all posts
	$posts = get_posts(array(
			'numberposts' => -1,
			'post_type' => 'post',
			'meta_key' => 'author',
			));

	$author_array_fixed = $author_array_raw = $author_letters_existing = array();

	$i = 0;
	foreach ($posts as $post) {
		// Grab the author name
		$raw_author = get_post_meta($post->ID, 'author', true);
		// Convert the name
		$author_split = explode(" ", $raw_author);
		if (count($author_split) == 1) {
			$author_fixed = $author_split[0];
		}
		else {
			$author_fixed = $author_split[count($author_split) - 1].", ";
			$author_fixed .= implode(" ", array_slice($author_split, 0, count($author_split) - 1));
			$author_fixed = trim($author_fixed);
		}
		if (!in_array($author_fixed, $author_array_fixed)) {
			// Add to arrays
			$author_array_fixed[$i] = $author_fixed;
			$author_array_raw[$i] = $raw_author;
			if (!in_array(strtolower(substr($author_fixed, 0, 1)), $author_letters_existing)) {
				$author_letters_existing[] = strtolower(substr($author_fixed, 0, 1));
			}
		}
		$i++;
	}
	natcasesort($author_array_fixed);
	// Now we have all the authors
	foreach (range('a', 'z') as $alpha) {
		echo '<h4>'.strtoupper($alpha).'</h4>';
		if (in_array($alpha, $author_letters_existing)) {
			echo '<ul>';
			foreach ($author_array_fixed as $id => $author) {
				if (strtolower(substr($author, 0, 1)) == $alpha) {
					// List the author
					echo '<li><a href="/tag/'.str_replace(' ', '-', $author_array_raw[$id]).'">'.$author.'</a></li>';
				}
			}
			echo '</ul>';
		}
	}

?>

Yikes, that’s crazy, isn’t it? Honestly, I’m not even going to try to break it down for you… it’s crazy and it just makes me drool over how awesome my boyfriend is for being able to write that! If you want to understand what’s going on, just reread How It Will Work.

3. Upload & Test It Out!

Make sure you upload your new template file (if you made a new one) to the proper directory. Then, you’ll have to go back to your page for this index and set it so that it’s using your new template.

Then, edit one of your old book reviews and fill out the custom field box to enter in the “Author.” Also make sure that the post is correctly tagged with the author’s name (spelled the same way as in the custom field). Then, view the Reviews by Author page and see if it’s displaying the author!

Unfortunately you will have to go back and edit every single book review post to add in the book letter title. But it will save you from a lifetime of manually updating your index!

15 Responses to “Create an Automatically Updating Review Index by Book Author”

  1. Jaki

    this is such an excellent tutorial! i’m stuck on one thing tho – you say “The ‘Reviews by Author’ should be whatever you titled your page that will be displaying this index. Then you could include the code inside the { brackets }”. Exactly what code are you putting inside those brackets? I’m a bit lost there – I’m editing the page template instead of creating a new one, and while I have that code there, and then pasted the big code section on the new page, the page is just displaying the code.

    • Ashley

      Paste the really long big of code inside those brackets. But since you’re doing it that way, you need to remove the < ?php ?> tags (at the very beginning of the long bit of code, and at the very end). Because if you’re adding in that “if” statement then you already have PHP tags open and you don’t need to open them again!

  2. Chantelle

    Great tuts. But I have 1 issue. Normally I am pretty good with php, but there is something I can’t figure out. The ‘book reviews by author’ page just has the letters of the alphabet, no links.
    I tag all my authors as ‘author: firstname surname’ so can I change something in the coding to make it work? I tried tagging them as ‘firstname surname’ but that still doesn’t link to anything. The ‘author’ field in the custom fields is the same as it is in your set up, because I figured that would be easiest.
    The title one worked perfectly though. Maybe I am just too tired.
    Chantelle recently posted..Book Review: Whose Body?My Profile

Leave a Reply

CommentLuv badge