luguokankan 發表於 2013-6-10 13:51 
5 Y% Q `+ c1 x8 \; gpost裡面要設置featured image 8 s' u6 F, z5 B' ~5 q5 W
在這裡面設置嗎?
( K8 o- Y2 y2 d# B. o* Q. Y+ ]. T# P5 x# C+ R
: ~0 N) S" i& F( X0 s5 i7 N8 b
<?php: J- I! Q/ Z3 b
/*) I. Q! E% i8 s
Plugin Name: Single Post Template
1 T2 O3 }" x: |( {: bPlugin URI: http://www.nathanrice.net/plugins7 X/ i7 [/ s8 L l h' p) |
Description: This plugin allows theme authors to include single post templates, much like a theme author can use page template files., g. q: ~, G# P. O
Version: 1.3" U" ~- t4 B2 q: F5 Z, n
Author: Nathan Rice
% [4 q8 S) `- f( B4 ]* nAuthor URI: http://www.nathanrice.net/
% v' I/ A& f: B( C( t0 L q- L |6 T$ e5 j
This plugin inherits the GPL license from it's parent system, WordPress.( |1 }/ H. G+ p) {# U
*/4 p# B" G( g+ W1 Q8 w+ n8 ?
; Z$ G) `- z5 x3 l6 L9 u
// This function scans the template files of the active theme, ; x' n0 `0 L8 U; c+ a
// and returns an array of [Template Name => {file}.php]; V g! E# X# P! i* n5 ?* E1 x7 x
if(!function_exists('get_post_templates')) {
$ J( O* l; B6 J$ pfunction get_post_templates() {$ n2 W( ^/ w" s2 X1 g. f# }7 G4 V( b
$themes = get_themes();( g8 N! I! J( v- {! K
$theme = get_current_theme();
% M7 |5 \* Y& S" b6 @ t# o7 u- u) ^+ r, [ $templates = $themes[$theme]['Template Files'];
1 }3 Y* D$ S# ~2 N" z! ^: v0 O \ $post_templates = array();9 ~/ J* m0 X; H% h. N* _ V
8 H2 r* [5 B! {# ^+ _
$base = array(trailingslashit(get_template_directory()), trailingslashit(get_stylesheet_directory()));4 r/ c4 c7 S' P) |5 U; R4 ?
J9 {2 [1 E/ `5 |$ B; E0 j foreach ((array)$templates as $template) {3 K6 h' Y4 S2 ~ L; R9 B
$template = WP_CONTENT_DIR . str_replace(WP_CONTENT_DIR, '', $template); 1 h: w* l$ H# j! h, g2 l6 M
$basename = str_replace($base, '', $template);
% L, c, A. z9 i; c8 M
$ l7 `, K) X2 f8 U6 S) S4 C // don't allow template files in subdirectories
* U1 w, |9 D9 m4 u2 u8 c! j# F if (false !== strpos($basename, '/'))
' K0 x: J7 x6 m. f/ ^2 C continue;& X6 ?1 D* R' s( i, A | N9 ]4 B
7 U' ^- Y* k6 n+ _/ Q3 E) ] $template_data = implode('', file( $template ));: r8 g3 D2 ?7 Y+ |; x4 ]3 @1 v1 K; W
+ S' {4 B' O& j" x
$name = '';& L' h2 f8 ?& f0 t
if (preg_match( '|Single Post Template .*)$|mi', $template_data, $name))
2 V# P/ m' J+ ?6 I6 a: N& D/ s $name = _cleanup_header_comment($name[1]);5 o, W3 p' @# s! c8 _6 |) f7 K3 C
- ?* A' {7 s8 Y
if (!empty($name)) {
5 T# s3 V, e5 N- a y* ` if(basename($template) != basename(__FILE__))5 l: ]4 j* y* y+ P# o1 L$ t
$post_templates[trim($name)] = $basename;1 g( C. t+ c, w# ~: z
}: z9 s; u5 R$ O6 Q
}
* x. ? h! O5 O4 }3 ^$ F, b5 W; D8 t- |
return $post_templates;
8 T# j$ P. [( z7 [. J w
, A: K( X' a7 J% s. ]( ^' U' L}}9 k& O& `& _4 [: O' J4 i
' B( a+ a+ u" j9 Y; i5 j- g
// build the dropdown items
/ B( u) X& B# n B6 I+ D Hif(!function_exists('post_templates_dropdown')) {
' u7 M6 E' N$ |3 Y! x1 Ufunction post_templates_dropdown() {2 u3 _! B: c( i2 p% `% x: P H z
global $post;
( a# R0 s, Y3 d $post_templates = get_post_templates();
: z& c) t' l Q' b. m/ g1 u 2 E# W8 A. @+ @1 p, p, [
foreach ($post_templates as $template_name => $template_file) { //loop through templates, make them options; c+ m) u- x9 v( q9 P
if ($template_file == get_post_meta($post->ID, '_wp_post_template', true)) { $selected = ' selected="selected"'; } else { $selected = ''; }' F: [1 d- H0 U7 O6 V4 } g
$opt = '<option value="' . $template_file . '"' . $selected . '>' . $template_name . '</option>';( ]& [. t% s L& G3 W" M
echo $opt;8 t8 \3 d& H7 [2 _
}
, Z/ K* A1 D$ U1 n}}8 ?8 L$ |/ H4 j4 D4 r! _) B
' C$ F+ W; B+ [' m
// Filter the single template value, and replace it with/ B: I3 v* H- W; q6 H0 `; M
// the template chosen by the user, if they chose one.
" s; x2 S/ I+ i5 \, H, kadd_filter('single_template', 'get_post_template');
7 {' L# b+ p( C- r/ Q- @0 t6 mif(!function_exists('get_post_template')) {& l+ Q, h3 s5 `
function get_post_template($template) {
1 D5 I3 U. Q! j3 f `/ @ global $post;
" c, S& }, O/ z! |( t $custom_field = get_post_meta($post->ID, '_wp_post_template', true);
. _( B, K' k9 [ x v" | if(!empty($custom_field) && file_exists(TEMPLATEPATH . "/{$custom_field}")) { 7 l& ?, `$ A. F
$template = TEMPLATEPATH . "/{$custom_field}"; }
: b2 N* @4 e1 @9 k8 e. ]. O* K return $template; K: g- n6 k0 z% u/ v3 m$ b
}}
; {1 B+ C6 Y# q
0 ~! S4 l) Z! |2 ?8 I7 ?// Everything below this is for adding the extra box
0 }0 W/ V4 p# c% J// to the post edit screen so the user can choose a template L7 C4 L/ Z$ @8 W* Q0 z
2 f$ B: g" g! W- S1 |5 c7 e// Adds a custom section to the Post edit screen
% G8 z3 z0 s$ x( d$ Uadd_action('admin_menu', 'pt_add_custom_box');
o$ q; ?8 T7 C9 f* S) g" p! Pfunction pt_add_custom_box() {* ~& H, e% ~6 g& @3 J3 ~
if(get_post_templates() && function_exists( 'add_meta_box' )) {% B% x1 k3 S# E( ]
add_meta_box( 'pt_post_templates', __( 'Single Post Template', 'pt' ), 4 T/ ?5 A; n7 B' d2 q3 }
'pt_inner_custom_box', 'post', 'normal', 'high' ); //add the boxes under the post
% A% Q5 s& ~2 Q. `. D. A }
: y5 B6 j. w5 Q/ @) o0 A: c7 ^ z}7 }8 V; }+ p# C
* ^( m4 d/ J' F) v, u; l* N! l
// Prints the inner fields for the custom post/page section
( s$ t$ B9 E" k5 R( Y# y# y1 u* G* @- b2 Vfunction pt_inner_custom_box() {4 Z, ]; c; H( ?, G9 B6 f* `
global $post;
8 B3 B+ C e- B+ K$ A // Use nonce for verification- p1 `4 s* p/ v3 ~' U" |5 E
echo '<input type="hidden" name="pt_noncename" id="pt_noncename" value="' . wp_create_nonce( plugin_basename(__FILE__) ) . '" />';. P/ e- w2 {0 z' S, t/ Y
// The actual fields for data entry
4 i/ ~7 J9 J& r/ L: u+ J echo '<label class="hidden" for="post_template">' . __(" ost Template", 'pt' ) . '</label><br />';: v& c* j2 x% S( j( C: c
echo '<select name="_wp_post_template" id="post_template" class="dropdown">';8 J' m* V% C, b
echo '<option value="">Default</option>';) f( ^9 `7 Q6 w* c
post_templates_dropdown(); //get the options
% G, L9 q( t) y2 H6 J echo '</select><br /><br />';: e0 z" ^4 l0 L8 ~
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 />';! r7 l" B: L$ k
}7 Q- d* e" E( @* S
0 B0 V- H( n0 u. x4 e// When the post is saved, saves our custom data0 s% v- B$ Q7 R+ w) C
add_action('save_post', 'pt_save_postdata', 1, 2); // save the custom fields1 d) v% C; R$ C1 {4 T4 k( `. g
function pt_save_postdata($post_id, $post) {: [* J O Y M# U6 P4 Y
) R- b, ^5 O! L8 M3 o // verify this came from the our screen and with proper authorization,5 E. F. y. \6 @
// because save_post can be triggered at other times
, ]/ X4 R' j/ |+ C z, F if ( !wp_verify_nonce( $_POST['pt_noncename'], plugin_basename(__FILE__) )) {; B+ H7 {$ ^1 `
return $post->ID;" P" T# f0 X) U: P s
}. |% E( `) t) t" [1 V, B( H
6 S' e) M. q9 P" a2 u/ V+ w
// Is the user allowed to edit the post or page?9 |+ q# R! p v
if ( 'page' == $_POST['post_type'] ) { s$ W9 | ~$ R; Q2 \
if ( !current_user_can( 'edit_page', $post->ID ))) u" X4 e6 d, S) a, ~+ O
return $post->ID;0 F" H6 u8 W- s+ I
} else {
9 j& I' x5 y6 k4 e if ( !current_user_can( 'edit_post', $post->ID ))
C+ D" X' a8 P2 J) {0 y return $post->ID;7 f/ i4 \2 [' P, E8 |7 X
}& I G0 `3 p; v/ L- {( T
e6 G+ \, X- P* z7 e
// OK, we're authenticated: we need to find and save the data
/ b" A: n, n N8 V
# L7 l7 ^, c0 A2 v5 u8 p; D // We'll put the data into an array to make it easier to loop though and save
9 ~& y; v' V: Z7 t; ~4 y4 R $mydata['_wp_post_template'] = $_POST['_wp_post_template'];
# w# O1 [9 y7 P, K // Add values of $mydata as custom fields) ?$ m5 p0 s$ @
foreach ($mydata as $key => $value) { //Let's cycle through the $mydata array!
& T' M. a* j+ H8 N) j1 M if( $post->post_type == 'revision' ) return; //don't store custom data twice0 L% Y' J. Z- ~5 ?# T9 g# b
$value = implode(',', (array)$value); //if $value is an array, make it a CSV (unlikely)
% c9 x% `+ d0 q% N2 M) X if(get_post_meta($post->ID, $key, FALSE)) { //if the custom field already has a value...- ^2 s% ~7 T& B1 U
update_post_meta($post->ID, $key, $value); //...then just update the data8 u) }5 `1 D8 \' @4 q# Y2 x/ X
} else { //if the custom field doesn't have a value...
; n2 I% [* z! V add_post_meta($post->ID, $key, $value);//...then add the data
9 j: K5 w: n0 v8 B, }) n }; b, P8 H1 Q" k. b* m
if(!$value) delete_post_meta($post->ID, $key); //and delete if blank, o) g. Q* d% b, N* k) L; e: E
}
/ ?/ x) o$ l% `0 W}( ?. i& k, P; m
?>* T, o9 X% m v. I2 }' n* @
. R7 j* O9 }2 u3 R6 V5 V8 q
`" L/ o# j! }/ V/ r$ c |