luguokankan 發表於 2013-6-10 13:51 
( }8 s1 o8 J+ Q; n' Rpost裡面要設置featured image & J- @5 V% M0 q
在這裡面設置嗎?
! x2 V4 E9 H |6 [: J0 O! N5 I0 t" a6 p
; x# `: w* O: A! ~
<?php
, c8 U5 }. M7 m. ]+ p: H6 w/*2 S6 p z# w7 ?+ I+ Y+ `
Plugin Name: Single Post Template
' R5 E6 G! }* C" x8 j( U- XPlugin URI: http://www.nathanrice.net/plugins; @, _9 w V) Q) u/ a; p. r2 q* d5 f! k
Description: This plugin allows theme authors to include single post templates, much like a theme author can use page template files.9 }* [# {5 Z+ f8 W2 F$ |
Version: 1.3
& m: n: k, x6 A8 j7 L, }Author: Nathan Rice* ?; a6 ~ ?/ p
Author URI: http://www.nathanrice.net/
9 E: [( c# S3 W& @' u" Q- Z# O0 M
. P9 Y f7 {. V% UThis plugin inherits the GPL license from it's parent system, WordPress.: B0 {1 q Z2 r8 g
*/
8 y+ ^% l" c! ~: L2 j# r. s3 t/ ]5 Y. |( K6 M* n
// This function scans the template files of the active theme, : [& r- R5 J0 s; [9 Y- L
// and returns an array of [Template Name => {file}.php]
) g" v- u1 _# c: t3 \0 X* Nif(!function_exists('get_post_templates')) {
0 A6 I% a2 p4 k2 {$ @6 jfunction get_post_templates() {
; u8 Q& t, ?: C$ X; @' z+ Y $themes = get_themes();
% q g8 V4 ?" S1 [3 h# d $theme = get_current_theme();
- h9 y8 T2 f: W6 p p $templates = $themes[$theme]['Template Files'];
2 z, V2 s$ X$ x $post_templates = array();( U( ?+ R" P0 R" p p5 U7 S0 Z* w
3 g% q `$ c6 S+ ~1 {. | $base = array(trailingslashit(get_template_directory()), trailingslashit(get_stylesheet_directory()));1 d5 C/ n3 v/ U8 B& y$ @
" i) I2 d: m2 a7 \" n
foreach ((array)$templates as $template) {1 V9 D7 W+ g# y _& N6 I b
$template = WP_CONTENT_DIR . str_replace(WP_CONTENT_DIR, '', $template); # [; b" ?+ k s A; m/ v
$basename = str_replace($base, '', $template);! x2 n: [4 _* |, p, O
7 w; |+ |' E( A6 ~' ^% f
// don't allow template files in subdirectories
6 E+ J6 f& J! W. [( F& e# Z. ` if (false !== strpos($basename, '/'))% G5 {: q) f" ^( t( k
continue;* P7 L8 Z! A8 h
' c6 A4 d& D _+ u8 Z
$template_data = implode('', file( $template ));
0 u" m; D' w, j. F' s( }% `: _! H' Q6 m1 G
$name = '';6 U. l1 w9 M2 Q/ o% N- ?, b
if (preg_match( '|Single Post Template .*)$|mi', $template_data, $name))# N5 E4 v: L( O2 D1 O' X3 _% }$ [
$name = _cleanup_header_comment($name[1]);
. T' B) J- Y, ~: Z; w' i8 p7 I2 `# G* v; f1 _. r* [
if (!empty($name)) {
" l: h& k- e; N6 |& w1 T2 S if(basename($template) != basename(__FILE__))
4 ?: n; N. Z& x/ W1 y! ? $post_templates[trim($name)] = $basename;% }& l- r" K A9 ?
}/ {; }, J, Y6 C" x, @
}
! e/ n. U0 x. v4 I4 ^, F; k
) i a" G" q/ G }$ F8 h+ l$ Y U return $post_templates;1 w/ ^- G; y1 Y! _0 z8 D
3 p, L; `: \9 G& p
}}
5 {! l* W3 k% s# B# O0 E4 W
7 k) \. _- g% \# P1 v// build the dropdown items( h2 ^- l. e1 p0 H- _. L: \
if(!function_exists('post_templates_dropdown')) {
% q2 y+ v! K9 m/ v: F, rfunction post_templates_dropdown() {1 m# {" C- H$ A3 c, H4 S
global $post;: i* c3 Y9 [, D9 `; J1 @' q
$post_templates = get_post_templates();: R$ N! Z T2 s7 I
8 _. B- V0 x6 C* C/ M0 f
foreach ($post_templates as $template_name => $template_file) { //loop through templates, make them options8 |8 d* P' @# ^3 [4 p
if ($template_file == get_post_meta($post->ID, '_wp_post_template', true)) { $selected = ' selected="selected"'; } else { $selected = ''; }8 ?' G, u8 S% \ X4 B
$opt = '<option value="' . $template_file . '"' . $selected . '>' . $template_name . '</option>';
3 _; b. D# `4 ]4 m- v: m echo $opt;7 K, e' T: {& M! n& K3 U
}
! u7 Y5 b; ]+ ` r( N}}2 C S! k- f/ o6 \+ L; J
' R9 v. R' A6 G% {. f# `. R
// Filter the single template value, and replace it with
& j/ [7 B5 ]9 p" a4 x! z// the template chosen by the user, if they chose one.
- [# f# _1 u0 ^/ v- x5 m2 M% @: l9 Cadd_filter('single_template', 'get_post_template');
6 \( u5 ^! r, l3 E& e* S8 ~if(!function_exists('get_post_template')) {
, Y1 I% S, I) j. `+ ffunction get_post_template($template) {2 I/ V! ^6 o! ~) C3 g3 ]: x
global $post;
0 f* f$ E6 c/ U6 w $custom_field = get_post_meta($post->ID, '_wp_post_template', true);
2 L) V0 }9 w. G7 i1 H' | if(!empty($custom_field) && file_exists(TEMPLATEPATH . "/{$custom_field}")) { " p6 H' k: K/ _0 ]( x
$template = TEMPLATEPATH . "/{$custom_field}"; }
. C' S* k9 c0 W" ?( P' }' S" S! S return $template;
" h X- V V. g4 N}}3 [0 \* a9 a* g* P
) H4 Z: C/ A% P8 L, L
// Everything below this is for adding the extra box
; B0 a) g, q7 @( X, x, @$ Z// to the post edit screen so the user can choose a template
! e: L3 C1 ^& ^1 \( U/ D' B% |( F& K) _
// Adds a custom section to the Post edit screen9 k& f' y- K" Z
add_action('admin_menu', 'pt_add_custom_box');) ?- I! W3 F; X
function pt_add_custom_box() {. ^, y5 r& ~, O3 H5 }, k# S$ c0 Q
if(get_post_templates() && function_exists( 'add_meta_box' )) {, r7 l$ H% L0 p, N* s
add_meta_box( 'pt_post_templates', __( 'Single Post Template', 'pt' ), 9 S& O/ q4 \: \4 O0 M) g( [( U) o
'pt_inner_custom_box', 'post', 'normal', 'high' ); //add the boxes under the post
: m( D7 h' k$ m2 ? }
# H0 d2 Y% f) L; a}& W0 R7 r5 h& U
" S+ R5 a$ v3 F+ o) K// Prints the inner fields for the custom post/page section
' e; R" |, t, `. |2 m& ^8 {' ifunction pt_inner_custom_box() {
$ ~; f3 x: }+ w2 [2 m- C' G global $post;
. U3 d& R H5 J, p5 b+ l7 _7 ~# D3 Q P // Use nonce for verification
3 I4 p9 D: N! p5 k% Y2 A echo '<input type="hidden" name="pt_noncename" id="pt_noncename" value="' . wp_create_nonce( plugin_basename(__FILE__) ) . '" />';* J0 o2 Q9 l R( A( ~8 {' |6 B6 L
// The actual fields for data entry0 |2 \, E( }/ G" C4 P
echo '<label class="hidden" for="post_template">' . __(" ost Template", 'pt' ) . '</label><br />';
+ i# T6 B5 E% ^, o echo '<select name="_wp_post_template" id="post_template" class="dropdown">';8 F3 h1 `) e* m* N6 a2 Y
echo '<option value="">Default</option>';& _- f3 t' k8 K6 g. O, X3 }
post_templates_dropdown(); //get the options
; h% X8 e9 f% Z" i _- h echo '</select><br /><br />';
3 I/ X H" ~" e 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 />';
1 A: D# A7 Y0 g6 n7 z1 A}* Y N: }' K3 V) Z# s( J* v
& _8 [+ {; O6 N& ], C
// When the post is saved, saves our custom data
1 `: Y! p7 X M4 Jadd_action('save_post', 'pt_save_postdata', 1, 2); // save the custom fields+ K* ?; E4 p; o- \' ?5 o9 z
function pt_save_postdata($post_id, $post) {
( Z/ _% N5 B: L& m
" F. l6 I! g! h! f: Y // verify this came from the our screen and with proper authorization," _2 c% M6 f" w1 q8 e
// because save_post can be triggered at other times
& L: B* U4 Q0 _1 w. e, \, f6 |. D if ( !wp_verify_nonce( $_POST['pt_noncename'], plugin_basename(__FILE__) )) {/ U, e; J+ B/ j) l4 O @
return $post->ID;
) `4 w% N+ W: a# ^ }) f i; W2 }/ [2 P- x
% H: ?3 H9 Y; Q! ?+ x* H) I // Is the user allowed to edit the post or page?
# k* c0 r9 v5 \ if ( 'page' == $_POST['post_type'] ) {
4 j9 B) e- X# | if ( !current_user_can( 'edit_page', $post->ID ))% ]4 |* P' K" ?7 Q+ G
return $post->ID;9 m7 t5 i6 L) C% ]/ a
} else {* G4 {5 Y5 N# h4 |% g
if ( !current_user_can( 'edit_post', $post->ID ))( u3 q9 d. d' |7 F" y
return $post->ID;/ Z1 \# O& L) u( d$ Q D
}# _% a; h1 T k; l7 S
( r9 s( i! I. s6 |
// OK, we're authenticated: we need to find and save the data
4 l+ u) q) T9 y x$ y: J! I ' Q# I/ p9 g: {! B }
// We'll put the data into an array to make it easier to loop though and save
1 q& p& U$ `3 B0 G $mydata['_wp_post_template'] = $_POST['_wp_post_template'];9 s) F k: p& Z( n4 F9 p% L4 X
// Add values of $mydata as custom fields2 W$ T( s; [& G1 V8 `! A* y
foreach ($mydata as $key => $value) { //Let's cycle through the $mydata array!
: T! e" G t* I if( $post->post_type == 'revision' ) return; //don't store custom data twice0 f' w* B# I! ]3 ?1 R/ F& y
$value = implode(',', (array)$value); //if $value is an array, make it a CSV (unlikely)( D- O, b8 B0 K9 E
if(get_post_meta($post->ID, $key, FALSE)) { //if the custom field already has a value...1 y6 r% N) [3 ]( H7 \
update_post_meta($post->ID, $key, $value); //...then just update the data* U. D; ]( n0 \3 N7 U
} else { //if the custom field doesn't have a value...
- u# D; d2 y$ s# R/ X add_post_meta($post->ID, $key, $value);//...then add the data
0 w, O4 e( P7 _& j* B3 n$ e }
# o0 z6 h: ^8 A: E/ K: ]# | if(!$value) delete_post_meta($post->ID, $key); //and delete if blank
7 G! R) c) d; v# x7 e6 e8 y. Q }+ b8 S' k" [* K3 L
}
; a+ X; T3 G3 H3 l+ A?>
4 a; E3 S' X0 d0 ]1 f2 X9 T4 ]# T8 Z/ P; r8 ^
2 U4 x X7 y6 x7 S |