Video: Adding advanced variables to the custom post type

In the previous movie, we created three basic custom post types. But as you saw, when we went to WordPress Admin, when creating a new post under them, the text displayed inside WordPress still calls all of them just Post. Though, this doesn't look like much right away. It's a bit sloppy and it can also lead to confusion. After all, when you click Add New under Recipes, why does it say create new post? Fortunately, we can fix out this with a few more lines of code in our function. Each of the custom post type definition functions can have a variable called labels.

Learn how to build an advanced portfolio site that showcases various types of content using the free open-source application WordPress. Author Morten Rand-Hendriksen demonstrates creating custom post types, differentiating and classifying content with custom taxonomies, and working with custom post templates. The course also shows how to embed YouTube videos, build index pages, display the latest posts from different custom post types, and hook custom post types into separate themes. Exercise files accompany with the course.

Adding advanced variables to the custom post type

In the previous movie, we created three basic custom post types.But as you saw, when we went to WordPress Admin, when creating a new post underthem, the text displayed inside WordPress still calls all of them just Post.Though, this doesn't look like much right away.It's a bit sloppy and it can also lead to confusion.After all, when you click Add New under Recipes, why does it say create new post?Fortunately, we can fix out this with a few more lines of code in our function.Each of the custom post type definition functions can have a variable called labels.

This is where we put all the labels for a custom post type.This variable can take an array of labels.So we are going to create a new separate array and then call it from withinthe function itself.If you go to your code-snippets, you see here under 03_03, there's an example ofa new array for recipes.It starts with recipe_labels and then it goes down to this end bracket.So, if you copy this, go into your posttypes, scroll to the top, and find yourarguments for the register_post_type, and then paste in the new array directlyabove it, we now have an array we can call from inside a basic function todisplay all the information about the content.

If we look at this array, you will see we have more specific definitionsabout all the text.We have the name of the posttype, Recipes, we have the singular_name Recipe, andthese correspond to the previous label and singular_label.We also have the text for all_ items, so it says All Recipes.We have add_new, Add new recipe, we have edit_item, we have new_item.This stuff just makes it clear inside WordPress Admin what is going on when youare working for the recipes.

So how do we activate this array?Well first, we should take out this singular_label, because we already have arepeated up here, so I will take that whole line out, and then instead of sayinglabel, we will say labels and call the array.The array is called $recipe_ labels, so that's what you put in.And when we now save the posttypes.php file, and go back to WordPress admin, andreload, you will see a subtle change.

We now have all recipes under Recipes, Add new recipe, and in the actual singleview, you see it says Add new recipe rather than saying Add New Post.We can do the exact same thing for Photos and Videos, just because it's a lotof code, I've created it for you, so what you can do if you don't want to dothis manually is simply go to the code -snippets, find it where it says Fullcode for posttypes.php and copy all the code all the way down to the end of the php delimiter.

Copy this, go back to the code editor, under posttypes.php, delete everythingand just paste in this new content.When I save it, reload WordPress Admin, we now have all the new post types andas you can see when we open them, under Photos we have All Photos, Add newphoto, under Videos we have All Videos, and when we select them, it will say Addnew video here as well.Setting up the proper labels for a custom post type not only makes the post typeeasier to use, but also adds a new layer of professionalism to it.

And as you've seen, it's easy to do, which is always a bonus.

There are currently no FAQs about Create an Online Portfolio with WordPress.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.

Already a member ?

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships.
Learn more

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.

Sign up and receive emails about lynda.com and our online training library:

new course releases

newsletter

general communications

special notices

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

new course releases

newsletter

general communications

special notices

Here’s our privacy policy with more details about how we handle your information.