I’m trying to create a custom post type and would like that post type to have two content blocks. So that in the loop I can ask for content-1 and content-2 separately.
I can write the code just having the hard time figuring out how I should start, i.e., is there a way to add another “post content” block easily. Should I remove the existing content block and use two custom fields (can custom fields have the Kitchen Sink?)?
Thanks!
I would keep the primary content field and add a metabox + a secondary instance of the wp editor (using the handy
wp_editor
function).Custom field values are stored in the database as
LONGTEXT
, so they can handle just about anything you wish to throw at them.A class to wrap everything up. There’s a few constants here we’ll use later.
To add the meta box, hook into
add_meta_boxes_{{YOUR_POST_TYPE}}
. I’m just going to use pages for this example. Change the value of theTYPE
constant in the class to make it work for a custom post type.The meta box callback is also included above. It just spits out a nonce for us to validate as well as the editor field using
wp_editor
.Now we just need to hook into
save_post
and save things. We’ll check to make sure we’re on the right post type. Then validate the nonce and check if the current user has permission to edit the post, then it’s just a matter of callingupdate_post_meta
ordelete_post_meta
as appropriate. The only other thing of note here is that I check to see if the current user can post unfiltered HTML. If they can, I’ll just let whatever go through in the meta box. If not, better run it throughwp_filter_post_kses
.To fetch this on the front end, you just need to do
echo get_post_meta($post->ID, '_secondary_content', true);
somewhere in the loop. But it might be nicer to include a wrapper function in our class.Now you can just do
Secondary_Content::content($post);
to fetch things.A final note: this content won’t get anything nice like
wpautop
or the like. If you want to do that, you’ll need to add that as a filter to the final output.The end result:
All of the above as a plugin.