One of the sweetest things in WordPress are custom post types. You can create your very own special layout for them, pull content in a different way that the regular theme this way.
However, custom posts can be improved with custom fields. Personally, I hate the default custom fields in WordPress, and I prefer to hide them with screen options. I am too busy to remember the meta keys for this and that if they’re not straight forward. Plus, sometimes I just might want more.
Nowadays, many themes come with their own set of custom post types – such as Slider posts, Testimonials, Products, and so on.
If you want to customize that post type further, without messing around with the template there is a neat “toolbox” you can use.
In this toolbox would be the following plugins:
- More Fields;
- Create your nice looking custom fields with More Fields
Using More Fields to create custom fields is a walk in the park. Once you have installed the plugin, go to Settings> More Fields in your admin menu.
Create the custom field box (it will look like your Categories box. The click on Add New Field to start creating the actual custom field.
Enter all the data and don’t forget to add the custom field key. Copy it and paste it somewhere if you don’t have a good memory.
- Display the content of your new custom field in a sidebar widget
Now, install Shortcode Exec PHP. Go to either Tools> Shortcode Exec PHP or Settings>Shortcode Exec PHP (it creates a menu link in both, but they’re the same thing).
The first thing you need to do is generate the custom field shortcode by entering a name for it in the text field between [ these brackets ]. Next, you need to enter the PHP code that will pull your custom field values.
Let’s say you wanted to create an Additional Information box about your product.
You created a new input box called Additional Information. And now you want to specify the size of your product. The new field is called Size of Product and it has the custom field key “product_size”.
What you need to put in the Shortcode Exec PHP is as follows:
[product-size]
Then the code:
global $wp_query;
$postid = $wp_query->post->ID;
echo ‘<strong>’; echo _e(‘Product Size: ‘);
echo ‘</strong>’;
echo get_post_meta($postid, ‘product_size’, true);
echo ‘<br/>’;
Simple enough, right? Now, go to Appearance > Widgets. Pull a Text widget to your sidebar and put in your new shortcode [product-size].
Go to a post, enter the product size info in your new custom field and TADAAA! Refresh your page to see the contents nicely displayed in the widget.
Now, let’s say you have some products where entering size would be just ridiculous, so you want to display the “Product Size: ” bit only if it applies to that product.
To do this, create a new custom field in the Additional Information input box. Call it something like “Enter size?”. Enter a caption such as “Mark this checkbox if you want to disclose the size of the product”. And from the list of field types select checkbox. Put the custom field key something like “psize_yes_no”.
Now back to Shortcode Exec PHP. You must edit the PHP code behind your shortcode, to include the condition “Only display Product Size if product size applies”, right?
A piece of cake again:
global $wp_query;
$postid = $wp_query->post->ID;
if (get_post_meta($postid, ‘psize_yes_no’, true)) {
echo ‘<strong>’; echo _e(‘Product Size: ‘);
echo ‘</strong>’;
echo get_post_meta($postid, ‘product_size’, true);
echo ‘<br/>’;}
The Product Size bit will only appear if you mark that checkbox.
- Display the content of your custom fields in the post/page without editing the template
Of course, you can always integrate the code above in a page or post template, if you know what you’re doing.
If you’d rather not mess around with templates, you can always get the Custom Shortcode Sidebars plugin. This allows you to create a custom sidebar, which you can pull into your post or page with a shortcode. So, create your new sidebar using this plugin. Copy the shortcode. Go to your post/page and paste in the shortcode.
Then go to widgets and move your Additional Information widget from the regular sidebar into the new custom-made one.
And that’s it. Any widgets you add in this sidebar will be displayed inside your post.
PS: Make sure you mark this checkbox “Execute shortcodes in (sidebar) widgets“ in the Shortcode Exec PHP Admin panel.