The Must Have for WordPress
The Must Have for WordPress
×
  • Home
December 5, 2019
WordPress

Set custom WordPress post format for custom post type

by Martin Jost
No comments
2 min Reading Time
1.68 K Views
5 Score
3 Likes
Share on Facebook Tweet on Twitter Share on Linkedin Pin on Pinterest Send via E-Mail Send via WhatsApp

If you’re working with custom post types in WordPress, you may have noticed that all post formats for the default post type automatically apply to custom post types as well. Depending on the type of post, this does not always make sense to use the post formats such as “image”, “video”, “quote”, “link”, “gallery” or “audio”.

Which post formats are available?

  • aside – Typically styled without a title. Similar to a Facebook note update..
  • chat – A chat transcript.
  • gallery – A gallery of images.
  • link – A link to another site.
  • image – A single image.
  • quote – A quotation.
  • status – A short status update, similar to a Twitter status update.
  • video – A single video or video playlist.
  • audio – An audio file or playlist.

Then of course there is the default post format, which is always selected if you have not defined any other. The most common post formats in WordPress are default, gallery, quote, video and audio. More information about the WordPress post formats can be found here.

How do I add custom post formats?

To add additional formats in addition to the default post format, you have to extend your theme functions.php file with the following code snippet:

Within the array you can define the new post formats to be supported.

How do I add my own post formats depending on the custom post type?

The problem is that all post formats defined for the default post type automatically apply to all other custom post types. For example, post formats like chat, quote or audio are less useful in a recipe post type, but can be still selected in the WordPress backend for this custom post type. This can confuse the user if, for example, the post formats for this post type are not formatted and displayed correctly.

The following procedure will help us with this dilemma. First, generally we do not define post formats, like in the example above. We restrict support for custom post formats depending on the custom post type.

Define post formats ONLY for default post type

In the following example we define the supported post formats exclusively for the default WordPress post type. Add this code snippet, eg in the functions.php of your theme:

Add post formats for custom post types only

Similar to the example above, we now define the supported post formats only for our custom post types. In this example, I only added the video and gallery post formats for the custom post type “recipe”.

The WordPress filters load-post.php and load-post-new.php ensure that our function will be applied when editing or adding a post.

Last updated: December 5, 2019

Set custom WordPress post format for custom post type Score: 5 / 5 (2votes)
Share on Facebook Tweet on Twitter Share on Linkedin Pin on Pinterest Send via E-Mail Send via WhatsApp
Post types
The Author
Published by
Martin Jost

View all posts by Martin Jost

Related Posts
C
PHP Convert HEX color to RGBA color code in PHP

While working with theme or plugin options, in some cases we need to convert a hex (hexadecimal) ...

Read More
A
WordPress Add custom classes to the WordPress BODY tag

Anyone who develops their own theme with a customizable layout or settings a theme needs to add ...

Read More
M
CSS Minify or compress CSS in PHP

For developers of plugins or themes, it is important to provide the included CSS (stylesheets) f ...

Read More
Read more
R
Previous Post
Remove date / time from WordPress comments
A
Next Post
Add custom classes to the WordPress BODY tag
Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Imprint
  • Privacy Policy
© 2016-2023 - wphave - The Must Have for WordPress
Login

Lost your password?

RequiredEssential cookies enable basic functions and are necessary for the proper functioning of the website.
  • Name: Cookie Status
  • Provider: Owner of this website
  • Purpose: Is necessary to check whether the user's web browser allows cookies.
  • Privacy Policy: https://wphave.com/privacy-policy/
  • Cookie Name(s): cookie_status
  • Duration: 1 Minute
  • Name: Cookie Consent
  • Provider: Owner of this website
  • Purpose: Is necessary to check whether the user agrees to allow opt-in cookies or not.
  • Privacy Policy: https://wphave.com/privacy-policy/
  • Cookie Name(s): cookieconsent_analytics, cookieconsent_marketing, cookieconsent_status
  • Duration: 1 Year
  • Name: Like Button
  • Provider: Owner of this website
  • Purpose: Is necessary to check whether the user has already gave a like for a specific post.
  • Privacy Policy: https://wphave.com/privacy-policy/
  • Cookie Name(s): like_btn_{POST_ID}
  • Duration: 21 Days
  • Name: Post Rating
  • Provider: Owner of this website
  • Purpose: Is necessary to check whether the user has already rate a specific post.
  • Privacy Policy: https://wphave.com/privacy-policy/
  • Cookie Name(s): rating_{POST_ID}
  • Duration: 7 Days
AnalyticsStatistics cookies collect information anonymously. This information helps us to understand how our visitors use our website.
  • Name: Google Analytics
  • Provider: Google LLC
  • Purpose: Google cookie for website analysis. Generates statistical data on how the visitor uses the website.
  • Privacy Policy: https://policies.google.com/privacy
  • Cookie Name(s): _ga, _gat, _gid
  • Duration: 2 Years