[WordPress] 外掛分享: Custom Post Template

首頁外掛目錄 › Custom Post Template
10,000+
安裝啟用
★★★★
4.8/5 分(43 則評價)
3029 天前
最後更新
問題解決
WordPress 3.4+ v1.5 上架:2008-11-26

內容簡介

提供一個下拉式功能,讓您可以在文章編輯頁面中選擇不同的模板。這些模板和頁面模板類似,且會取代指定文章的 single.php。然而此外掛並不會在文章列表頁面(例如日期或類別檔案)上切換模板,僅會影響單獨文章的模板使用(也就是您可以選擇非 single.php 的模板)。

就此外掛而言,文章模板的設置方式和頁面模板類似,因為它們在頂部具有特定形式的 PHP 註解。每個文章模板必須包含以下或類似的設置:

<?php
/*
Template Name Posts: Snarfer
*/
?>

注意:頁面模板使用「Template Name:」,而文章模板使用「Template Name Posts:」。

注意:您必須將自訂文章模板檔案與 index.php 模板檔案置於同一個目錄/資料夾中,而不是在子目錄/子資料夾中。

開發者

如果您想要在自訂文章類型上實現自訂文章模板,您可以使用 cpt_post_types 過濾器。以下是一個範例,它顯示如何將自訂文章模板選擇器和元框架新增至「Movie」和「Actor」自訂文章類型中。此程式碼可新增至外掛或置於您的佈景主題中的 functions.php 檔案中。

/**
* 將 WP cpt_post_types 過濾器連接到函式
*
* @param array $post_types 這些模板所用的文章類型名稱陣列
* @return array這些模板所用的文章類型名稱陣列
**/
function my_cpt_post_types( $post_types ) {
$post_types[] = 'movie';
$post_types[] = 'actor';
return $post_types;
}
add_filter( 'cpt_post_types', 'my_cpt_post_types' );

外掛標籤

開發者團隊

⬇ 下載最新版 (v1.5) 或搜尋安裝

① 下載 ZIP → 後台「外掛 › 安裝外掛 › 上傳外掛」
② 後台搜尋「Custom Post Template」→ 直接安裝(推薦)
📦 歷史版本下載

原文外掛簡介

Provides a drop-down to select different templates for posts from the post edit screen. The templates are defined similarly to page templates, and will replace single.php for the specified post. This plugin will NOT switch the templates for the different posts in a listing page, e.g. a date or category archive, it will only affect the template used for single posts (i.e. you can choose a template which is not single.php).
Post templates, as far as this plugin is concerned, are configured similarly to page templates in that they have a particular style of PHP comment at the top of them. Each post template must contain the following, or similar, at the top:

First note: Page templates use “Template Name:“, whereas post templates use “Template Name Posts:“.
Second note: You must have the custom post template files in your theme in the same directory/folder as your index.php template file, not in a sub-directory/sub-folder.
Developers
If you want to implement the custom post templates on a custom post type, you can use the cpt_post_types filter, here’s an example below of adding the custom post template selector and metabox to the “Movie” and “Actor” custom post types. This code can be added to a plugin or to the functions.php file in your theme.
/**
* Hooks the WP cpt_post_types filter
*
* @param array $post_types An array of post type names that the templates be used by
* @return array The array of post type names that the templates be used by
**/
function my_cpt_post_types( $post_types ) {
$post_types[] = 'movie';
$post_types[] = 'actor';
return $post_types;
}
add_filter( 'cpt_post_types', 'my_cpt_post_types' );

延伸相關外掛

文章
Filter
Apply Filters
Mastodon