TIP&HACK

WordPress Tips – Tips for advanced users : Creating a WordPress custom post type

Creating a WordPress custom post type

Running a WordPress blog sometimes requires more than the standard post formats, especially when you want to highlight specific themes like projects, artwork, or reviews. This is where Custom Post Types (CPT) come in handy! In this post, I will introduce how to create and manage custom post types in WordPress.

advertisement
advertisement

Creating a Custom Post Type

Using a Plugin

If you prefer not to work directly with code, I recommend using a plugin called 'Custom Post Type UI'. This plugin allows you to create custom post types through an intuitive UI.

  1. In the WordPress admin panel, go to 'Plugins > Add New', search for 'Custom Post Type UI', then install and activate it.
  2. Once installed, navigate to 'CPT UI > Add/Edit Post Types' and input the required variables.
    • Post Type Slug: A unique identifier in one word, for example, 'work'.
    • Plural Label: A plural label, e.g., 'Works'.
    • Singular Label: A singular label, e.g., 'Work'.
    • Has Archive: Set to 'True' for an archive page.
    • Menu Icon: Choose your desired icon.

Registering via Code

If you are confident with coding, you can directly register using the register_post_type() function in the functions.php file.

function register_custom_post_type() {
    register_post_type('work',
        array(
            'labels'      => array(
                'name'          => __('Works'),
                'singular_name' => __('Work'),
            ),
            'public'      => true,
            'has_archive' => true,
            'menu_icon'   => 'dashicons-admin-tools',
        )
    );
}
add_action('init', 'register_custom_post_type');

With this code, you can create a custom post type named 'Work' and display it on the WordPress admin panel.

Creating a Custom Taxonomy

For better organization, adding a custom taxonomy can be beneficial.

  • In 'Custom Post Type UI', go to 'CPT UI > Add/Edit Taxonomies'.
    • Taxonomy Slug: 'work_categories'.
    • Plural Label: 'Work Categories'.
    • Singular Label: 'Work Category'.
    • Attach to Post Type: 'Work'.

This allows you to create a taxonomy called 'Work Categories', making it easier to categorize posts.

Writing and Displaying Content for Custom Post Types

Writing a Custom Post

To write a new custom post, go to 'Work > Add New' in the WordPress admin. After entering the title and content, adding images, and setting taxonomy, you’re ready! Don’t forget to preview before publishing!

Creating an Archive Page

To create an archive page for the custom post, you need to create a file named 'archive-work.php' in your theme directory.

<?php get_header(); ?>

<div class="content-area">
    <main class="site-main">
        <?php if ( have_posts() ) : ?>
            <header class="page-header">
                <h1 class="page-title">Work Archive</h1>
            </header>
            <?php while ( have_posts() ) : the_post(); ?>
                <article id="post-<?php the_ID(); ?>" <?php post_class(); ?> >
                    <header class="entry-header">
                        <?php the_title( '<h2 class="entry-title">', '</h2>' ); ?>
                    </header>
                    <div class="entry-content">
                        <?php the_content(); ?>
                    </div>
                </article>
            <?php endwhile; ?>
        <?php else : ?>
            <article class="no-posts">
                <h2>No works found</h2>
            </article>
        <?php endif; ?>
    </main>
</div>

<?php get_sidebar(); ?>
<?php get_footer(); ?>

Adding Custom Post Types to Menus

In 'Appearance > Menus' in the WordPress admin, add your favorite custom post type ('Work') to the menu. This way, visitors can easily access it.

Querying Custom Post Types with WP_Query

Using the WP_Query class, you can fetch and display custom post types that meet specific criteria.

$args = array(
    'post_type' => 'work',
    'posts_per_page' => -1,
);
$query = new WP_Query( $args );

if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        the_title();
        the_content();
    }
    wp_reset_postdata();
}

By utilizing these methods, you can create and manage custom post types in WordPress more effectively! Enjoy the freedom it brings, whether for a cute blog or a professional site!

Copied title and URL