Pergunta

I have a custom post type called "showroom'. Here is the code

<?php
function register_showroom_post_type() {
    $labels = array(
    'name'               => _x( 'Showroom Cars', 'post type general name' ),
    'singular_name'      => _x( 'Showroom Car', 'post type singular name' ),
    'add_new'            => _x( 'Add Showroom Car'  ),
    'add_new_item'       => __( 'Add New Showroom Car' ),
    'edit_item'          => __( 'Edit Showroom Car' ),
    'new_item'           => __( 'New Showroom Car' ),
    'all_items'          => __( 'All Showroom Cars' ),
    'view_item'          => __( 'View Showroom Car' ),
    'search_items'       => __( 'Search Showroom Listings' ),
    'not_found'          => __( 'No Showroom listings found' ),
    'not_found_in_trash' => __( 'No Showroom listings found in the Trash' ),
    'parent_item_colon'  => '',
    'menu_name'          => 'Showroom Cars',
  );
  $args = array(
    'labels'        => $labels,
    'description'   => 'Holds the showroom cars and specific data',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'thumbnail' ),
    'has_archive'   => true,
    'menu_icon'     => 'dashicons-admin-network',
  );
  register_post_type( 'showroom', $args );
}
add_action( 'init', 'register_showroom_post_type' );

function showroom_updated_messages( $messages ) {
  global $post, $post_ID;
  $messages['showroom'] = array(
    0 => '',
    1 => sprintf( __('Car listing updated. Nice one. <a href="%s">View Car</a>'), esc_url( get_permalink($post_ID) ) ),
    2 => __('Custom field updated.'),
    3 => __('Custom field deleted.'),
    4 => __('Car listing updated.'),
    5 => isset($_GET['revision']) ? sprintf( __('Car listing restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
    6 => sprintf( __('Car listing published. Sell that beast! <a href="%s">View the listing</a>'), esc_url( get_permalink($post_ID) ) ),
    7 => __('Car listing saved.'),
    8 => sprintf( __('Car listing submitted. <a target="_blank" href="%s">Preview car listing</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
    9 => sprintf( __('Car listing scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview car listing</a>'), date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
    10 => sprintf( __('Car listing draft updated. <a target="_blank" href="%s">Preview Car listing</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
  );
  return $messages;
}
add_filter( 'post_updated_messages', 'showroom_updated_messages' );

function taxonomies_showroom() {
  $labels = array(
    'name'              => _x( 'Car Manufacturers', 'taxonomy general name' ),
    'singular_name'     => _x( 'Car Manufacturer', 'taxonomy singular name' ),
    'search_items'      => __( 'Search Car Manufacturers' ),
    'all_items'         => __( 'All Car Manufacturers' ),
    'parent_item'       => __( 'Parent Car Manufacturer' ),
    'parent_item_colon' => __( 'Parent PCar Manufacturer:' ),
    'edit_item'         => __( 'Edit Car Manufacturer' ),
    'update_item'       => __( 'Update Car Manufacturer' ),
    'add_new_item'      => __( 'Add New Car Manufacturer' ),
    'new_item_name'     => __( 'New Car Manufacturer' ),
    'menu_name'         => __( 'Car Manufacturers' ),
    'show_ui'           => true,
  );
  $args = array(
    'labels' => $labels,
    'hierarchical' => true,
  );
  register_taxonomy( 'car_manufacturers', 'showroom', $args );
}
add_action( 'init', 'taxonomies_showroom', 0 );

?>

This creates a admin panel etc and I can create posts fine. I have a single-showroom.php file that displays the post type fine.

I want to create a template page that displays only this post type. So I created this very basic page to test this

<?php
    /**
    *Template Name: Showroom 2
    *Description: Creates a gallery of featured images from the showroom post type
    */

    get_header();
?>

<p>SHOWROOM 2 PAGE</p>

<?php
$args = array( 'post_type' => 'showroom', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
    the_title();
    echo '<div class="entry-content">';
    the_content();
    echo '</div>';
endwhile;
?>

<?php get_footer(); ?>

Ive created a Showroom page and set the template to "Showroom 2" but, this page isn't displayed. Instead the archive.php page is shown.

I have another custom post type called "auction" and when tested with this template file displays "auction" titles fine. So this leads me to think I've got something wrong in the post type creation. I can't see it!

Why isn't my Showroom post type showing with this template?

[EDIT]

I'm running all this locally so I installed another fresh copy of wordpress, created my custom post type (I have it as a plugin), copied over my template file I wanted to use and it worked fine!

The only thing I can think of is that I created the 'showroom' posts before transferring the custom post type to a plugin. When I created the 'auction' custom posts I had already made it 'auction' custom post a plugin.

So perhaps there is some quirky conflict.

I'm going to delete the 'showroom' posts form the database and see what happens...

[EDIT 2]

Upon further investigation this problem is only happening when I set Permalinks to "Post name". If I select "Numeric" for example, the template file is displayed.

Does anyone know why that might be?

[SOLUTION]

Thanks all for your suggestions. In the end it was just like Fyn suggested a naming issue.

I eventually changed the custom post type name from 'showroom' to 'car' and all is fine!

That was horrible! Lost about a day of my life... ah the joys of coding :)

Foi útil?

Solução

I think there could be a name conflict. Well, I had one once. If there's an archive for your post type it's name is automatically "showroom", so maybe it chooses the showroom archive over the showroom page. Did you try to rename your page to check if there's a conflict?

Or maybe you should consider using archive-showroom.php to display all your showroom posts! It would be the easiest way.

Here are also some references that might just help you:

https://codex.wordpress.org/Post_Type_Templates

https://developer.wordpress.org/themes/template-files-section/page-template-files/

http://codex.wordpress.org/Post_Types

Outras dicas

Here is the link for the template hirarchy of wordpress template

https://codex.wordpress.org/images/1/18/Template_Hierarchy.png

Hope this helps you.

You can achieve this by :

   global $post;
   if ($post && $post->ID = 123) {
     include TEMPLATEPATH . '/single-123.php';
     exit();
   }

put this code at the end of your functions.php file. Change 123 by your post id

You should always create your taxonomy first and then your custom post type for archive pages to work correctly. One other thing, you have a bug in your code. There is an argument missing in this line

'add_new'  => _x( 'Add Showroom Car'  ),

It should be

'add_new'  => _x( 'Add Showroom Car', 'post type general name' ),

If you really need a page.php template to show custom post types on, you can check out this Q&A I did on that

I've noticed that sometimes going under Settings -> Permalinks and clicking "Save Changes" will sometimes fix the issue.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top