How to Display A Custom Post In WordPress?

17 minutes read

To display a custom post in WordPress, you can follow these steps:

  1. First, you need to create a custom post type. This can be done by adding some code to your theme's functions.php file or by using a custom post type plugin. The code typically looks like this:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
function custom_post_type() {
    register_post_type('custom_post',
        array(
            'labels' => array(
                'name' => 'Custom Post',
                'singular_name' => 'Custom Post'
            ),
            'public' => true,
            'has_archive' => true,
            'supports' => array('title', 'editor', 'thumbnail'),
        )
    );
}
add_action('init', 'custom_post_type');


  1. After creating the custom post type, you can start adding your custom posts. Go to the WordPress admin dashboard and click on "Custom Post" (or whatever name you provided for your custom post type). Then click on "Add New" to create a new post.
  2. Once you have added some custom posts, you need to display them on your website. To do this, you can create a custom page template or modify an existing one. Open your theme folder and locate the template file (often named page.php or single.php) you want to edit.
  3. Inside the template file, find the spot where you want to display the custom post, and add the following code:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
$args = array(
    'post_type' => 'custom_post',
    'posts_per_page' => 5, // number of posts to display
);

$custom_posts = new WP_Query($args);

if ($custom_posts->have_posts()) {
    while ($custom_posts->have_posts()) {
        $custom_posts->the_post();
        // Display the post content or specific fields using template tags
    }
    wp_reset_postdata();
} else {
    // No custom posts found
}


  1. Save your changes and go to the page where you added the custom post display code. You should now see your custom posts being displayed.


Remember to replace 'custom_post' with the actual name you gave to your custom post type.


That's it! You have successfully displayed a custom post in WordPress without using list items.

Best WordPress Books of January 2024

1
WordPress: The Missing Manual: The Book That Should Have Been in the Box

Rating is 5 out of 5

WordPress: The Missing Manual: The Book That Should Have Been in the Box

2
WordPress All-in-One For Dummies (For Dummies (Computer/Tech))

Rating is 4.9 out of 5

WordPress All-in-One For Dummies (For Dummies (Computer/Tech))

3
WordPress Explained: Your Step-by-Step Guide to WordPress (2020 Edition)

Rating is 4.7 out of 5

WordPress Explained: Your Step-by-Step Guide to WordPress (2020 Edition)

4
Professional WordPress: Design and Development

Rating is 4.5 out of 5

Professional WordPress: Design and Development

5
WordPress Plugin Development Cookbook: Create powerful plugins to extend the world's most popular CMS, 2nd Edition

Rating is 4.4 out of 5

WordPress Plugin Development Cookbook: Create powerful plugins to extend the world's most popular CMS, 2nd Edition

6
WordPress 5 Complete: Build beautiful and feature-rich websites from scratch, 7th Edition

Rating is 4.3 out of 5

WordPress 5 Complete: Build beautiful and feature-rich websites from scratch, 7th Edition

7
Building Web Apps with WordPress: WordPress as an Application Framework

Rating is 4.2 out of 5

Building Web Apps with WordPress: WordPress as an Application Framework

8
WordPress for Beginners 2020: A Visual Step-by-Step Guide to Mastering WordPress (Webmaster Series)

Rating is 4 out of 5

WordPress for Beginners 2020: A Visual Step-by-Step Guide to Mastering WordPress (Webmaster Series)


How to create a custom archive page for a custom post type in WordPress?

To create a custom archive page for a custom post type in WordPress, you'll need to follow these steps:

  1. Register your custom post type: Use the register_post_type() function in your theme's functions.php file to register your custom post type. Include the has_archive parameter and set it to true. For example: function custom_post_type() { $args = array( 'label' => 'Custom Post Type', 'public' => true, 'has_archive' => true, // Add other parameters here ); register_post_type('custom_post_type', $args); } add_action('init', 'custom_post_type');
  2. Create a new template file: In your theme folder, create a new PHP file and name it archive-custom_post_type.php. This will be the template used for your custom post type's archive page.
  3. Customize the archive template: Open archive-custom_post_type.php and customize it according to your needs. You can use HTML, PHP, and WordPress functions to display the content you want. You can use a loop inside the template file to display the posts belonging to the custom post type. Here's a basic example:
  4. Assign the new template to the custom post type's archive: In the WordPress Dashboard, go to "Pages" or "Posts" and create a new page. Set its title as desired, e.g., "Custom Post Type Archive," and select the newly created archive-custom_post_type.php template from the "Template" dropdown on the right side.
  5. Save the page and navigate to it in your browser: You should now be able to see your custom post type's archive page with the content and styling you specified in the template file.


Remember to flush your permalinks by visiting "Settings" -> "Permalinks" in the WordPress Dashboard or by resaving your permalink settings to ensure the archive page works correctly.


What is a custom post status in WordPress?

