簡體   English   中英

Wordpress自定義模板未顯示在自定義帖子類型中

[英]Wordpress custom template not showing in custom post type

我正在嘗試在自定義帖子類型中使用自定義頁面模板。 但是,在嘗試添加新頁面時,模板選項不會顯示在帖子類型中。 請參閱以下代碼:

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內置頁面。 自定義帖子類型應該只有一個模板。 如果您需要更改它,那么您很可能想要創建另一個帖子類型或只使用頁面或常規帖子。

來源: 自定義帖子類型的頁面屬性選項

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM