luguokankan 發表於 2013-6-10 13:51 # G- S" m, X" {' @; f
post裡面要設置featured image 2 u' p F5 u* e
在這裡面設置嗎?8 L# i$ H6 I5 }) ]# t1 A
/ o8 w* N( A6 y F, Z8 r* ?$ C+ t8 J3 M o
<?php
+ s, U! t' J H. d4 M/*
. [# }% x+ S, n- yPlugin Name: Single Post Template
* @8 S% h m8 W& {: kPlugin URI: http://www.nathanrice.net/plugins& O' ?; y3 G5 w3 }- [ i% S
Description: This plugin allows theme authors to include single post templates, much like a theme author can use page template files.$ w# C( C$ u+ y' }
Version: 1.3
: b1 g( N) r& d" Y, {Author: Nathan Rice5 d: g6 {3 @1 B' {% b( V# G
Author URI: http://www.nathanrice.net/( _4 `' ~* m7 Y. {- B; y; }
. V1 B3 |' } f
This plugin inherits the GPL license from it's parent system, WordPress.
4 g& G' T3 G+ X3 n) ^; t*/- Q6 H, b0 F" e& k; |6 b9 m
( P) q8 k$ P' m, c// This function scans the template files of the active theme, 7 t2 _% d7 d3 Q1 s2 A; k7 H
// and returns an array of [Template Name => {file}.php]
! O- Y s I- F9 l3 jif(!function_exists('get_post_templates')) {, t, u. e& u. \
function get_post_templates() {2 S S& l0 V1 C
$themes = get_themes();
% M$ N1 C, h: v( _- f' k+ k $theme = get_current_theme();
* F& E r- r1 m8 t $templates = $themes[$theme]['Template Files'];
- `9 K1 M) ^5 o $post_templates = array();
0 p! _7 q9 K0 a/ z& c% L% B/ I+ k; w/ @9 {4 q
$base = array(trailingslashit(get_template_directory()), trailingslashit(get_stylesheet_directory()));. a0 K" I! c5 K V9 Y8 W' {
. w+ J# b7 y0 W" ~' }
foreach ((array)$templates as $template) {4 d& _! Y- e }, Y/ i8 @# @6 N
$template = WP_CONTENT_DIR . str_replace(WP_CONTENT_DIR, '', $template); - F! C$ Y' A/ Z
$basename = str_replace($base, '', $template);
g" m0 G }1 k3 ^
3 l6 `2 a) b6 f8 {$ N // don't allow template files in subdirectories
. e8 F8 T. K0 U, ~* }9 m if (false !== strpos($basename, '/'))3 s8 Y- F6 H7 J' B9 j
continue;
% X2 _! Y5 ?% p3 t
/ h4 W" s! K! T/ v: L! t $template_data = implode('', file( $template ));
9 I: A( { C/ A" x
5 g9 q& I4 F4 Z* z $name = '';
6 k1 ]4 K% n* X O if (preg_match( '|Single Post Template .*)$|mi', $template_data, $name))
' \) l: }- T* ~6 C- F $name = _cleanup_header_comment($name[1]);
$ c9 F4 q6 e8 k' v1 ]
5 U2 g6 ^9 K" O- c3 U+ p& S if (!empty($name)) {
: f* g& _7 Q' I t7 c- t" y if(basename($template) != basename(__FILE__))
d! f4 s( v3 T, I3 R; D* K2 Y $post_templates[trim($name)] = $basename;
; Y7 @. K3 T% \# x1 Z; ~: n/ H9 ? }0 i5 {* y/ u& V$ z/ h n) G
}
( C0 X% `0 U5 [& A! l& W4 t5 Q& X) T* u1 f- b# d% a
return $post_templates;
. n# g, j0 `7 A1 Z
; s+ |8 F! G; V) M/ C# H2 x6 f}}
' b& Y M& J' d2 z; _* Q7 B& y4 p2 d( w3 U- J+ {- O
// build the dropdown items% T! K2 `, v7 W& k/ I: c" X
if(!function_exists('post_templates_dropdown')) {2 |! k- ]7 ^5 e1 H7 ]
function post_templates_dropdown() {
* c6 M! Z, S) [$ e+ e6 B8 N global $post;
+ G- ^( P m3 B* V7 N/ D( v# n3 A $post_templates = get_post_templates();- u. q7 R' @9 n0 D, d: t
1 m& G' n2 R4 B
foreach ($post_templates as $template_name => $template_file) { //loop through templates, make them options
$ ]( R ]3 M% P! E5 c3 Q if ($template_file == get_post_meta($post->ID, '_wp_post_template', true)) { $selected = ' selected="selected"'; } else { $selected = ''; }
$ `) E* W) w0 C1 f $opt = '<option value="' . $template_file . '"' . $selected . '>' . $template_name . '</option>';
( T+ B2 ]: w3 A1 z* h7 ` echo $opt;
1 E9 v/ U g+ n4 J) y }
. Q/ ]1 ~; J1 ~' |2 A; v! X}}% j' G( U/ X5 s9 Y
; }6 |; c3 f$ I// Filter the single template value, and replace it with, `. V# a" }+ D7 X* R7 v D6 m
// the template chosen by the user, if they chose one.4 o( H3 C( ]1 q' y j7 N. n
add_filter('single_template', 'get_post_template');$ f v7 P F; n- _: f7 | k
if(!function_exists('get_post_template')) {) a5 j' p! C2 {' @& E
function get_post_template($template) {
( N' h7 ^5 G0 s4 w* n+ I8 R global $post;
% O) s9 R J! A) l" k1 Q, B $custom_field = get_post_meta($post->ID, '_wp_post_template', true);
S4 K: d+ g g! f0 Y: b if(!empty($custom_field) && file_exists(TEMPLATEPATH . "/{$custom_field}")) { * Z8 l2 a* @- ]$ P# a! w: C% E
$template = TEMPLATEPATH . "/{$custom_field}"; }% [6 C( l) J' c# o% f
return $template; p; [2 L; T* n3 [ }
}}
' S, u1 u7 h8 T u0 Y# [, S5 G" d2 i
8 t( S2 J% d: N+ k// Everything below this is for adding the extra box
5 ~$ h% [7 U6 `- }8 x// to the post edit screen so the user can choose a template
3 c9 Y/ J, d3 K# J7 F3 {0 r% S0 E7 [( i
// Adds a custom section to the Post edit screen! a: L$ N; z+ ^1 G; _" T
add_action('admin_menu', 'pt_add_custom_box');& {$ \% o- o6 I
function pt_add_custom_box() {3 t; z( t' t A+ D" Z
if(get_post_templates() && function_exists( 'add_meta_box' )) {
/ N& N2 W: I" R4 E3 ] add_meta_box( 'pt_post_templates', __( 'Single Post Template', 'pt' ),
7 T0 s: G5 g: R( } 'pt_inner_custom_box', 'post', 'normal', 'high' ); //add the boxes under the post
0 B# l) l" S. A }/ }# j. N# u* q" j
}
; ^: p, k7 F- C( Z7 [5 D
8 l I7 ~7 i. G: v% ^ ^// Prints the inner fields for the custom post/page section- f+ X+ R/ B5 _2 p% I7 D- P$ R
function pt_inner_custom_box() {
! `2 ]) Q/ r/ h5 L- U global $post;- Z6 e$ U( P5 G$ h$ M. [3 I
// Use nonce for verification- R# N! z) \1 R
echo '<input type="hidden" name="pt_noncename" id="pt_noncename" value="' . wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
% L+ v, i X: |# K" Y, ? // The actual fields for data entry
; O7 @" z) a5 x/ N, j) | echo '<label class="hidden" for="post_template">' . __(" ost Template", 'pt' ) . '</label><br />';: k9 c0 Y6 x+ {; Q3 k6 D' b# Y
echo '<select name="_wp_post_template" id="post_template" class="dropdown">';
6 j( P/ m; Y% c- V echo '<option value="">Default</option>';9 T e. D d: H9 Q4 ?: S" f6 l
post_templates_dropdown(); //get the options0 U* c$ i6 c! F4 \
echo '</select><br /><br />';
! [( M5 r0 ^1 R% p0 B- z echo '<p>' . __("Some themes have custom templates you can use for single posts that might have additional features or custom layouts. If so, you』ll see them above.", 'pt' ) . '</p><br />';! ]( l8 q" o* u, F
}& [* F/ k4 x5 z" G4 |+ I# _, u$ R4 r
! q8 U( j3 v. ^// When the post is saved, saves our custom data+ f1 U% u+ @5 o0 r# Y' j3 Z% M
add_action('save_post', 'pt_save_postdata', 1, 2); // save the custom fields+ V# ]& K, ?! K1 s% f2 |( P7 ]
function pt_save_postdata($post_id, $post) {0 i+ g- F" e- w- E# y! `1 h- K
$ I' E; w9 f! Z! P8 B
// verify this came from the our screen and with proper authorization,! T1 H) h3 m$ J; g. e# I
// because save_post can be triggered at other times( n0 V2 G3 g& x K
if ( !wp_verify_nonce( $_POST['pt_noncename'], plugin_basename(__FILE__) )) {
! o8 }. [1 e+ y7 k* x return $post->ID;: ? \) q) _) R6 |9 }9 y0 a
}: h6 [4 Z. ~! p) Z$ \7 ?. d
/ I4 A( X2 h/ W! N6 c // Is the user allowed to edit the post or page?/ }) S, h5 L2 _2 R4 r. E5 d
if ( 'page' == $_POST['post_type'] ) {+ W" X/ z% f2 {1 O2 ^7 b
if ( !current_user_can( 'edit_page', $post->ID ))7 K0 K! j) D. Y4 s/ h$ n
return $post->ID;4 X9 K. W/ u& N2 c1 ]" a9 O/ K: q9 ?
} else {" h$ S" `5 |7 o5 h0 g$ @
if ( !current_user_can( 'edit_post', $post->ID )) {. [8 U) L( W& u+ e! Q3 B! F$ Q5 e
return $post->ID;
3 \. S# g" k4 b6 `' C }+ H% V" A8 U5 }$ Y7 R5 F! N) o: E$ `" g
' {3 k6 p6 O* U6 j; i$ `
// OK, we're authenticated: we need to find and save the data U4 Y( X7 t4 _; m
$ W9 G$ f; ]1 x+ n5 X* Y/ g4 e
// We'll put the data into an array to make it easier to loop though and save3 ?6 i8 o# m6 `3 ^9 l
$mydata['_wp_post_template'] = $_POST['_wp_post_template'];+ ?1 N& a" I0 H8 f
// Add values of $mydata as custom fields/ T6 B; ~2 u/ ~$ j) Q$ W" }6 w! O9 A+ H8 k
foreach ($mydata as $key => $value) { //Let's cycle through the $mydata array!4 ]2 ^! Z+ v [2 i
if( $post->post_type == 'revision' ) return; //don't store custom data twice
1 P& \. e+ S- E3 Q7 ]( P* l8 m $value = implode(',', (array)$value); //if $value is an array, make it a CSV (unlikely)
& l/ ~: ]) ^2 z* I. M* t if(get_post_meta($post->ID, $key, FALSE)) { //if the custom field already has a value...9 `7 w' u5 z7 E6 ?4 U/ M
update_post_meta($post->ID, $key, $value); //...then just update the data* R' h0 }" @( k9 t) M- [" ~4 r" k
} else { //if the custom field doesn't have a value...; _) j* d; @( }8 S2 T4 C
add_post_meta($post->ID, $key, $value);//...then add the data
3 W0 g+ ~( q" k t1 p }* r$ m( q$ B* C- U5 c: J5 m
if(!$value) delete_post_meta($post->ID, $key); //and delete if blank% |3 @7 w5 d( w1 {( |
}$ \$ \7 o+ }% H, y1 N/ c; l: n
}' [; }% f0 t5 d* k
?>
' F8 k2 ~- {6 U* D S( ]7 k
\* o4 |" ?+ y: `, Q$ r, B q( {$ X$ U% t4 N9 x
|