Other Review Indexes
Requirements
- This is intended for the WordPress platform. 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.
What we’re making
This review index is much simpler than the last two. For starters, it doesn’t require that you use the Custom Fields plugin! And secondly, it doesn’t require that you go back and edit stuff into all your old reviews. This code works right out of the box and will automatically compile everything — yay!
For an example of how this works, check out my Reviews by Year index. Basically we’ll have a header that lists the year, and then an ordered list of all the books that have been reviewed in that year, with links to the posts. Spiffy, yeah?
Create a New Page for the Review Index
If you’ve read my other tutorials, you know the drill. You need a new page that will be the home of this index, so go ahead and create one. Name it something relevant like “Reviews by Year.” The title doesn’t really matter though — it’s up to you. 😛
The Code
As usual, I created a new page template for my theme called “page-reviews-year.php”. If you do this, 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 Year')) { } ?>
The ‘Reviews by Year’ 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 global $wp_query; $args = array( 'cat' => 3, 'year' => 2012, 'order' => 'ASC', 'posts_per_page' => -1 ); $posts = query_posts( $args ); echo '<h4>2012</h4>'; if($posts) { echo '<ol>'; foreach($posts as $post) { echo '<li><a href="' . get_permalink($post->ID) . '">' . get_the_title($post->ID) . '</a></li>'; } echo '</ol>'; } wp_reset_query(); ?>
Phew, this doesn’t look too bad — huh? However, it’s important that you remember to change the value for ‘cat’. Find the line of code that says:
'cat' => 3,
I have ‘cat’ set to ‘3’ because ‘3’ is the ID of my “review” category. You will have to change this to whatever ID your review category is. You can find the ID by simply going to the category page (Posts » Category), hovering over the category name for your reviews section, and taking a look at the URL. Look for something like this:
&tag_ID=3
That will be near the end of the URL. Note down that number and replace the ‘3’ in the code with whatever your ID number is.
Let me break it down
If you want to know what the code is doing, read this section. If not, skip ahead to the next.
First, we define a custom query. We want to look for posts in “category 3” (which is my book review category), posted in 2012, put them in ascending order, and display an unlimited amount of posts per page.
Then, we insert a header for the year (2012). Below that, we check to see if our query yielded any results. If there are no posts that match our criteria, we do nothing. But if there are posts, then we create an unordered list. For each post that exists, we display the title of the post and include a link to it.
Only one year at a time…
So if you read the above section on breaking it down, you’ll see that the above code is only grabbing posts from one year — 2012. For each year that you have reviews, you will have to copy that code and paste it again, changing all instances of 2012 to whatever year you want to display reviews from. So this isn’t fully automatic since you will have to add a new section of code every year. But hey, that’s not too bad, is it? 😛
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, view the Reviews by Year page and see if it’s indexing your reviews! If it is, you did everything correctly and you’re done!