自定义帖子类型 - 找不到模板页面

时间:2014-04-17 05:54:58

标签: wordpress templates custom-post-type

我有一个名为“展厅”的自定义帖子类型。这是代码

<?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 );

?>

这会创建一个管理面板等,我可以很好地创建帖子。我有一个单一的showroom.php文件,可以显示帖子类型。

我想创建一个仅显示此帖子类型的模板页面。所以我创建了这个非常基本的页面来测试这个

<?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(); ?>

我创建了一个Showroom页面并将模板设置为“Showroom 2”但是,此页面不会显示。而是显示archive.php页面。

我有另一个名为“拍卖”的自定义帖子类型,当使用此模板文件进行测试时,显示“拍卖”标题很好。所以这让我觉得我在帖子类型创建中有错误。我看不到它!

为什么我的陈列室帖子类型没有显示此模板?

[编辑]

我在本地运行所有这些因此我安装了另一个新的wordpress副本,创建了我的自定义帖子类型(我把它作为插件),复制到我想要使用的模板文件上并且工作正常!

我唯一能想到的是,在将自定义帖子类型转移到插件之前,我创建了“陈列室”帖子。当我创建“拍卖”自定义帖子时,我已经将其作为“拍卖”自定义发布了一个插件。

所以也许有一些古怪的冲突。

我要从数据库中删除'陈列室'帖子,看看会发生什么......

[编辑2]

经过进一步调查,只有当我将永久链接设置为“帖子名称”时才会出现此问题。例如,如果我选择“数字”,则会显示模板文件。

有谁知道为什么会这样?

[溶液]

感谢大家的建议。最后,就像Fyn提出了一个命名问题。

我最终将自定义帖子类型名称从“陈列室”更改为“汽车”,一切都很好!

太可怕了!失去了我生命中的一天......编码的乐趣啊:)

5 个答案:

答案 0 :(得分:1)

我认为可能存在名称冲突。好吧,我有一次。如果您的帖子类型有一个存档,它的名称将自动为“陈列室”,因此它可能会在展厅页面上选择展厅存档。您是否尝试重命名页面以检查是否存在冲突?

或许您应该考虑使用archive-showroom.php来展示您的所有展厅帖子!这将是最简单的方法。

以下是一些可能对您有帮助的参考资料:

https://codex.wordpress.org/Post_Type_Templates

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

http://codex.wordpress.org/Post_Types

答案 1 :(得分:0)

以下是wordpress模板

的模板层次结构的链接

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

希望这会对你有所帮助。

答案 2 :(得分:0)

您可以通过以下方式实现这一目标:

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

将此代码放在 functions.php 文件的末尾。通过您的帖子ID更改123

答案 3 :(得分:0)

您应该首先创建分类,然后创建归档页面的自定义帖子类型才能正常工作。另外一件事,你的代码中有一个错误。这一行中缺少一个参数

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

应该是

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

如果您确实需要page.php模板来显示自定义帖子类型,可以查看{我在上面做过的{3}}

答案 4 :(得分:0)

我注意到有时有时会进入“设置”->“永久链接”下并单击“保存更改”会解决此问题。

相关问题