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!
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.
Paste the really long big of code inside those brackets. But since you’re doing it that way, you need to remove the 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!
OH! I feel like a total idiot now. LOL. I actually thought the huge code was meant to go on the page I created for the index. No wonder it didn’t work! :headdesk: Thank you!! It works perfectly now! π Your site is absolutely brilliant – i spent about 3 hours here tonight going through all your tuts and tips. I’m still building my blog and your site has a wealth of information for me. π
Yaay! I’m glad you got it figured out and that it’s working. π
you’ll probably regret explaining it tho, cos I’m going through ALL your wordpress tuts and implementing them. *laughs* Thank you again.
No problem! π Enjoy!
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.
Oops! That was slightly incoherent wasn’t it. What I mean is that although the index shows up on my ‘Book Reviews by Author’ page, there are no links. I am assuming this is because I tag everything ‘author: firstname surname’, but then when I changed them to just ‘firstname surname’ there still seems to be an issue somewhere.
It looks like your page isn’t picking up anything being in the author field. Do you definitely have the ACF author fields set up *and filled out*?
The author field is definitely filled out in each review and as far as I can tell, they are set up the same. This is a screencap of what it looks like:
http://www.butterflytempest.net/Screen%20Shot001.jpg
http://www.butterflytempest.net/Screen%20Shot002.jpg
So that’s why I can’t figure it out. *headdesks*
I’m getting 403 Forbidden errors for both of those images. :/ It’s kind of hard to figure out what the problem is without seeing your custom fields set up and how you’re entering in the information.
Argh! I’ll upload them to my photo bucket account and redo the links later when I am on the computer later this afternoon (Can’t do it from mum’s iPad). Stupid website.
Okay, this time the pictures should work. It is obviously not my week to be working with technology.
http://img.photobucket.com/albums/v609/Angelic_Butterfly/ScreenShot002-1_zps7dbab151.jpg
http://img.photobucket.com/albums/v609/Angelic_Butterfly/ScreenShot001-1_zps436cb990.jpg
Can you take a picture of the edit page of one of your posts too?
Yep. Here you go.
http://img.photobucket.com/albums/v609/Angelic_Butterfly/ScreenShot003-1_zps6bf02938.jpg
There must be something wrong with your code then because I just tested it using those same variables and it works perfectly fine!
Thanks for your help Ashley. I just checked the page again and it’s all working, so I must have had some sort of gremlin or something in my WP.
This specific code snippet requires that you use the WordPress Custom Fields plugin.
What if I already have custom fields for items such as author, genre, series created within my theme already, would this code work if I used the meta title of those?
It completely depends on how your theme is set up to do it. You could certainly try it and see if it works, but if not, I wouldn’t really be able to dig into your theme code to debug it for you. π
Thank you! I am just so frustrated trying to find php code that will accomodate custom field types, or a plugin that will accomplish the same thing. Since they are already set, I don’t want to have to create new fields and then touch every single review π
I have 4 custom taxonomies, Genre, Author, Series & Publisher. Each taxonomies can then have however many entries I chose. Once created, they look like this http://www.mysiteurl.com/book-book-author/gina-robinson. I’m just confused on if the code should point to the custom field _book_author or the entry within it, or if it will even work π
The code would take some rewriting anyway to get it to work with custom taxonomies. Custom fields and custom taxonomies are two different things!
Thank you anyway, I greatly appreciate the input π
What if a book has more than one author? Do you put both author’s name in the ACF for author? How do you separate them, by comma or semi colon?
This bit of code isn’t really written to support multiple authors. You certainly could just type both author names in the author box, but then they’ll be organized a bit weirdly and won’t appear under two separate entries (one for each author’s name).
Well. This didn’t do what I was hoping to do but if you were wondering the coding still works perfectly. I just misread what it would do (was hoping it would show both the author and the posts under it, should have known that would be more complex).
As always, thanks for being such a great resource.