- Custom Layouts Overview
- Cannot edit Custom Layouts / Custom Layouts having 404 error?
- Custom Header
- Custom Footer
- Site Builder – Hooks
- Custom 404 Page
- How to Translate Custom Layouts with WPML?
- Display Settings of Custom Layouts in Astra Pro
- Inside Page/Post Content Custom Layouts
- Quick admin bar navigation to edit custom layout & page header
- Astra WooCommerce Mini Cart Shortcode
- WooCommerce Module Overview
- How to Design a Product Catalog Page or Shop Page Using WooCommerce Module in Astra?
- Single Product WooCommerce
- Checkout Page WooCommerce
- Colors & Background options for WooCommerce
- Typography Options for WooCommerce
- How to Add WooCommerce Mini Cart in Header? (Old Astra Header)
- Off-Canvas Sidebar for WooCommerce Shop Page
- Quick View for WooCommerce Products
- How to Disable EDD Inbuilt Styling?
- How to Add Download Archive Pages to the Menu When Using Astra with EDD?
- How to Add EDD Cart in Header? (Old Astra Header)
- How to Display a Mini Cart Anywhere Using Shortcode? (Astra and EDD)
- EDD – Easy Digital Downloads Module Overview
- General – EDD Module Options
- Product Archive – EDD Module Options
- Single Product – EDD Module Options
- Checkout Page – EDD Module Options
- Colors & Background options for EDD
- Fix for – The PCLZIP_ERR_BAD_FORMAT (-10) Error
- Fix for – Parse error: syntax error, unexpected T_FUNCTION
- How to fix Fatal Error / White Screen of Death?
- Fix for- cURL error 51: SSL: no alternative certificate subject name matches target host name ‘websitedemos.net’
- Getting error – The package could not be installed. The theme is missing the style.css stylesheet?
- ‘The preview could not be loaded’ Pop Up with Astra and Elementor
- Troubleshooting Steps ( with Health Check & Troubleshooting plugin )
- How to Deal with Update Issues in Astra Theme and Astra Pro Addon?
- Blog Featured Image Size Not Working / Error in Image Processing Library
- How to Clear Astra’s Cache?
- How To Reset WordPress Installation?
- XMLReader Support Missing – Starter Templates
- cURL Support Missing – Starter Templates
- Required File Permissions Missing – Starter Templates
- Disable Debug Mode – Starter Templates
- Update Required Plugins – Starter Templates
- How to Import A Complete Site With Starter Templates?
- Starter Templates — Basics and FAQs
- How to Import Single Page With Starter Templates?
- Starter Templates with Other Themes
- How to Translate Astra Theme / Plugins in Your Own Language using GlotPress?
- How to Turn Astra Multilingual with WPML?
- How to Translate Custom Layouts with WPML?
- How to Translate Astra Strings with WPML?
- How Translations can be Manually Exported and Uploaded to the Site?
- How to Turn Astra Website Multilingual with Polylang?
- How to Translate Categories, Tags, and Astra Strings with Polylang
- How to Turn Astra Website Multilingual with TranslatePress?
- How to translate the WooCommerce string?
- Astra theme string translation for WooCommerce
- How to Change the Default Astra Strings
- Using Hooks in Astra
- How to Change the “Scroll To Top” Icon in Astra?
- Astra Pro WP CLI Commands
- How to Add Custom PHP Code?
- How to Disable the Loading of Astra’s Default Font File? (Astra.woff)
- Disable Featured Image on Posts, Pages, or Other Post Types
- Change Sidebar Widget Title Heading Tag
- Disable Astra’s Native AMP Functionality
- Disable All Meta Settings of Page/Post by Default
- How to Display “Last Updated” instead of “Published” Date
- How to Change Previous and Next Link Text from a Single Blog Post?
- How to Remove Featured Image Link on Archive Page?
- Filter to Remove Link From Featured Images on Blog Page
- Blog Featured Image Size Not Working / Error in Image Processing Library
- How To Change Navigation Links Text for a Blog Archive?
- How to Display the Post Category as a Related Posts Title?
- Change “Leave A Comment” title tag
- Customizing Social Profile Links for Individual Authors in Single Posts
- Change Woocommerce Out of Stock Text
- How to Disable Product Quantity (Plus-Minus) Buttons?
- How to Modify/Change the Quick View text?
- Filter to Add Global Button Settings Support for WooCommerce Buttons
- Change the “Shopping Cart” Text for WooCommerce & EDD Mobile Header Cart
- Fix Woocommerce Cart Becoming Transparent With Header Builder
- Restrict Search Results to WooCommerce Products Only
- How To Hide Quantity Number When the Woocommerce Cart Is Empty?
- Remove Astra Customization for WooCommerce
- Remove Woocommerce Product Category Archive Title
- How to Change Website Logo Destination URL
- Remove Primary Navigation Menu with Hook
- Change the Astra Header Breakpoint Width
- How to Disable Primary Header?
- Add Title attribute to Header Background Image as a Substitute for Alt Text
- How to Change HTML tag for Site Title and Tagline?
- How to Change the Heading Tag for the Page/Post Titles?
- Change the String “Search Results For”
- Change Placeholder for Search Box (Old Astra Header)
- How to Update Responsive Breakpoints for Tablet+Mobile in Astra?
- Fix Swap Sections Not Working on Mobile (Old Astra Header)
- How to Remove Google Fonts Suggestions in Astra Theme?
- Remove default stretched block layout spacing
- How to Change the Logo on Specific Pages?
- How to remove horizontal & vertical gallery layouts from a single product page?
- Introducing New Filter to Enable/Disable Rank-Math Theme Support
- How to Fix the Line Height Unit being converted to “EM”?
- How to Change WordPress Post labels to Projects
- Managing User Roles and Permissions for the Gutenberg Template Library
- Footer Custom Text Helper Strings
- Does Astra support Beaver Themer Plugin?
- Increasing the PHP Memory Limit of Your Website
- How to Disable Header or Footer for a Landing Page or Post?
- Where Does Astra Primary Color Setting Take Effect?
- How to Adjust the Width of Your Sidebar?
- How to Update the Plugin Manually from WordPress Backend?
- Recommended Settings for Elementor and the Astra Theme
- Recommended Settings for Beaver Builder and the Astra Theme
- Astra Pro WP CLI Commands
- Why Is My Logo Blurry?
- How to Update Responsive Breakpoints for Tablet+Mobile in Astra?
- FAQs – Astra Header/Footer Builder
- Elements in Header/Footer Builder With Astra Theme and Astra Pro
- Add Multiple Elements in Header Footer Builder
- How To Create a Header With Astra Header Builder?
- How To Create a Footer With Astra Footer Builder?
- How To Create Mobile Header With Astra Header Builder?
- FAQs – Astra Header/Footer Builder – Existing Customers
- Clone and Delete Elements in Header Footer Builder
- Global Container – Astra Theme
- Boxed – Container Layout
- Content Boxed – Container Layout
- Full Width / Contained – Container Layout
- How to Set the Full-width/Stretched Container Layout in Astra?
- Global Colors – Astra Theme
- Global Typography – Astra Theme
- Typography Improvement for Astra
- Astra Global Color Palette
- Astra Typography Presets
- Blog Overview
- Blog / Archive
- Single Post
- How to Display “Last Updated” instead of “Published” Date
- Display Related Posts on Single Blog Post
- The Recommended Size for Featured Image Upload
- How to remove an Author’s name from a Single Blog Post?
- How to Remove Astra Post Excerpt from the Post Archive
- Add Last Updated or Published Date to Blog Posts
- Enhanced Blog Experience: Explore What’s New in Astra v4.6.0
- Astra – Customize the submenu
- The blank screen in the Customizer area
- How to use the color palette of the Astra theme
- How to Import / Export Astra Customizer Settings
- How to disable logo cropping
- How to Create a Sticky Sidebar for Your WooCommerce Shop Page
- How to use dynamic customizer from Astra 4.0.0
- How to Change the Typography of the Astra Menu
- What is Astra Pro Add on?
- What Is a Child Theme and How To Install It for Astra?
- How to Activate Astra Pro Addon License?
- How to Get License Key of Astra Pro?
- How to Install Astra Pro Plugin?
- Getting Started with Astra Pro Addon Plugin
- Getting error – The package could not be installed. The theme is missing the style.css stylesheet?
- Do Not See License Activation Form for Astra Pro Addon Plugin?
- How to Install Astra Theme?
- Know More about Astra Beta Versions? How to Download and Use?
How to add custom post type with the Astra theme
The Astra theme is famous for its flexibility. In addition, you can even use astra_content_loop() to add your custom content to any existing post type. But sometimes, you just need more. Hence, this document will show you how to create a custom post type that works perfectly with the Astra theme.
You might wonder why you need Custom Post Type (CPT)? Simply put, in some situations adding content to existing post types and templates can’t provide the needed result. Thus, you have the option to add a completely new post type and shape it based on your needs.
Adding Custom Post Type (CPT)
Adding CPTs to your website can bring a lot of advantages. For example, you can add a CPT with a custom layout and functionality making it suitable for non-standard content. Also, each CPT has a custom taxonomy that can help you improve the way your content is organized.
To illustrate, we will import the “Businessly” Starter Template and create an additional CPT called Sessions.
The template we will use contains an “Interactive Sessions” page. Each session has some specific information like dates, host, and speaker. We will show you how to create a CPT containing these options. In addition, we’ll set the “Interactive Sessions” page as an archive for displaying Sessions.
Before starting, make sure you have your Astra theme and your Astra Child Theme set. Also, you can import the “Businessly” Starter Template to your website. If you need any assistance importing the template, you can check this document.
Now, let’s show you how to register our Sessions CPT.
Register Custom Post Type
You can register your custom post type and give them any name you want. Also, you can add an unlimited number of CPTs to your website.
To register a CPT you need to add a few lines of code to the functions.php file of your Astra theme. The issue with this is that your CPTs will be removed with every theme update. For this reason, we will register our CPT to the functions.php file of the Astra Child Theme. To explain, the child theme will keep all our registered CPTs and other changes safe when updating the Astra theme. If you still don’t have the child theme installed, check this doc for instructions on how to do it.
Now, to register your CPT follow these steps:
Step 1 – Log in to your website, and navigate to Dashboard > Appearance > Theme File Editor. Here, on the top right side of the screen, select your Astra Child theme;
Step 2 – Next, in the Theme Files list, click on the functions.php to open the file;
Step 3 – Under the existing code in the file, add the following:
// Custom post type function
function astra_custom_post_types() {
// Basic Session Post Type
register_post_type('session', array(
'public' => true,
'rewrite' => array('slug' => 'sessions'),
'labels' => array(
'name' => 'Sessions',
'add_new_item' => 'Add New Session',
'edit_item' => 'Edit Session',
'all_items' => 'All Sessions',
'sinuglar_name' => 'Session'
),
));
}
// Hooking up the function to the Astra Theme
add_action('init', 'astra_custom_post_types');
The register_post_type is a WordPress function used to register post types. To use this function, you just need to pass it two arguments: custom post name, and an array of post type options.
Further, you can extend the code any way you need. For example, some of the additional arguments are:
- Add the Block editor support: ‘show_in_rest’ => true
- Add the CPT archive:‘has_archive’ => true
- Add features to the CPT post editor. In this example we added title field, editor, excerpt, featured image, revisions and custom fields: ‘supports’ => array(‘title’, ‘editor’, ‘excerpt’, ‘thumbnail’, ‘revisions’, ‘custom-fields’)
- dd icon to an admin menu item (dashicon) for your CPT. In our example, we chose a calendar icon. You can check the list of available dashicons and use the one you need by changing the icone name: ‘menu_icon’ => ‘dashicons-calendar’
Now, our CPT code looks like this:
// Extended Session Post Type
register_post_type('session', array(
'public' => true,
'has_archive' => true,
'supports' => array('title', 'editor', 'excerpt', 'thumbnail', 'revisions', 'custom-fields'),
'show_in_rest' => true,
'rewrite' => array('slug' => 'sessions'),
'labels' => array(
'name' => 'Sessions',
'add_new_item' => 'Add New Session',
'edit_item' => 'Edit Session',
'all_items' => 'All Sessions',
'sinuglar_name' => 'Session'
),
'menu_icon' => 'dashicons-calendar',
));
Also, the list of labels can be extended if needed. In addition, you can extend the code even further by adding all your arguments using the $args variable. Here, you would use the following code for registering your CPT:
register_post_type( 'movies', $args );
You can find more details and the full list of available CPT options here.
Now you registered your new CPT and you will notice a matching new menu item in your Dashboard menu.
Update permalink structure
If you created and visited a new CPT post, you’ll notice your website is displaying a “Page Not Found” notification. This happened because the WordPress permalink structure of your website won’t update automatically when you register your CPT.
Thus, any time you add a new CPT, you need to update the permalink structure yourself. To do this, navigate to Dashboard > Settings > Permalinks. Though you can change your permalinks here, it’s not needed – just click on the “Save Changes” button.
Creating Custom Template for Your CPT And Archive
Once you’ve registered your CPT, it’s time to check it out on the front end. Now, you’ll notice that it looks the same as your regular posts. The reason for this is that we haven’t added any custom template for this CPT. For this reason, WordPress is using the default template located in the single.php file.
Also, if you check the Archive page, you will notice that it looks the same as other archive pages on your website. The reason is the same.
To create your custom templates, follow these steps:
Step 1 – Access your website root folder and navigate to your Astra Child theme folder (the default path is: /wp-content/themes/astra-child);
Step 2 – Add separate files for your CPT post and archive. Keep in mind that these files need to be named in the exact format:
- single-{cpt-name}.php
- archive-{cpt-name}.php
Accordingly, we will name our Session CPT files as:
- single-session.php
- archive-session.php
This will provide you with empty templates which you can build any way you want.
Step 2b – Alternatively, make copies of the original files if you want to use the existing Astra template with some modifications. Here, you can copy the following files from your Astra theme folder (the default path is: /wp-content/themes/astra-child):
- single.php
- archive.php
Now add these files to your Astra Child theme folder and rename them to:
- single-session.php
- archive-session.php
This will give you precisely the same template as used by default files. Now you can modify these files and change how your CPT pages look.
These templates will be applied only to your CPT. Keep in mind that these files need to be placed in your Astra Child folder. Contrary, if placed in your Astra theme folder, the theme update will delete your CPT files.
Update the Container Layout for Your CPT
Though many Astra Customizer settings will be applied to your CPTs, when it comes to container layout there are some limitations.
Namely, your CPT will follow the website container layout. But, if you want to set a different layout for your CPT you need to add the following code to your Astra Child Theme functions.php file:
add_filter ( 'astra_get_content_layout', 'custom_post_type_content_layout'); // Update the container layout based on Custom Post Type.
function custom_post_type_content_layout ( $layout ){ // Add your CPT instead of Session. If you want to use this layout for more than one CPT, you can add them all separating each CPT by coma (eg. 'session', 'events').
if ( is_singular( array( 'session' ) ) ) {
$layout = 'boxed-container';
}
return $layout;
}
In the code above, you need to specify the layout option that you want to apply, and CPTs to which it should be applied. You cen use following codes for different layout options:
- Full Width / Streched = ‘page-builder’
- Full Width / Contained = ‘plain-container’
- Content Boxed = ‘content-boxed-container’
- Boxed = ‘boxed-container’
Add Sidebar
Similar to the container layout, the default Sidebar Customizer settings will be applied to your CPTs.
Thus, to add a sidebar to your CPT, you need to set this as a default value for the whole website:
Step 1 – Navigate to Dashboard > Appearance > Customize > Sidebar;
Step 2 – Under the “Default Layout” dropdown, select the “Left Sidebar” or “Right Sidebar” option.
Adding CPT Content
Previously, we showed you how to add support for the Block (Gutenberg) Editor to your CPT. Thus, you can add your post content using Block editor out of the box.
On the other hand, if you prefer using Elementor or Buiver Builder instead, you will need to enable them for your CPT.
To enable Elementor for CPT, follow these steps:
Step 1 – Navigate to Dashboard > Elementor > Settings and click on “General” tab;
Step 2 – Under “Post Types” check the CPTs you wish to edit with Elementor. Click the “Save Changes” button.
To enable Beaver Builder for CPT, follow these steps:
Step 1 – Navigate to Dashboard > Settings > Beaver Builder;
Step 2 – Click the “Post Types” option and check the CPTs you wish to edit with Beaver Builder. Click the “Save Post Types” button.
Astra Loop
The alternative option to adding a CPT is to use the astra_content_loop(). You can use this option to add a CPT template content to existing post types instead of creating a new Custom Post Type.
To do this, you need to add this code to the Astra Child Theme functions.php file:
function add_cpt_content() {
// bail if this current post type is different.
if ( 'cpt' !== get_post_type() ) {
return;
}
?>
<!-- Your custom HTML markup here -->
<p>My custom theme HTML</p>
<?php
}
add_action( 'astra_entry_content_before', 'add_cpt_content' );
Further, if you add your code using this method, any future template changes made by Astra them will not have an influence on the CPT content you added. Thus, there will be no need for you to manually update your content.
We don't respond to the article feedback, we use it to improve our support content.