【问题标题】:How can I add own theme template for custom post type?如何为自定义帖子类型添加自己的主题模板?
【发布时间】:2016-11-30 15:43:08
【问题描述】:

我有一些帖子类型,但现在我需要在页面部分添加某些模板。

function products_init() {
  $args = array(
    'label' => 'Products',
    'capability_type' => 'page',
    'menu_icon' => 'dashicons-cart',
    'taxonomies' => array('category'),
    'rewrite' => array('slug' => 'products'),
    'hierarchical'        => false,
    'public'              => true,
    'show_ui'             => true,
    'menu_position'       => 4,
    'can_export'          => true,
    'has_archive'         => true,
    'exclude_from_search' => false,
    'publicly_queryable'  => true,
    'query_var' => true,
    'supports' => array('title', 'editor', 'thumbnail', 'page-attributes')
  );
  register_post_type('products', $args);
}
add_action('init', 'products_init');

【问题讨论】:

  • 您是在问如何创建模板文件或如何将自定义字段添加到帖子类型(并在创建/编辑屏幕中显示它们)?
  • 现在,我现在如何创建模板文件,但是当我尝试添加新的自定义帖子类型帖子时它们没有显示。
  • 如——创建新帖子后,转到帖子的网址时无法在网站上查看?
  • 是的,我收到 404 错误页面
  • 您能否将创建帖子类型的代码粘贴到您的问题中?

标签: php wordpress wordpress-theming


【解决方案1】:

正如codex 提到的,您需要添加两个文件来显示自定义帖子类型。

archive-{post_type}.php
single-{post_type}.php

如果您遇到 404 错误,您可以尝试在 functions.php 中调用 flush_rewrite_rules() 并刷新您的页面。如果一切正常,请立即删除flush_rewrite_rules()。

否则,您可以从仪表板 -> 设置-> 永久链接页面刷新 reweire 规则。单击保存按钮,然后检查您是否可以看到该页面。保存永久链接设置也会调用flush_rewrite_rules()。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-16
    • 1970-01-01
    • 1970-01-01
    • 2013-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多