32. Custom Post Types and Custom Fields in WordPress

WordPress is an incredibly flexible platform that allows users to extend its functionality beyond standard content like posts and pages. Two powerful features that enable this extension are Custom Post Types (CPTs) and Custom Fields. They are essential for creating an advanced WordPress website as they allow you to customize and organize content in an effective and structured way.

What are Custom Post Types?

Custom Post Types are essentially new content types that you can create and customize to meet your website's specific needs. By default, WordPress comes with some pre-defined post types, such as 'post', 'page', and 'attachment'. However, when creating a CPT, you can have a post type for anything you want, such as 'Products', 'Testimonials', 'Portfolio' and more.

Creating Custom Post Types

There are two main ways to create CPTs in WordPress: programmatically, through code, or using a plugin. The programmatic approach involves the register_post_type() function, which you add to your theme's functions.php file or in a specific plugin for your site's features.

function create_custom_post_type() {
    $args = array(
        'labels' => array(
            'name' => __('Products'),
            'singular_name' => __('Product')
        ),
        'public' => true,
        'has_archive' => true,
        'supports' => array('title', 'editor', 'thumbnail'),
        'rewrite' => array('slug' => 'products'),
    );

    register_post_type('product', $args);
}
add_action('init', 'create_custom_post_type');

Plugins like 'Custom Post Type UI' offer a graphical interface for creating and managing CPTs without the need to write code.

What are Custom Fields?

Custom Fields, also known as custom fields, allow you to add additional information to posts. For example, if you have a 'Product' CPT, you may want to add fields like 'Price', 'Color', 'Size', among others. WordPress offers a simple way to add custom fields through the default interface, but for finer control and a better user experience, many developers opt for plugins like 'Advanced Custom Fields (ACF)' or 'CMB2'.

Using Custom Fields

To add custom fields manually, you can use the 'Custom Fields' box in the post edit screen, but this is not the friendliest approach for non-developer users. Using the 'Advanced Custom Fields' plugin, you can create custom fields with a much more user-friendly and powerful interface.

For example, to add a price field to a product, you would create a new field group in ACF and assign it to the CPT 'Product'. You can define the field type (text, number, selection, etc.), user instructions and other relevant settings.

Displaying Custom Fields

After creating and assigning custom fields to your posts, the next step is to display them on the front-end of your website. This usually involves editing your theme's template files and using functions like get_post_meta() to retrieve the values ​​of custom fields.

<?php
$post_id = get_the_ID();
$price = get_post_meta($post_id, 'price', true);
echo '<div>Price: ' . esc_html($preco) . '</div>';
?>

With ACF, you can use your own functions to retrieve and display custom fields even more simply:

<?php the_field('price'); ?>

It's important to ensure that information is displayed cleanly and professionally on your website, maintaining design consistency and good user experience.

Final Considerations

Custom Post Types and Custom Fields are extremely powerful tools for creating an advanced website on WordPress. They allow you to go beyond standard posts and pages by creating custom content types and fields to store and display specific data in an organized and efficient way. By mastering these features, you can create complex, feature-rich websites that meet your users' exact needs.

To ensure you get the most out of CPTs and Custom Fields, it's important to carefully plan your site structure and understand how these features fit into the overall architecture of your content. With practice and experience, you will become proficient at adapting WordPress to meet any project requirement, creating websites that not only look great, but are also functional and easy to use.

As you advance in WordPress development, remember that the W communityordPress is vast and collaborative. There are plenty of resources, tutorials, and forums where you can learn more and get support when you need it. With these tools in hand, you're well-equipped to create dynamic, personalized websites that will make you or your business stand out in the digital world.

Now answer the exercise about the content:

Which of the following statements about Custom Post Types (CPTs) in WordPress is true?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Portfolio and gallery creation 48

Next page of the Free Ebook:

Portfolio and gallery creation

Estimated reading time: 4 minutes

Download the app to earn free Certification and listen to the courses in the background, even with the screen off.

+ 9 million
students

Free and Valid
Certificate

60 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video and ebooks