So what are we doing?
In this tutorial I’ll show you how to use custom fields to easily fill out and automatically display certain data. More specifically: book data. Check out any one of my book reviews as an example. For each review I display the same information: title, author, series, publisher, publish date, genre, etc. Now it’s totally annoying to have to format that information every time, so we’ll create an easy way to enter it into each review!
Requirements & Recommendations
- Required: Must be using the WordPress platform
- Required: Must be able to install plugins
- Recommended: Access to theme files and general knowledge of how to edit them
1. Install the Plugin
We’re going to be using a plugin called Advanced Custom Fields, so go ahead and install it! You can do this by navigating to Plugins » Add New and searching for “Advanced Custom Fields.”
Got it? Spiffy! Moving on.
2. Create the Field Group
Next up, we’ll be creating the field group. So in your WordPress admin area, navigate down to Custom Fields, then click “Add New.” Then follow these instructions:
- You’ll have to enter a title for your group. This can be anything. I called mine Book Reviews. But ultimately, the title doesn’t matter.
- Next, we create new fields! You have one field for each piece of information you want to display, such as book title, author, publish date, publisher, genre, etc. For each field enter a field label and select the field type. The field name should be distinguishable so you don’t get anything conflicted with other elements of WordPress. A good recommendation is to use the suffix “book” for all your entries (i.e. “Book Title,” “Book Author,” “Book Pages,” “Book Genre,” etc.). The field name should automatically be filled out for you. If your label is “Book Title,” it should enter “book_title” as the name. That’s good! As for the field type, that can always be “Text.” There’s no need to fill out instructions or a default value (unless you want). Required can be set to “no.” I used to set some of mine to “yes” but it was annoying because then you had to fill them out for your drafts, and sometimes I didn’t want to fill out that information until later.
-
Once you have created all your fields, scroll down to the next section, Location. The location specifies when you want your field options to appear. Now this will vary depending on your own blog settings, but in general you only want them to appear when you create a new post in whatever category you have for book reviews. Otherwise these field options will show up on every new post you create, even when you’re not writing a book review! Here’s what I have mine set to:
Post type is equal to post
Post Category is equal to Reviews
Match all of the aboveAs I said, this may vary depending on how you have your blog set up. It’s not a problem if you don’t fill this out, it just means that the entry fields will show up whenever you create a new post, regardless of which category it’s in. They won’t actually show up on the published post — just on the create post page.
- If you want you can mess with the settings in the Options category. I have mine on the defaults. The most significant is the position (I have mine set to “Normal”).
- Once everything looks good, click Update on the right!
3. Create a new post (or edit an old one) for testing
Next you’ll want to either create a new post or edit a previous post for testing purposes. Make sure it’s in your book review category. If you followed the above instructions correctly, you should now see a new section on the Add New Post page called Book Reviews (or whatever you titled your field group). And there you will see entry fields for all the fields you created! You can easily type in the book title, author name, pages in the book, etc. Fill all that out and save your draft (or update your post, if editing an old one). But unfortunately, this isn’t enough to display your data. All we’ve done so far is store it in the database, now we have to display it!
4—A. Displaying the information – ADVANCED
This is where the blog splits up into two parts. If you’re able to and comfortable editing your blog’s theme files, use this method. It makes the entire displaying process automatic. If you’re not, skip down to section 4—B. It’s a bit more tedious and requires work each time you create a new review, but it doesn’t require you to edit your theme files.
Using the method of your choosing, open up your theme files. If you’re unsure how, go to Appearance » Editor. You should see a list of files on the right hand side. Now depending where you want to display this information, there are a few different files to edit. But the main one is Single Post – single.php. Locate and open that file.
Scroll down until you find this piece of code:
<?php the_content(); ?>
We’re going to be pasting our code right above this.
Now, for each field you created, we have to repeat the same piece of code (but slightly edited for each one). It looks like this:
<?php $book_title = get_field('book_title'); if($book_title) { echo '<strong>Title:</strong> ' . $book_title . '<br />'; } ?>
You probably noticed that this is the snippet for the Book Title field. Basically we’re getting the data in the “book_title” field, checking to see if an entry for that field exists, and if it’s not blank, we display the word “Title” followed by whatever is entered in the book_title field.
You’ll probably want to open up your Custom Field group page in a new tab while you enter in this code. You’ll need to know the field name for each field (note: NOT the “field label”). Yours may be different to what I’m about to paste below, so keep that in mind and edit yours accordingly.
Here’s what my final copy looks like:
<p> <?php $book_title = get_field('book_title'); if($book_title) { echo '<strong>Title:</strong> ' . $book_title . '<br />'; } ?> <?php $author = get_field('author'); if($author) { $author_link = str_replace(' ', '-', $author); echo '<strong>Author:</strong> <a href="/tag/'.$author_link.'">' . $author . '</a><br />'; } ?> <?php $book_series = get_field('book_series'); if($book_series) { $series_link = str_replace(' ', '-', $book_series); echo '<strong>Series:</strong> <a href="/tag/'.$series_link.'">' . $book_series . '</a><br />'; } ?> <?php $publisher = get_field('publisher'); if($publisher) { echo '<strong>Publisher:</strong> ' . $publisher . '<br />'; } ?> <?php $publish_date = get_field('publish_date'); if($publish_date) { echo '<strong>Publish Date:</strong> ' . $publish_date . '<br />'; } ?> <?php $genre = get_field('genre'); if($genre) { echo '<strong>Genre:</strong> ' . $genre . '<br />'; } ?> <?php $book_pages = get_field('book_pages'); if($book_pages) { echo '<strong>Pages:</strong> ' . $book_pages . '<br />'; } ?> <?php $book_source = get_field('book_source'); if($book_source) { echo '<strong>Source:</strong> ' . $book_source . '<br />'; } ?> <?php if (get_field('related_books')) : ?> <?php echo '<strong>Related Books:</strong> '?> <?php foreach(get_field('related_books') as $post_object): ?> <?php $temp_array[] = '<a href="'.get_permalink($post_object->ID).'">'.get_the_title($post_object->ID).'</a>'; ?> <?php endforeach; ?> <?php print implode(", ", $temp_array); ?> <?php echo '<br />' ?> <?php endif; ?> <?php $buy_the_book = get_field('buy_the_book'); if($buy_the_book) { echo '<strong>Buy the Book:</strong> ' . $buy_the_book . '<br />'; } ?> <?php $book_goodreads = get_field('book_goodreads'); if($book_goodreads) { echo '<strong>Goodreads:</strong> <a href="' . $book_goodreads . '">' . $book_title . '</a> <br />'; } ?> <?php $my_rating = get_field('my_rating'); if($my_rating) { echo '<strong>My Rating:</strong> <span class="' . $my_rating . '"></span>'; } ?> </p>
Remember, you have to change the variables to reflect your own field names! I don’t mind if you copy mine directly but it won’t work unless your field names are exactly the same as mine.
Once you save the single.php file, view your test page and if you followed the instructions correctly, you should see your information! Much easier than formatting the text every time!
Now since you only edited the single.php file, this information will only show up when you view an individual page. If you also want it to show up on your index page, archive page, search page, and any other index of posts, you have to copy the exact same code into these files (if they exist for your theme):
- Category Template – category.php
- Main Index Template – index.php
- Search Results – search.php
- Tag Template – tag.php
- Archive Template – archives.php
The code should always be pasted before the piece of code
<?php the_content(); ?>
or
<?php the_excerpt(); ?>
Congratulations! You’re done! π
4—B. Displaying the information – EASY
This way of displaying the information is easier, in the sense that it requires less coding knowledge, but it is much more tedious in the long run. In the advanced method described above, the information is always automatically generated. In this method, you still have to enter in some kind of code any time you create a new book review.
In one tab, have open your testing post with the fields already filled out. In another tab, open up your Custom Fields settings so that you can view all the fields you created. You’re going to need to note down the field names of each field. Note: this is the field name — NOT the “field label.”
On your testing post page, enter this code in the body of the text (using the visual editor):
[acf field=”field_name”]
You’ll have to change “field_name” to whatever your field name is. So say you want to display the book title. Presumably your field label is “Book Title” and the field name is “book_title”. So your code would look like this:
[acf field=”book_title”]
Now you’ll have to do that for each of the fields you created. A full example might look like this:
[acf field=”book_title”]
[acf field=”book_author”]
[acf field=”book_series”]
[acf field=”publish_date”]
[acf field=”publisher”]
[acf field=”book_pages”]
[acf field=”my_rating”]
As I said, this option requires less coding, but in the end, it is a lot more tedious and has a lot less flexibility. If you have the ability to edit your theme files, I’d strongly suggest using the first method. You can always keep a backup of your theme file in case you make a mistake and need to revert back!
Closing words
I hope this tutorial proves helpful π I find it extremely useful when displaying the same data for every single book review. It makes the process a lot easier and quicker! If you have any questions or if anything is unclear, feel free to leave a comment or send me an e-mail.
So… since I have the attention span of a gnat, I’ll have to come back in the AM when I’ve slept all night bc this tutorial is AWESOME!!
<3
Thanks chick!!!
I’m glad you like it! Don’t hesitate to let me know if you have any questions. I’m not sure how good my tutorial-writing skills are!
Oh and I need to know what plug in that freaking social sidebar thing is!! WIN!!!
The plugin is “Subscribers Text Counter.” The plugin itself will keep track of your RSS, Twitter, and Facebook followers and will display them for you. Then I manually added in my StatCounter stats and Goodreads friends. The Goodreads friends is the only one I’m having to update manually (but I’ll try to find a way around that!).
The nice thing about this plugin is that you can style it however you want! π
Love this! I have this plug-in installed but never used it. Now I know how! You’re awesome. Thank you for this. I’m learning lots
I’m so glad this was helpful for you Giselle! I use ACF for like.. EVERYTHING! For book info, giveaway info, blog tour info, adding extra CSS classes to my Featured Image (like to determine if I want it to float left, float right, be center blocked, etc.). This is the best plugin ever!
Nice. Of course, my theme doesn’t have a single.php file, nor does it have anywhere for me to put the code that I can see, unless I can put the code in the functions file.
Thanks so much for this!
Hmm. Of course all themes are different. It’s certainly possible that your theme-maker is just using an if/else statement in the index.php file (for example, “if it’s the homepage, do this”, “if it’s an individual page, do that”).
It has to be in the place where your content is being called (so not functions.php). The most likely place after single.php would be index.php!
If you can’t figure it out, I’m happy to take a look at your theme and see if I can make it work. π
Hmm! Maybe it’s different on mine because mine is a child theme and you can’t edit the original genesis theme.
Should I email you the theme to take a look at it? Thanks so much. Right now I’m using templates, and I find them to be a pita.
Sure, you can e-mail it to me at ashley@booknook.me
I honestly don’t have as much experience with child themes, but I can definitely take a look!
Hi! I was just wondering if you worked out how to get this tutorial to work with a Genesis child theme.
There is a way to do it all in your functions.php file but I’d have to rewrite the entire tutorial to explain that, which I don’t really have time to do at the moment.
Although, the Ultimate Book Blogger Plugin does have it all built in and automatically displays without you having to edit your theme at all.
I’m going to email you with questions. π
And thanks for replying so quickly!
AH-MAY-ZZZING tips you have here.
I have been looking for plugin like this! Thanks!
You’re so welcome JoAnne! Advanced Custom Fields would be PERFECT for your book report cards!!
I love these posts…they’re infinitely helpful!
If you don’t mind helping me out a bit, I’m curious how to make this work slightly differently for my blog…
I start out the same as most everyone, with Title/Author/Publisher etc (or rather, I’m starting to, my first few didn’t have this, I’m going back to edit them once I’m finished my switch to self-hosted WP), the image to the right and the blockquote there with the blurb. AFTER the blurb I have the links to add to GR or buy it on Amazon etc. I’m thinking that what I need to do is set up 2 different custom fields, one for the book info and another for the buy it/add it section. How does the .php code show where for the fields to show up within the post? If necessary, I can do the book info in the .php files and then use the beginner code you posted for the buy it/add it in the html when I’m typing the post (or continue typing it out the way I have been), I’m just curious if there’s a way to do it automatically. The link to my last review below will show you what I’m talking about if I’m not making sense, lol.
TIA for any help you can give. These posts are so helpful, thanks again!
Hi Bree!
What you’ll have to do is set up custom fields for book title, author, publisher, date, format, source, genre, blurb, and the goodreads/buy it now links! Also depending on your set up you might need one for the book cover too. I don’t have one for book cover since I use the featured image, but if you don’t (like if you insert it into the post) then you’ll need one for the image as well.
When you install Advanced Custom Fields, you’ll see that you can have custom field boxes that are larger text areas for the blurb, or image upload buttons for the image.
How the custom fields are arranged and displayed depends on how you write up the PHP file in your theme. Around the PHP for calling each of the custom fields you can insert HTML. For example, you can put in blockquote tags and then call the blurb field inside of them so that the blurb shows up in blockquotes.
If you still have questions, feel free to e-mail me! And if you can’t figure it out, I do provide a service where I can set it up for you in exchange for an Amazon Gift Card. π
Thanks, Ashley…that makes perfect sense. I should be able to get it from here, if not I will let you know!
i love this! makes things SO much easier! The one thing I can’t figure out – for the “My Rating”, i have the field called the same as in your code, and i have it set as “image” instead of “text” and I upload the image (png) when doing the post…but the image isn’t showing up. Is there something with that particular image that needs adding in the code?
Okay I have my rating set up slightly differently. If you’re uploading an image, make sure on the custom field it is set to return the Image URL. Then, in the output, it should look something like this:
My Rating:</strong> <img src=”‘ . $my_rating . ‘” alt=”My Rating” />’; } ?>
ah! thank you! took me a bit, i must have copied some incorrect code. LOL. It’s working now, just gotta figure out why it’s under the ‘rating’ instead of next to it. You’ve helped me heaps tonight, I’d better start figuring some of this out on my own. LOL. I think I’ll be getting your book plug in at some stage too, it looks brilliant π
Hi Ashley! I just came across this wonderful post, and I must say it’s a very helpful one! I knew that it’s possible to create custom fields but I had no idea how. Since I only have basic HTML knowledge and zero PHP, I didn’t even know where to begin. Seriously cannot thank you enough for posting this tutorial, and making my life for future reviews so much easier! (Sorry if I’m gushing too much but I was so ecstatic when I finally got everything up and running :D)
I’m so glad it was helpful to you! π
If I wanted to use these fields for index purposes only, what php files would need to be updated?
If they’re just for the index then you don’t have to update any php files for this part. You just have to create the fields themselves. π
Thank you! So, I would only need to create a custom page template with the php that supports the custom fields that I create, correct?
You just have to create the page template for the review archive page. And that one will have the custom field details on it.
Thank you, thank you, thank you! I am determined to find an easier way to index my reviews. Unfortunately, I don’t know enough about php to try and code for the existing data contained in my custom taxonomies (with children), within my custom post types. Even though creating custom fields will require I touch every post, at least I know that there is light at the end of the tunnel. π
Hi there – I was looking for something like this, I love that your blog is the 1st place I go! Anyways, I’m going to start creating mini book lists on my blog. Can I use this form multiple times in one post for different books?
This particular example could not be. It is certainly possible though, it would just require this add-on: http://www.advancedcustomfields.com/add-ons/repeater-field/ and a bit more coding/configuration to make it work.
Hi Ashley,
Thanks for this tutorial. It was a lot of help.
For those Genesis users, they’ll need to create a custom template. You can get my template as an example here: https://gist.github.com/bradonomics/c2e1d8f006e9e60d0e18. There’s also a link to the Single Post Template plugin in the description to get that template to work.