luguokankan 發表於 2013-6-10 13:51 
' ` y$ O# r3 Zpost裡面要設置featured image
! H. z, \: k. v$ {9 t在這裡面設置嗎?% I6 D( w0 R: d/ p/ l( Q6 U' r
0 M$ ?/ ^0 n6 w/ i% e4 [
8 e4 M" m6 u, }2 w% r<?php4 a1 E" |; V! p
/*
1 c1 ~, g) f5 i" g& kPlugin Name: Single Post Template
4 c- l( @" z1 FPlugin URI: http://www.nathanrice.net/plugins, y+ M+ ^' m- c) K$ V
Description: This plugin allows theme authors to include single post templates, much like a theme author can use page template files.! b; X7 r4 z" X6 G4 G( }9 N X8 j
Version: 1.3
- N- v1 Q' I# M/ D$ F8 V( e, |Author: Nathan Rice G. K" }/ c; j* _ ~- r4 F
Author URI: http://www.nathanrice.net/
" o: @+ W# G8 B, S
6 ] U0 h/ C" \2 \! x- g0 ^( k7 J" j: \This plugin inherits the GPL license from it's parent system, WordPress.2 d5 C" g/ E! m; u4 q/ \; ^
*/
. @" h; j* t* T! V* l `' O- J9 D% M
4 J7 F9 |9 v. A+ W" J# c+ [% v// This function scans the template files of the active theme,
0 N% ] |3 e1 B9 x2 W$ N4 o. m// and returns an array of [Template Name => {file}.php]- G$ h- f w7 ]
if(!function_exists('get_post_templates')) {
& J: e9 s0 g* U8 p; Q' _7 ifunction get_post_templates() {
& X& {5 E7 G2 m' _ $themes = get_themes();
. q; H4 _1 H' {% w+ k& V. y( F $theme = get_current_theme();0 L2 C5 y6 R3 t) g
$templates = $themes[$theme]['Template Files'];
4 J' o. c0 ]/ Y a2 Z1 t $post_templates = array();
* x& v6 a( d# a! H) z
) K$ n. S; ]4 r4 R $base = array(trailingslashit(get_template_directory()), trailingslashit(get_stylesheet_directory()));
7 C. K! A0 x! T, Q D% X9 r. d' a( c) H" u. l( a9 J
foreach ((array)$templates as $template) {9 u% _8 b" p; U4 r
$template = WP_CONTENT_DIR . str_replace(WP_CONTENT_DIR, '', $template); * l' z* R! }/ c Y
$basename = str_replace($base, '', $template);0 s- N' Q5 N5 W' g4 l
( Y0 B* K4 P, C2 C2 a- k // don't allow template files in subdirectories. Y; q+ e- F, g! z: t ^- c
if (false !== strpos($basename, '/'))$ t* g1 ~7 c& ~+ J1 L5 o
continue;
1 `* I; @. C9 D; I" Z: w+ |4 N/ C& y3 @" z; W& u
$template_data = implode('', file( $template ));
1 H2 D) J6 F4 [0 J( }: E9 o7 Q# ~% k1 t
$name = '';
1 o) @( B: D9 p7 B if (preg_match( '|Single Post Template .*)$|mi', $template_data, $name))5 L g% T; k- ]" r, e
$name = _cleanup_header_comment($name[1]);
" D7 D6 J5 P& c% _ s# p& ^" v, H, q
if (!empty($name)) {
2 B+ ]: J6 k: @) g- m+ g if(basename($template) != basename(__FILE__))
# {, J: a3 _* g: w7 L $post_templates[trim($name)] = $basename;& L+ o$ r& x' g) r6 c
}0 j1 Y2 ]: _2 n/ b# ?
}
/ e& m$ k- g3 W2 Y) ]: u) k6 W6 j" R- ~# X( t) {. R% q- G% J
return $post_templates;9 b `5 _1 p8 m; F% k, f: I
6 Q) F" i0 V! }& L& c& ?
}}# R8 d* h" F& C- m! G, A
) [) L0 t) U2 [& T2 [8 p
// build the dropdown items6 p# M; ]4 R8 j- s
if(!function_exists('post_templates_dropdown')) {
6 y5 a7 O- V- }function post_templates_dropdown() {
0 s7 K- N5 W' a6 ~; y4 _7 n global $post;
! W% P0 _ }) s! `: s7 j3 V $post_templates = get_post_templates();
1 K% v+ }9 O4 u0 S ( s2 O, L* D+ N& |0 R/ K
foreach ($post_templates as $template_name => $template_file) { //loop through templates, make them options+ b* q0 [, t- w0 N/ d
if ($template_file == get_post_meta($post->ID, '_wp_post_template', true)) { $selected = ' selected="selected"'; } else { $selected = ''; }$ l$ W& L R4 O$ r, `1 G, F
$opt = '<option value="' . $template_file . '"' . $selected . '>' . $template_name . '</option>';7 F( R0 B* r) f3 j9 V
echo $opt;* m u- X9 A' ]+ o9 ~
}
- r: ^# D; F, N' {}}
3 I, D' ?( N3 k9 T6 y! R0 |6 |; z! h4 u8 X) L* ^& g# V
// Filter the single template value, and replace it with
# }( ^- A0 o+ ?3 f2 m9 c. q) F// the template chosen by the user, if they chose one.( S/ ~; L* S9 o" v3 r
add_filter('single_template', 'get_post_template');! O5 J8 v9 R; C% ~" n# R& p! Q
if(!function_exists('get_post_template')) {
. j/ M! @1 v I5 U5 A2 [7 ^6 afunction get_post_template($template) {( o/ d+ t0 z; g5 e7 m7 y) V
global $post;
6 u$ E0 `0 x: x $custom_field = get_post_meta($post->ID, '_wp_post_template', true);& P3 c, `8 G6 h1 J! e
if(!empty($custom_field) && file_exists(TEMPLATEPATH . "/{$custom_field}")) { 7 ~$ r2 z5 Q( {+ X+ h) U3 C
$template = TEMPLATEPATH . "/{$custom_field}"; }+ e, I! p7 o' M
return $template;( d/ m5 }3 f$ q- A3 O# } G3 C1 F
}}" j, {7 C& W# o$ v' Q; m) O
! n, q( e) g" p9 g8 v
// Everything below this is for adding the extra box
: c' Z8 q5 N# H) }// to the post edit screen so the user can choose a template% f! J' k) b! @/ _$ e$ Q1 R, I0 S* a
1 N7 _# _7 s% s1 F// Adds a custom section to the Post edit screen& z1 i# p0 q& Q" i
add_action('admin_menu', 'pt_add_custom_box');
( c% d& ^9 V2 u' [! B/ ffunction pt_add_custom_box() {
. k2 L( c6 S' Y; E) t if(get_post_templates() && function_exists( 'add_meta_box' )) {
! @3 K% F, j& e% y. j" R- e+ P add_meta_box( 'pt_post_templates', __( 'Single Post Template', 'pt' ),
p3 C; O% }* I. V 'pt_inner_custom_box', 'post', 'normal', 'high' ); //add the boxes under the post+ w9 C0 T/ k4 l. _, _: x$ ?
}0 B5 D! S1 R3 |3 t
}
: U! t3 b& U8 O. Q. C# T) g2 R" p3 j" s
// Prints the inner fields for the custom post/page section1 S8 _2 w7 d6 k
function pt_inner_custom_box() {
6 A$ A( s( y! `' n; v global $post;
* W g# W ^) ?( ^ // Use nonce for verification3 U! T9 z/ {/ Q. l
echo '<input type="hidden" name="pt_noncename" id="pt_noncename" value="' . wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
0 S8 ?0 p. j6 V. R // The actual fields for data entry
" s; _# u$ i1 J0 I/ h echo '<label class="hidden" for="post_template">' . __(" ost Template", 'pt' ) . '</label><br />';
$ J" h4 |5 d1 O echo '<select name="_wp_post_template" id="post_template" class="dropdown">';
( b. b( f& W. |/ Q' V, B echo '<option value="">Default</option>';! l# d( S8 [7 c+ Z3 b: y
post_templates_dropdown(); //get the options
+ E5 x8 j1 e7 Y$ g1 r( l echo '</select><br /><br />';
: Q# Z" H ^, b3 ~7 q4 ? {; e7 J% \ 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 />';
# y& m/ v- G" H: _4 [}
8 C4 Z! }4 V" S8 n
. ]/ p; L# ]+ Z# K// When the post is saved, saves our custom data6 E4 `0 \: K; g3 C( {
add_action('save_post', 'pt_save_postdata', 1, 2); // save the custom fields
% ?/ h+ W. V& y; tfunction pt_save_postdata($post_id, $post) {
' T! }. U9 M H* n' T/ s
* K8 J9 D* P2 p3 e3 B" K/ N // verify this came from the our screen and with proper authorization,
: L5 H; j$ p& ~ @# m // because save_post can be triggered at other times6 }6 k% t5 A( n
if ( !wp_verify_nonce( $_POST['pt_noncename'], plugin_basename(__FILE__) )) {
0 f, w3 D1 O# @# c+ j9 n return $post->ID;
$ T9 \5 r2 F' O6 e8 u3 h+ ]6 e: Z }2 X- u+ x ?; S5 E$ B
$ M1 K9 |( I+ M7 V" l; y) k& Z t
// Is the user allowed to edit the post or page?
, |& i" ~% p: }. e if ( 'page' == $_POST['post_type'] ) {
4 p0 u2 l F1 V# p3 _ if ( !current_user_can( 'edit_page', $post->ID ))/ M, D& n( h( C, N
return $post->ID;
2 b' I- U# X. Q# i7 A } else {
$ o9 V' O( D3 F7 m. ?; o U$ x if ( !current_user_can( 'edit_post', $post->ID ))
! T* [5 z; a4 c# e, G# U return $post->ID;
5 @4 o: f" U& F7 l4 H3 V }* T& E0 s% P9 I: N. r
1 b( v) E- U z+ z
// OK, we're authenticated: we need to find and save the data
5 ?& c2 S3 K* m& ]- J 5 P& [ A- Q, B! s. g
// We'll put the data into an array to make it easier to loop though and save
: f- z3 m `% H. O $mydata['_wp_post_template'] = $_POST['_wp_post_template'];
: p! \( G$ v0 [4 E, [! y; n' I8 R. e // Add values of $mydata as custom fields
8 |# Y2 t8 V) @( Z' D) ]/ h foreach ($mydata as $key => $value) { //Let's cycle through the $mydata array!1 `- }! {2 U; G9 K' s8 e& w& T
if( $post->post_type == 'revision' ) return; //don't store custom data twice S. H" u. x0 }: k6 D4 ] U
$value = implode(',', (array)$value); //if $value is an array, make it a CSV (unlikely)+ i9 }, o/ e5 w
if(get_post_meta($post->ID, $key, FALSE)) { //if the custom field already has a value...& q ]* r \( k+ \0 u' E$ Z8 J
update_post_meta($post->ID, $key, $value); //...then just update the data% p) t8 E1 w% T, }0 e$ p
} else { //if the custom field doesn't have a value...
" z ~$ b1 E/ ]* C2 ?* Z add_post_meta($post->ID, $key, $value);//...then add the data
2 |# {- S) E6 A# a3 J! A" R0 L }& B c% z @2 |. p
if(!$value) delete_post_meta($post->ID, $key); //and delete if blank
" {$ E: X! J" u+ f' u } s" {& Z7 n+ `
}
* Y; J+ z$ H: z% H?>
2 Y2 }" M! Y9 E0 @
1 n. C! U! `7 v5 n/ c6 g* B% d/ N9 }1 o
|