Hi there! Have you ever thought to have another field in the wordpress post edit page, for example, an image field to set a thumbnail for that post, a price field to set the price of the houses you are showing or, who knows, just a subtitle field to place a text down the post title?
Whatever you have imagined now is possible in wordpress 3.0+ using a few plugins and… coding a bit. Let’s start by downloading the needed plugins:
More Types
More Taxonomies
More Fields
All of them are independent but can work together very well. I don’t want this post to be so long so, I’m gonna let you install those plugins and check them a bit until you realize how they work.
More Fields lets you, as I’ve said before, add new fields to a post. You can add new boxes and inside them new fields of any type, a text, a textarea, radio button, list, etc. You can configure this very easily in the admin panel and doesn’t need to be explained.
More Types lets you add more types of post, so you will have a post that will be called differently than a post, and, depending on which fields or taxonomies you set to it, it will also act a bit different. For example, imagine we make a custom textarea called description and hide the edit box for that type. Instead of having a post content that type of post will have a description field, acting differently.
That example may seem a bit stupid, so I’m gonna show another much more interesting. What if I want to add cars to my blog? Yes, I want to list cars with their photo, price, provider, model and year of construction. They are not blog posts, but they will act as if they were for most things.
Adding our own customized type to wordpress
First, we make a new type using the More Types plugin. We will call it “Cars”, take attention to the name in singular, whatever you set will be what we will use in the code, in this example we are going to use “Car”.
Now, we should have something like this:
You can try to add a car now, it will be like a post, but called car, thats all at this moment. If you do that you will notice something… the car post is not shown in the blog index. How is that possible? Well, it seems that only the default post type is shown by default, so its time to get to our theme and set this in functions.php:
add_filter( 'pre_get_posts', 'my_get_posts' ); function my_get_posts( $query ) { $query->set( 'post_type', array( 'post', 'car' ) ); return $query; }
With that done, we are saying wordpress to add also our cars to the database queries. Now, we want to add prices and all that stuff to those cars, and we want to add that in a way that all of them will be shown same way that we can edit in the future and also make non programmers people to use it.
Its time to make custom fields. Lets say we make a box called “Car details”, and inside it we add the fields Price, Model, Year, etc. Now we set that this box will be shown only in the car type post, so normal posts will not have these custom fields. Pay attention to what you set in the “Custom field key”, since that is used to access it in the code.
Now, we are going to functions.php to add the function that will show our cars in the way we want:
<?php function printCarDetails(){ $custom_fields = get_post_custom(); ?> <img class="mainPhoto" src="<?php echo $custom_fields['image'][0]; ?>" alt="<?php echo $custom_fields['model'][0]; ?>"/> <table class="carDetails"> <tr><td><strong>Price</strong></td><td><?php echo $custom_fields['price'][0].'€'; ?></td></tr> <tr><td><strong>Constructor</strong></td><td><?php echo $custom_fields['constructor'][0]; ?></td></tr> <tr><td><strong>Model</strong></td><td><?php echo $custom_fields['model'][0]; ?></td></tr> <tr><td><strong>Year</strong></td><td><?php echo $custom_fields['year'][0]; ?></td></tr> </table> <a class="more-link" href="<?php the_permalink(); ?>">More details</a> <div class="clear"></div> <?php } ?>
Ok, now we have a function that will show our car details. Notice that the cars will also have a long description, we will use the post content field for that. But our custom fields will let us show the cars in a list differently than if they were posts and without having to do it manually in each car-post.
Now, we only need to call this function in the index.php and the rest of the listings (category.php, archive.php, tag.php, etc.), and also, we need to differentiate between a normal and a car post. So we will use get_post_type($post) to check the type:
<div class="post_content"> <?php if (get_post_type($post)=='car'): printCarDetails(); else : the_content("Read more"); endif; ?> </div>
That’s it. Now we have cars in our blogs and any person can add them without knowing about programming, also, if we want to change how they are shown we only need to edit one function.
I’ve not mentioned taxonomies in this post but once you get the idea, I don’t think will be hard for you to check how to make new taxonomies. For example, we can make a taxonomy called “Model”, and add it to our cars post hiding the category taxonomy to them. So, instead of having categories, cars will have models. Just an idea.
This is a nice article..
Its very easy to understand ..
And this article is using to learn something about it..
Thanks a lot..!