Selecting Post Formats
Support › MB Frontend Submission › Selecting Post FormatsResolved
- This topic has 4 replies, 2 voices, and was last updated 4 years, 8 months ago by
Rebecca Robertson.
-
AuthorPosts
-
March 3, 2021 at 9:05 PM #24697
Rebecca Robertson
ParticipantWhen adding a new post on the frontend of the site, I need to be able to select the post format the same way I can on the backend. How would I go about creating a form element for doing that? ACF has the option to choose Format in the taxonomy field, but I can't find anything similar in Meta Box other than with conditional logic, which doesn't help if I can't select a post format in the first place.
March 3, 2021 at 11:29 PM #24700Long Nguyen
ModeratorHi,
Meta Box has not supported a field to select the post format yet. But we can create a custom field
radiowith options like WordPress post formats. Sample code below:$meta_boxes[] = [ 'title' => __( 'Post Meta', 'your-text-domain' ), 'id' => 'post-meta', 'fields' => [ [ 'name' => __( 'Post Format', 'your-text-domain' ), 'id' => $prefix . 'post_format', 'type' => 'radio', 'options' => [ 'gallery' => __( 'Gallery', 'your-text-domain' ), 'aside' => __( 'Aside', 'your-text-domain' ), 'status' => __( 'Status', 'your-text-domain' ), 'link' => __( 'Link', 'your-text-domain' ), 'image' => __( 'Image', 'your-text-domain' ), 'quote' => __( 'Quote', 'your-text-domain' ), 'video' => __( 'Video', 'your-text-domain' ), 'audio' => __( 'Audio', 'your-text-domain' ), 'chat' => __( 'Chat', 'your-text-domain' ), ], 'inline' => false, ], ], ];Use the action hook rwmb_{$field_id}_after_save_field to set post format after saving the custom field.
add_action( 'rwmb_post_format_after_save_field', function( $null = true, $field, $new, $old, $object_id ) { set_post_format( $object_id, $new ); }, 10, 5 );Ok, then we can use the function get_post_format() to get the post format as default feature post format.
March 13, 2021 at 3:39 AM #24891Rebecca Robertson
ParticipantThis is the part I'm confused about. Where do I put action hook and function?
March 14, 2021 at 2:51 PM #24905Long Nguyen
ModeratorHi Rebecca,
You can add the code to the file functions.php in the theme/child theme folder or use the plugin Code Snippets https://wordpress.org/plugins/code-snippets/.
March 14, 2021 at 6:35 PM #24907Rebecca Robertson
ParticipantThank you.
-
AuthorPosts
- You must be logged in to reply to this topic.