Hire Now!
Whether you’re looking to launch your brand, showcase your portfolio, or open an online store, we’re here to bring your ideas to life.
Custom Post Types (CPTs) are one of the most powerful features of WordPress. They allow you to extend the functionality of your website by adding new types of content beyond the default options (posts, pages, etc.). With CPTs, you can manage unique content such as portfolios, testimonials, events, or products in a structured and organized way.
This guide explains how to create, register, and manage custom post types in WordPress.
Custom Post Types are content types you can define to suit your specific needs. WordPress comes with built-in post types like:
Custom Post Types let you go beyond these defaults to manage unique content structures.
1- Add the following code to your theme’s functions.php
file or a custom plugin:
function create_custom_post_type() {
$args = array(
'labels' => array(
'name' => 'Portfolios',
'singular_name' => 'Portfolio',
),
'public' => true,
'has_archive' => true,
'supports' => array('title', 'editor', 'thumbnail', 'custom-fields'),
'rewrite' => array('slug' => 'portfolios'),
);
register_post_type('portfolio', $args);
}
add_action('init', 'create_custom_post_type');
2- Save the file and refresh your WordPress dashboard. You’ll see a new menu item for “Portfolios.”
if (have_posts()) :
while (have_posts()) : the_post();
$custom_field = get_field('your_field_name');
echo '<p>' . $custom_field . '</p>';
endwhile;
endif;
WordPress uses specific templates for displaying CPTs:
single-{post-type}.php
: Template for individual posts of the CPT.archive-{post-type}.php
: Template for the CPT archive page.1- Create single-portfolio.php
in your theme folder.
2- Add custom HTML and PHP code to display CPT content:
<?php get_header(); ?>
<div class="portfolio-content">
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
</div>
<?php get_footer(); ?>
3- Save the file and refresh a Portfolio post to see the custom template in action.
1- Modify the CPT registration code to include taxonomy support:
'taxonomies' => array('category', 'post_tag'),
2- Use plugins like Custom Post Type UI to create custom taxonomies for better organization.
'show_in_rest' => true,
Custom Post Types are invaluable for creating tailored content solutions in WordPress. Whether you’re a developer or a non-technical user, CPTs allow you to expand your site’s functionality and better organize content.
Start creating your Custom Post Types today to enhance your WordPress site’s flexibility and scalability.
Website security is a top priority for WordPress site owners. From brute force attacks to malware, threats are every…
Affiliate marketing is a proven way to monetize your website and drive additional revenue streams. WordPress affilia…
Integrating secure and efficient payment gateways is essential for any e-commerce site. WordPress payment gateway pl…
WooCommerce is one of the most popular platforms for building online stores, but its functionality can be significan…
No results available
ResetDiscussion
Get the latest news about our updates and discounts
Copyright © 2024 - CollectWP
Powered by DeoPixel with love & passion 💪
Content Harmony: On-Site Data