A custom post status in WordPress is a feature that allows users to define and create their own custom statuses for posts, pages, or any other custom post type. By default, WordPress provides draft, pending review, private, published, and trashed as the default post status options.


However, with custom post status, users can create and assign new statuses according to their specific needs. These custom statuses can be used to differentiate the progress or workflow of a post, such as "in progress," "approved," "rejected," "on hold," etc.


Custom post status provides more flexibility and control over content management in WordPress, as it allows users to customize the available options to match their specific requirements.


What is the difference between a custom post type and a regular post in WordPress?

A regular post in WordPress is the default content type that allows you to create and publish blog posts. It has a predefined set of fields such as title, content, author, tags, categories, and publish date. Regular posts are typically used for daily blog updates or news articles.


On the other hand, a custom post type in WordPress allows you to create your own content types with custom fields and settings. This means you can create and manage different types of content that are not blog posts, such as portfolio items, products, events, testimonials, etc. Custom post types provide more flexibility in organizing and displaying content on your website, as they can have unique characteristics, taxonomies, and templates.


In summary, regular posts are primarily used for blog updates and news articles, while custom post types allow you to create and manage different types of content with their own distinct characteristics.

Best WordPress Hosting Providers in 2024

1
Vultr

Rating is 5 out of 5

Vultr

  • Ultra-fast Intel Core
  • High Performance and Cheap Cloud Dedicated Servers
  • 1 click install Wordpress
  • Low Price and High Quality
2
Digital Ocean

Rating is 4.9 out of 5

Digital Ocean

  • Active Digital Community
  • Simple Control Panel
  • Starting from 5$ per month


How to add a featured image to a custom post type in WordPress?

To add a featured image to a custom post type in WordPress, follow these steps:

  1. Register your custom post type: If you haven't already, create a custom post type using the register_post_type() function in your theme's functions.php file or within a custom plugin.
  2. Enable featured image support: Set the thumbnail parameter to true when registering the custom post type. For example:
1
2
3
4
5
6
$args = array(
    // other parameters
    'supports' => array( 'title', 'editor', 'thumbnail' ),
    // other parameters
);
register_post_type( 'your_custom_post_type', $args );


  1. Add the featured image to the post: When editing a custom post type, you should now see the "Featured Image" meta box below the content editor. Click on the "Set Featured Image" button to upload/select an image from your media library.
  2. Save the post: After selecting an image, click the "Set Featured Image" button and then click the "Publish" or "Update" button to save the post.


Now, the featured image will be displayed wherever your theme or plugin template references it. You can typically retrieve the featured image using the get_the_post_thumbnail() function within your template files.


How to add a taxonomy to a custom post type in WordPress?

To add a taxonomy to a custom post type in WordPress, you can follow these steps:

  1. Register the custom post type: If you haven't already, register your custom post type using the register_post_type() function. For example:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
function custom_post_type() {
    $args = array(
        'labels'    => array(
            'name'          => 'Custom Post Type',
            'singular_name' => 'Custom Post'
        ),
        'public'    => true,
        'supports'  => array( 'title', 'editor', 'thumbnail' ),
    );
    register_post_type( 'custom_post_type', $args );
}
add_action( 'init', 'custom_post_type' );


  1. Register the taxonomy: Use the register_taxonomy() function to create and register the taxonomy for your custom post type. For example:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
function custom_taxonomy() {
    $args = array(
        'labels'       => array(
            'name'              => 'Custom Taxonomy',
            'singular_name'     => 'Custom Taxonomy',
            'menu_name'         => 'Custom Taxonomy',
            'all_items'         => 'All Items',
            'edit_item'         => 'Edit Item',
            'view_item'         => 'View Item',
            'update_item'       => 'Update Item',
            'add_new_item'      => 'Add New Item',
            'new_item_name'     => 'New Item Name',
            'search_items'      => 'Search Items',
            'popular_items'     => 'Popular Items',
            'separate_items_with_commas' => 'Separate items with commas',
            'add_or_remove_items'        => 'Add or remove items',
            'choose_from_most_used'      => 'Choose from the most used items',
            'not_found'                  => 'No items found',
        ),
        'public'       => true,
        'hierarchical' => true, // If you want hierarchical taxonomy (like categories), set it to true. Otherwise, set it to false.
        'show_ui'      => true,
        'show_in_menu' => true,
        'show_admin_column' => true,
    );
    register_taxonomy( 'custom_taxonomy', 'custom_post_type', $args );
}
add_action( 'init', 'custom_taxonomy' );


  1. Label the custom post type with the taxonomy: To associate the taxonomy with the custom post type, you need to add 'taxonomies' => array( 'custom_taxonomy' ) to the arguments of register_post_type() function. For example:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
