How to How to Custom Post in WordPress

Creating a custom post type in WordPress allows you to organize and display specific types of content (e.g., products, testimonials, portfolio items) separately from regular posts or pages. Here’s a step-by-step guide:


Method 1: Using a Plugin (Beginner-Friendly)

  1. Install a Plugin for Custom Post Types
    • Go to Plugins > Add New.
    • Search for Custom Post Type UI or similar plugins.
    • Install and activate the plugin.
  2. Create a Custom Post Type
    • Navigate to CPT UI > Add/Edit Post Types.
    • Fill in the required fields:
      • Post Type Slug: e.g., portfolio (lowercase, no spaces).
      • Plural Label: e.g., Portfolios.
      • Singular Label: e.g., Portfolio.
    • Configure settings, such as:
      • Enable archives.
      • Add support for features like title, editor, featured image, etc.
    • Save the post type.
  3. Display the Custom Post Type
    • The new custom post type will appear in your WordPress admin menu. You can create and manage content like regular posts.
  4. Customize Frontend Display
    • Use a page builder like Elementor, or manually edit your theme files to display the custom post type.

Method 2: Adding Code to Your Theme (Advanced)

For more control, you can register a custom post type using code. Add the code to your theme’s functions.php file or a custom plugin.

  1. Add the Code
function my_custom_post_type() {
    $labels = array(
        'name'               => _x( 'Portfolios', 'post type general name' ),
        'singular_name'      => _x( 'Portfolio', 'post type singular name' ),
        'menu_name'          => _x( 'Portfolios', 'admin menu' ),
        'name_admin_bar'     => _x( 'Portfolio', 'add new on admin bar' ),
        'add_new'            => _x( 'Add New', 'portfolio' ),
        'add_new_item'       => __( 'Add New Portfolio' ),
        'new_item'           => __( 'New Portfolio' ),
        'edit_item'          => __( 'Edit Portfolio' ),
        'view_item'          => __( 'View Portfolio' ),
        'all_items'          => __( 'All Portfolios' ),
        'search_items'       => __( 'Search Portfolios' ),
        'not_found'          => __( 'No portfolios found.' ),
        'not_found_in_trash' => __( 'No portfolios found in Trash.' )
    );

    $args = array(
        'labels'             => $labels,
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array( 'slug' => 'portfolio' ),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => false,
        'menu_position'      => null,
        'supports'           => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' )
    );

    register_post_type( 'portfolio', $args );
}
add_action( 'init', 'my_custom_post_type' );
  • Refresh Permalinks
    • After adding the code, go to Settings > Permalinks in the WordPress dashboard and click Save Changes to refresh the permalinks.
  • Customize Frontend Templates
    • WordPress uses specific templates to display custom post types:
      • single-{post_type}.php for single items.
      • archive-{post_type}.php for archives.
    • Example: For a post type called portfolio, create single-portfolio.php and archive-portfolio.php in your theme directory.
  • Query and Display
      • Use a query in your theme to display custom post types:
    $args = array(
        'post_type' => 'portfolio',
        'posts_per_page' => 10,
    );
    $portfolio_query = new WP_Query( $args );
    
    if ( $portfolio_query->have_posts() ) {
        while ( $portfolio_query->have_posts() ) {
            $portfolio_query->the_post();
            the_title();
            the_content();
        }
        wp_reset_postdata();
    } else {
        echo 'No portfolios found.';
    }
    

Additional Tips

Use ACF (Advanced Custom Fields) to add custom fields to your post type for more functionality.
Use Taxonomies to organize your custom post types. Register custom taxonomies using code or a plugin.

Would you like help with creating templates or setting up specific functionality for your custom post type?

2 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x