php - 自定义帖子类型中未显示 Wordpress 自定义模板

标签 php wordpress templates

我正在尝试在自定义帖子类型中使用自定义页面模板。但是,当尝试添加新页面时,模板选项不会显示在帖子类型中。请看下面的代码:

自定义帖子类型的 functions.php:

    add_action( 'init', 'register_cpt_product' );

function register_cpt_product() {

    $labels = array( 
        'name' => _x( 'Products', 'product' ),
        'singular_name' => _x( 'Product', 'product' ),
        'add_new' => _x( 'Add New', 'product' ),
        'add_new_item' => _x( 'Add New Product', 'product' ),
        'edit_item' => _x( 'Edit Product', 'product' ),
        'new_item' => _x( 'New Product', 'product' ),
        'view_item' => _x( 'View Product', 'product' ),
        'search_items' => _x( 'Search Products', 'product' ),
        'not_found' => _x( 'No products found', 'product' ),
        'not_found_in_trash' => _x( 'No products found in Trash', 'product' ),
        'parent_item_colon' => _x( 'Parent Product:', 'product' ),
        'menu_name' => _x( 'Products', 'product' ),
    );

    $args = array( 
        'labels' => $labels,
        'hierarchical' => true,
        'description' => 'Product pages',
        'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'custom-fields', 'revisions', 'page-attributes' ),
        'taxonomies' => array( 'category' ),
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 5,

        'show_in_nav_menus' => true,
        'publicly_queryable' => true,
        'exclude_from_search' => false,
        'has_archive' => false,
        'query_var' => true,
        'can_export' => true,
        'rewrite' => true,
        'capability_type' => 'page'
    );

    register_post_type( 'product', $args );
}

页面模板(模板标签除外)

<?php
/*
Template Name: Product
*/
?>

最佳答案

您不能拥有任何 Wordpress 模板文件的页面模板:

喜欢:

archive-*.php
category-*.php
single-*.php // in your case

或任何其他。

但是您可以为 page-*.php 或您的自定义 filename.php 使用页面模板:

any-file-name.php
page-*.php

将您的 single-product.php 文件重命名为其他名称或删除 single 并将其重命名为其他名称

喜欢:

my-single-product.php // this should work

请记住 single-product.php 将用于呈现您的自定义帖子类型(产品)单个帖子内容。

已编辑:

页面模板仅适用于 Wordpress 内置页面。自定义帖子类型应该只有一个模板。如果您需要更改它,那么您很可能想要创建另一种帖子类型,或者只使用页面或常规帖子。

来源:Page Attributes options for custom post types

关于php - 自定义帖子类型中未显示 Wordpress 自定义模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21974656/

相关文章:

javascript - 使用 enqueue 实现 WordPress 子主题的 jQuery 函数

wordpress - 如何从自定义登录页面上的 wp_user 表检查用户?

C++ Callback——如何解耦回调类型

C++ 模板歧义

php - 如何从mysql数据库连接多个表并在android中使用 View 列表显示

php - 像 stackoverflow 这样的搜索标签?

Wordpress 在移动到不同的服务器后显示 fatal error

c++ - 无法解析模板委托(delegate)中重载的类方法

php - 谷歌饼图动态与 mysql 和 php

php - 如何在 Magento 购物车中使用特殊公式