function custom_post_type() {
    $args = array(
        'labels'    => array(
            'name'          => 'Custom Post Type',
            'singular_name' => 'Custom Post'
        ),
        'public'    => true,
        'supports'  => array( 'title', 'editor', 'thumbnail' ),
        'taxonomies' => array( 'custom_taxonomy' ),
    );
    register_post_type( 'custom_post_type', $args );
}
add_action( 'init', 'custom_post_type' );


After making these changes, you should see your custom taxonomy added to your custom post type in the WordPress admin panel.


How to display custom post meta data in WordPress?

To display custom post meta data in WordPress, follow these steps:

  1. Open the WordPress theme file where you want to display the meta data. This is typically the single.php or content.php file.
  2. Inside the loop, use the get_post_meta() function to retrieve the custom meta data. This function takes two parameters - the post ID and the meta key.


For example, if your custom meta key is "custom_field_name", and you want to display it for the current post, you can use the following code:

1
2
3
4
5
<?php
$custom_field_value = get_post_meta( get_the_ID(), 'custom_field_name', true );
?>

<p>Custom Field Value: <?php echo $custom_field_value; ?></p>


  1. If you want to display the meta data for a specific post outside the loop, you'll need to specify the post ID in the get_post_meta() function.


For example:

1
2
3
4
5
<?php
$custom_field_value = get_post_meta( 123, 'custom_field_name', true );
?>

<p>Custom Field Value: <?php echo $custom_field_value; ?></p>


Replace "123" with the actual post ID you want to display the meta data for.

  1. Customize the HTML markup and styling as per your needs.


Note: Make sure to replace "custom_field_name" with your actual custom meta key name. Additionally, remember to sanitize and validate any user input before displaying it to ensure security.


How to register a custom post type in WordPress?

To register a custom post type in WordPress, you need to use the register_post_type() function. Here are the steps to do so:

  1. Open your theme's functions.php file (or create a new one in your child theme if you have one).
  2. Add the following code to register your custom post type:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
function custom_post_type() {
    $labels = array(
        'name'               => _x( 'Custom Post Types', 'post type general name' ),
        'singular_name'      => _x( 'Custom Post Type', 'post type singular name' ),
        'menu_name'          => _x( 'Custom Post Types', 'admin menu' ),
        'name_admin_bar'     => _x( 'Custom Post Type', 'add new on admin bar' ),
        'add_new'            => _x( 'Add New', 'Custom Post Type' ),
        'add_new_item'       => __( 'Add New Custom Post Type' ),
        'new_item'           => __( 'New Custom Post Type' ),
        'edit_item'          => __( 'Edit Custom Post Type' ),
        'view_item'          => __( 'View Custom Post Type' ),
        'all_items'          => __( 'All Custom Post Types' ),
        'search_items'       => __( 'Search Custom Post Types' ),
        'parent_item_colon'  => __( 'Parent Custom Post Types:' ),
        'not_found'          => __( 'No custom post types found.' ),
        'not_found_in_trash' => __( 'No custom post types 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' => 'custom-post-type' ),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => false,
        'menu_position'      => null,
        'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
    );
    
    register_post_type( 'custom_post_type', $args );
}
add_action( 'init', 'custom_post_type' );


  1. Customize the labels and arguments in the $labels and $args arrays to fit your custom post type's name, capabilities, and desired features.
  2. Save the functions.php file.


After completing these steps and refreshing your WordPress admin area, you should see the new Custom Post Type menu in the sidebar. You can then start using it to create new posts of the custom type.


What is the purpose of custom posts in WordPress?

The purpose of custom posts in WordPress is to provide a way for users to create and manage content that is different from regular posts or pages. Custom posts allow users to define their own content types and create structured data to store specific information. This flexibility allows users to easily manage and display different types of content, such as portfolios, testimonials, products, events, or any other type of content that may not fit into the default posts or pages structure. Custom posts also enable users to have specific templates, taxonomies, and metadata for each content type, providing a more tailored and organized content management experience.


What are custom post type plugins in WordPress?

Custom post type plugins in WordPress allow users to create and manage different types of content other than regular posts and pages. They extend the functionality of WordPress by adding new content types, such as products, events, portfolios, testimonials, etc. These plugins provide a user-friendly interface to create and manage these custom post types, along with options to customize the layout, taxonomy, and other attributes of the content. By using custom post type plugins, users can effectively organize and present their content in a way that suits their specific needs and enhances the overall functionality of their WordPress website.

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

To extract and display a post by its specific ID in WordPress, you can follow these steps:Open your WordPress dashboard and navigate to the Appearance &gt; Editor section.Select the theme file where you want to display the post. Usually, it is the single.php o...
There may arise a need when developers want to hide the Visual editor aka WYSIWYG Editor from their Post types. If such is the case, you should just implement the following code in the functions.php file of your theme OR in the plugin files if you have a plugi...
To add page pagination to a custom WordPress page, you can follow these steps:Open the PHP file of your custom WordPress page using a text editor. Locate the code section where you want to display the pagination. Use the global $wp_query variable to access the...