Skip to main content
edited title
Link
fuxia
  • 107.1k
  • 38
  • 255
  • 459

Plugin Development - Custom Post Type single post type shown in theme single.php

Source Link

Plugin Development - Custom Post Type single post type shown in theme single.php

I am creating a plugin where i have created a custom post type smart-event and I want to serve the single posts under this category via a single page.That's why I have created a single post page named : single-event.php directly into plugin folder -like- event > single-event.php

And I am using this function to load the single page

function myevent($template) {
if ('event' == get_post_type(get_queried_object_id()) && !$template) {
    $template = dirname(__FILE__) . '/single-event.php';
}
return $template;
}
add_action( 'single_template', 'myevent'));

But the single post is being shown in theme single.php page not in the page i have created.

Below is my CPT register code and i have flush the rewrite mode in permalink settings.

function eventCPT(){
    $labels = array(
        'name'               => esc_html__( 'Events',                   'event' ),
        'singular_name'      => esc_html__( 'Event',                    'event' ),
        'menu_name'          => esc_html__( 'Event',                'event' ),
        'name_admin_bar'     => esc_html__( 'Event',                'event' ),
        'add_new'            => esc_html__( 'Add New Event',            'event' ),
        'add_new_item'       => esc_html__( 'Add New Event',            'event' ),
        'new_item'           => esc_html__( 'New Event',                'event' ),
        'edit_item'          => esc_html__( 'Edit Event',               'event' ),
        'view_item'          => esc_html__( 'View Event',               'event' ),
        'all_items'          => esc_html__( 'All Events',               'event' ),
        'search_items'       => esc_html__( 'Search Events',            'event' ),
        'parent_item_colon'  => esc_html__( 'Parent Events:',           'event' ),
        'not_found'          => esc_html__( 'No events found.',         'event' ),
        'not_found_in_trash' => esc_html__( 'No events found in Trash.','event' )
    );

    $args = array(
        'labels'             => $labels,
        'description'        => __( 'Description.', 'event' ),
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array( 'slug' => 'event' ),
        'capability_type'    => 'post',
        'has_archive'        => false,
        'hierarchical'       => false,
        'menu_position'      => null,
        'menu_icon'          => 'dashicons-calendar-alt',
        'supports'           => array( 'title', 'author', 'thumbnail', 'excerpt', 'comments' )
    );

    register_post_type( 'event', $args );
}

add_action( 'init', 'eventCPT'));