Custom Post TypesPHP Development

Creating Singular Page & Archives Page for Custom Post Type in Theme Folder

Using WP_Query(); class except we have to specify the 'post_type' => 'post-keyword', within list of arguments array of the class.
Events Custom Post type in WordPress

Creating custom post types is simple and straight forward like just hooking the register_post_type() function before WordPress’ 'init' function runs as shown in this post. After creating, the post type, in functions.php file or inside mu-plugins folder in wp-content folder, displaying those posts inside a page have not been such easy like shown in this post about “Custom” Querying Different Posts in WordPress”.

$customPostsQuery = new WP_Query( array(
    'post_type'      => 'post-keyword',
    'posts_per_page' => 3,
) );

The main difference while querying custom post type instead of WP posts, is that you must specify the 'post_type' => 'post-keyword', inside an array of the WP_Query() argument.

Creating singular page for the custom post type

After creating custom post type in WordPress, by default WordPress uses the single.php template to display the content of custom singular post. If you want to use different template for each your custom post type singular posts, follow this steps

  1. Within the root folder of our custom theme, create new file with the name single-post-keyword.php named as single followed by – and the post keyword that is very important. For example, if I am creating Event custom post type, the keyword will be event & to create singular page for event posts, I have to create a file called single-event.php (The file name is very important that matches this format)
  2. Copy content from single.php or create from scratch and redesign accordingly

Remember, After you create single-event.php file and saving it, WordPress may not display changes on the front-end still, that is because for performance purpose, WP doesn’t update the permalinks for custom post types. So, you will have to go to your WP admin page & save the permalink once again to refresh it.

Creating Archives Page for Custom Post Types

In order to have our custom post type listed as an archive in the post slug page, we need to tell WordPress that the custom post we created supports archive upon creating custom post type. using the 'has_archive' => true, inside the argument array of WP_Query(); class.

By default WP will use the custom post keyword as a base slug for archive page. For archive page if we want to change the base slug to any keyword like events instead of event in all events displaying page, we have to pass 'rewrite' => array('slug' => 'events'), inside the argument array of WP_Query(); class. So, it will look like,

function create_events_post_type() {
    register_post_type( 'event', array(
        'labels'             => array(
            'name'               => __( 'Events' ),
            'singular_name'      => __( 'Event' ),
            'add_new'            => __( 'Add New Event' ),
            'edit_item'          => __( 'Edit Event' ),
            'view_item'          => __( 'View Event' ),
            'search_items'       => __( 'Search Events' ),
            'not_found'          => __( 'No events found' ),
            'menu_name'          => __( 'Events' ),
        ),
        'public'             => true,
        'show_ui'            => true,
        'show_in_rest'       => true,
        'has_archive'        => true,
        'rewrite'            => array( 'slug' => 'events' ),
        'capability_type'    => 'post',
        'supports'           => array( 'title', 'editor', 'thumbnail', 'excerpt' ),
        'menu_icon'          => 'dashicons-calendar',
    ) );
}
add_action( 'init', 'create_events_post_type' );

Customizing The Archive Page of Custom Post Type

By default, while creating custom post type and adding archive support to it, WordPress archive.php as an archive template. If we want to use different template for custom post type:

  1. Create a file called archive-post-keyword.php inside the root folder of your custom theme. For example, If I am creating Event post type create a file named archive-event.php (The file name format is very important to match this format)
  2. Copy content from archive.php create new template from scratch and replace content & redesign accordingly.

Get URL of any custom post type dynamically

We could get the custom post type using home_url( '/events'); function but in the future if the permalink is changed, this will lead to 404. For that purpose, there is a WP function get_post_type_archive_link(); and pass in the post keyword as an argument. So it will look like this.

get_post_type_archive_link( 'event' );

—- Thankyou for Reading —-

Shares:

Related Posts

Top Categories

PHP Development
22
WordPress Theme Development
21
Wordpress Development
18
WordPress JS Development
13
Show Comments (0)
Leave a Reply

Your email address will not be published. Required fields are marked *