Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress 自定义帖子类型-找不到模板页面_Wordpress_Templates_Custom Post Type - Fatal编程技术网

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

Wordpress 自定义帖子类型-找不到模板页面,wordpress,templates,custom-post-type,Wordpress,Templates,Custom Post Type,我有一个自定义的帖子类型叫做“showroom”。下面是代码 <?php function register_showroom_post_type() { $labels = array( 'name' => _x( 'Showroom Cars', 'post type general name' ), 'singular_name' => _x( 'Showroom Car', 'post type singul

我有一个自定义的帖子类型叫做“showroom”。下面是代码

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

展厅2页

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

我有另一个自定义的帖子类型,称为“拍卖”,当使用这个模板文件测试时,显示“拍卖”标题的效果很好。因此,这让我觉得我在帖子类型创建过程中出错了。我看不到它

为什么我的展示室帖子类型不使用此模板显示

[编辑] 我在本地运行所有这些,所以我安装了wordpress的另一个新副本,创建了我的自定义帖子类型(我把它作为插件),复制了我想要使用的模板文件,它工作得很好

我唯一能想到的是,在将自定义帖子类型转换为插件之前,我创建了“展示室”帖子。当我创建“拍卖”自定义帖子时,我已经将其设置为“拍卖”自定义帖子插件

因此,也许有一些奇怪的冲突

我将从数据库中删除“展示室”帖子,看看会发生什么

[编辑2] 经过进一步调查,这个问题只有在我将Permalinks设置为“Post name”时才会发生。例如,如果我选择“Numeric”,则会显示模板文件

有人知道为什么会这样吗

[解决方案] 谢谢大家的建议。最后就像Fyn建议的命名问题一样

我最终将自定义的帖子类型名称从“showroom”改为“car”,一切都很好


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

这是wordpress模板的模板hirachy链接


希望这对您有所帮助。

我认为可能存在名称冲突。我曾经有过一次。如果您的帖子类型有存档,它的名称将自动命名为“showroom”,因此它可能会选择showroom存档而不是showroom页面。您是否尝试重命名页面以检查是否存在冲突

或者也许你应该考虑使用CaseJooSoRoM.PHP来显示你所有的展示室帖子!这将是最简单的方式。

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


您可以通过以下方式实现:

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

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

您应该始终首先创建分类法,然后创建自定义帖子类型,以便归档页面正常工作。另外,您的代码中有一个bug。此行缺少一个参数

'add_new'  => _x( 'Add Showroom Car'  ),
应该是

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

如果您确实需要一个
page.php
模板来显示自定义帖子类型,您可以查看我在该模板上所做的工作

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

更改页面slug希望它能工作。谢谢。我刚刚又在读结构。也许我做错了。如果我想创建一个页面来显示一个自定义的帖子类型和样式,该帖子类型的显示方式,例如网格等。我是否为该帖子类型创建一个归档页面并在其中构建结构?还是我在管理面板中创建的页面的模板文件?正确的方法是什么?当然你可以同时做这两件事,但正如我所说的,归档将是更简单的解决方案,因为你只需要创建archive-showroom.php,在其中放入一个常规循环,它就会工作。它认识到这是一个showcase归档页面,并且只从那里获取帖子。归档文件显示所有showcase文章似乎更容易,不是吗。如果您想显示单个showcase帖子,您可以创建一个single-showroom.php,也可以在其中放置一个常规循环,如果显示该类型的单个帖子,将使用该循环。至于样式:您可以在这些模板文件中使用您喜欢的任何结构。也许知道有一些wordpress函数可以用来动态呈现一些类会对你有帮助,谢谢,这是很有用的。我明白你在说什么,我想我的想法是,有时候容易总是正确的方法!:)尽管如此,我仍然不明白为什么“展示室”自定义帖子类型看不到模板文件,而我的“拍卖”自定义帖子类型(从“展示室”复制并相应调整)看不到模板文件……让我抓狂!我将创建一个新的showroom实例,看看会发生什么。这是一个命名问题。谢谢谢谢,但是这篇文章显示的很好。我不清楚是否应该使用归档页面或模板文件来显示我的“展示室”自定义帖子类型。(我想最终以网格的形式显示展厅帖子)谢谢。查看这一点表明,当wordpress显示自定义帖子类型的页面时,它会查找archive-POSTTYPE.php。我希望最终将我的展示室帖子类型显示为网格。我想我需要一个themplate文件(没有侧边栏等),用于查询“showroom”帖子类型并将其显示在网格中。可以吗?或者我必须创建一个归档页面才能做到这一点?谢谢你,我一定会仔细阅读的。我已经对我原来的帖子进行了编辑。所以我不认为遗漏的论点是问题的根源。但正如我说的,我也会看看。谢谢