Steven
Steven

Reputation: 19425

Why isn't my custom post type using my custom template?

I've created a custom post type and I'm trying to use a custom template for showing the content.

My template file is named single-event.php <-- The error was here

My custom post type code is:

function create_post_type() {
  $args = array(
    'labels' => array(
      'name' => __( 'Événement' ),
      'singular_name' => __( 'Événement' ),
      'add_new' => __('Nouvel Événement')
    ),
    'public' => true,
    'has_archive' => true,
    'taxonomies' => array('event_data'),
    'menu_position' => 5,
    'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt' ),
    'rewrite' => array('slug' => 'event')
  );

  register_post_type( 'event',$args);
}

My custom template looks like this:

<?php get_header(); ?>

  <div  id="event" class="full-width-container single-post">
    TEEEEEEEST
  </div>

<?php get_footer(); ?>

Opening a custom post in my browser, the url is:

http://my.site.no/?event=my-custom-post-title

But why is it using single.php and not single-event.php? What am I missing?

Upvotes: 1

Views: 779

Answers (2)

Steven
Steven

Reputation: 19425

I finally found my mistake - two actually.

I had a category called event - that was my first mistake (I think).
My second mistake was that my filename was singe-event.php <-- missing the l

And that's how you spend 5 hours debugging and reading up on Custom Templates combined with Custom Post Types....

Upvotes: 2

Bertrand
Bertrand

Reputation: 1000

Wordpress need sometimes refresh permalink with custom posts. You should try to activate permalink and try again !

Hope it will work for you..

And just add rewrite parameter and try url example.com/event

function create_post_type() {
$args = array(
'labels' => array(
  'name' => __( 'Événement' ),
  'singular_name' => __( 'Événement' ),
  'add_new' => __('Nouvel Événement')
),
'public' => true,
'has_archive' => true,
'rewrite' => array( 'slug' => 'event' ), //Add this parameter
'taxonomies' => array('event_data'),
'menu_position' => 5,
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt' ),
'rewrite' => array('slug' => 'event')
);

Upvotes: 0

Related Questions