luguokankan 發表於 2013-6-10 13:51 
' X' q( Q1 c& g- @( cpost裡面要設置featured image
! ^/ D- x0 q! h# d: S% B在這裡面設置嗎?5 Z8 s: o0 {# e' d" Q9 b
; B5 p' t; M7 a Y( f* n7 Y4 H7 n. c [
<?php% b9 d" h3 X, ^" m, J I
/*! l) @1 p1 p# @( m T" B; E. H$ A
Plugin Name: Single Post Template
. p c1 |+ x5 M. [3 A7 BPlugin URI: http://www.nathanrice.net/plugins
, {/ d; T. Y: c- F4 ]6 |% T% o' IDescription: This plugin allows theme authors to include single post templates, much like a theme author can use page template files.
' L" I1 q1 q, W% l' oVersion: 1.3
4 B, ^2 z! u3 h1 Z7 c9 @# F5 Z# fAuthor: Nathan Rice+ J, m( K- m4 N( \
Author URI: http://www.nathanrice.net/
. q6 G7 u! ^; E
, K9 O& V6 Y8 q4 R6 E1 qThis plugin inherits the GPL license from it's parent system, WordPress.4 n* s6 a- O9 S
*/2 F; d! b$ H, ~( d& {
4 A, E1 A9 O n
// This function scans the template files of the active theme, , G% ]: K& t. H6 \
// and returns an array of [Template Name => {file}.php]
. @' W2 L8 z6 B7 _if(!function_exists('get_post_templates')) {5 |) N* C" |( m+ {, d! o- ]
function get_post_templates() {: Y! U+ Q& z- q3 u V* L
$themes = get_themes();+ y& L$ g& l' U% u: l8 K
$theme = get_current_theme();$ x7 q8 c! \0 x( v5 z
$templates = $themes[$theme]['Template Files'];/ U% j- O( @9 j) j1 C
$post_templates = array();
1 ]3 V3 ^2 m4 b$ ?6 `* Y& B* f+ }$ @0 X) T
$base = array(trailingslashit(get_template_directory()), trailingslashit(get_stylesheet_directory()));6 \% P3 h \ O! f$ |9 s
: V* S+ U ` @, K
foreach ((array)$templates as $template) {
- ?% T+ X! n8 D) d9 J9 o $template = WP_CONTENT_DIR . str_replace(WP_CONTENT_DIR, '', $template); 0 K ^% x" P4 u7 e6 a5 i
$basename = str_replace($base, '', $template);
' _6 ]7 Q. h; D# Y
* B& W5 E4 r% y$ h' s- w7 _& ~ // don't allow template files in subdirectories
g# l( q: m1 F$ o; V" G3 A if (false !== strpos($basename, '/'))
6 E' `$ }0 G& J+ n- W) n continue;/ N; V# d# f) c. w f& A3 {
2 P8 d" U7 T+ J! N* l $template_data = implode('', file( $template ));/ Q+ ], j/ K1 z8 T
4 V& \. O; e) d5 p2 K
$name = '';, s ~& x# Q8 t& {4 }
if (preg_match( '|Single Post Template .*)$|mi', $template_data, $name))" x% ?: u7 A7 V2 Z6 B: Y+ ~9 y
$name = _cleanup_header_comment($name[1]);
& n# I" M2 ]' M4 I+ G" C, H! W' u# S6 `3 J
if (!empty($name)) {
J) I' [( t4 c' s. l! M( \! [& x if(basename($template) != basename(__FILE__)) s) }9 v/ {7 }3 [, j$ u. s }
$post_templates[trim($name)] = $basename;& N6 U+ x3 E' J& h/ p, @ A; e7 x
}0 u, {% [7 ~- s5 B+ m8 z$ p% I
}
* Z/ I9 [& V9 F
! f, h: `2 |% R: m; y return $post_templates;8 z% O- ]# B9 z' f' Q% q/ T7 j
! u. g8 c( {* }. t# R
}}1 z! r' c9 Y) A @& ~* D0 s2 e
* T; M# N4 Y3 u// build the dropdown items
. b0 ?2 c0 Q T) M' ~+ {if(!function_exists('post_templates_dropdown')) {
, O0 K, ?% |6 zfunction post_templates_dropdown() {& h O5 \# \+ T( B
global $post;
, P$ ^; I8 l |' c $post_templates = get_post_templates();/ r3 p% H' Z. V. A: `) b( {
# H, k3 ?; r# J$ \. v" U! a foreach ($post_templates as $template_name => $template_file) { //loop through templates, make them options
. G+ D0 T" ^, l% R/ s. ~; ~3 `# J. x if ($template_file == get_post_meta($post->ID, '_wp_post_template', true)) { $selected = ' selected="selected"'; } else { $selected = ''; }
3 ~/ W; m* _: F* o $opt = '<option value="' . $template_file . '"' . $selected . '>' . $template_name . '</option>';
4 } f% G$ a# g: y, N echo $opt;( c2 T& z4 q9 _0 Y. w
}
% |# U: N, y! r3 m# |( a}}. F @6 L1 e' j9 j5 \
6 h7 p* o0 C( z- e! v. h// Filter the single template value, and replace it with
! c! q# g5 k9 Z: F0 g// the template chosen by the user, if they chose one.
{& f8 G0 L. o8 u; m9 `5 D, @add_filter('single_template', 'get_post_template');
# A1 i+ i! D4 ], g$ dif(!function_exists('get_post_template')) {- q5 ~0 V9 @+ C& a, c/ j4 B( i; N* e
function get_post_template($template) {
3 e u* g ]( t" e global $post;; T5 W- F, D7 K$ }
$custom_field = get_post_meta($post->ID, '_wp_post_template', true);3 Q0 ~7 c* u" `, g0 u, x
if(!empty($custom_field) && file_exists(TEMPLATEPATH . "/{$custom_field}")) {
0 J) w/ i) X7 g ~# | $template = TEMPLATEPATH . "/{$custom_field}"; } A$ v0 W! Q. c4 a# |4 p2 K7 t- d I
return $template;
$ j/ }! P. f: u) D" Z' _}} L+ G7 A6 N( T- F" x$ ?* ?
6 _9 `8 Z4 i$ X/ K+ v9 k0 O// Everything below this is for adding the extra box; Y; ?6 Y; P0 L- A
// to the post edit screen so the user can choose a template/ J0 X2 V- v' `' C, [$ } _
: ?" f# \7 ?# w6 ]2 A// Adds a custom section to the Post edit screen
, o, `9 d8 b$ Hadd_action('admin_menu', 'pt_add_custom_box');9 }# G0 y+ w- B' \: f0 H
function pt_add_custom_box() {
p1 f7 i6 v! f" W* Y( \' Y2 b. V if(get_post_templates() && function_exists( 'add_meta_box' )) {
6 n/ a2 E8 O( d: N add_meta_box( 'pt_post_templates', __( 'Single Post Template', 'pt' ), / z2 T5 }* B0 y0 M; ^' g/ Z
'pt_inner_custom_box', 'post', 'normal', 'high' ); //add the boxes under the post
' R) l, P. D+ C7 ~4 x' A3 G/ K }
' k* Q9 q& a6 u4 q' |7 L}
2 J/ k8 @* X4 ^6 u
# | ?6 R& F; P$ r0 R// Prints the inner fields for the custom post/page section% P# g- z* b# a, P
function pt_inner_custom_box() {
2 w' D2 ?5 N- z3 [: Q& x global $post;
+ R% J4 a9 R+ t/ X5 P/ `0 W // Use nonce for verification
6 J D% E/ R- Q4 |. |' S echo '<input type="hidden" name="pt_noncename" id="pt_noncename" value="' . wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
1 D( c' Q; |4 ], h! g3 Z // The actual fields for data entry
T; U a/ L5 z9 f+ \ echo '<label class="hidden" for="post_template">' . __(" ost Template", 'pt' ) . '</label><br />';+ _7 Z# z( x' \4 j. w0 ?
echo '<select name="_wp_post_template" id="post_template" class="dropdown">';
& g' ^5 a, H& y1 W) _ echo '<option value="">Default</option>';. v% q% l; V+ D+ L; U& B
post_templates_dropdown(); //get the options+ `) e+ Q7 m8 j, |) B
echo '</select><br /><br />';
. r3 P9 K8 F4 r9 g* d 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 />';
4 e0 K: M+ ~- l# U3 w4 p1 O( L}
. R+ G" w9 ^9 R# B' j. w+ d. W5 w$ P$ w' a# k- i* r8 M Z
// When the post is saved, saves our custom data
% \( k0 N' U5 P: k5 Wadd_action('save_post', 'pt_save_postdata', 1, 2); // save the custom fields
" m/ A. Y! N& \5 a3 h7 bfunction pt_save_postdata($post_id, $post) {
! o* t, o" ]9 k$ e
: U: C2 k/ g' j; h5 [- s // verify this came from the our screen and with proper authorization,
/ i( e8 p9 L1 w // because save_post can be triggered at other times
( E7 c- k4 l/ k9 q. e) a6 x9 P- s if ( !wp_verify_nonce( $_POST['pt_noncename'], plugin_basename(__FILE__) )) {" \/ b6 H5 } l" E+ c r
return $post->ID;# G2 x# q- J G2 N- X
}
1 k# c- \ p) @2 D1 y- L0 ^
% ^* P2 {. P3 {( N+ V // Is the user allowed to edit the post or page?
3 Q, w/ g- K# y" { if ( 'page' == $_POST['post_type'] ) {9 s; V. A4 p. Q+ q2 w' }' j2 ]% E
if ( !current_user_can( 'edit_page', $post->ID ))) Z5 Q9 p, T5 u# E
return $post->ID;( d6 V, V! w) K# n( R8 O( S
} else {; V' M+ p, I4 T. x! F/ s B& L- i1 X( O
if ( !current_user_can( 'edit_post', $post->ID ))
3 [# u. J- K& B! ` p# C3 h0 ^ l# r return $post->ID;6 C7 G/ t- o. ^5 P
}
+ M- C' @6 \: O- S* T) }' n! n/ @6 @; e" g1 F" c
// OK, we're authenticated: we need to find and save the data! _" `/ e+ C, q
$ V- K7 m3 r1 n% c% S // We'll put the data into an array to make it easier to loop though and save
2 D M9 [( J6 y& H4 u $mydata['_wp_post_template'] = $_POST['_wp_post_template'];
' u; H; P' n" a( p1 q( h4 v // Add values of $mydata as custom fields
3 `& n* p1 {8 G% L j foreach ($mydata as $key => $value) { //Let's cycle through the $mydata array! M; k+ f2 A `) q3 p9 L5 p
if( $post->post_type == 'revision' ) return; //don't store custom data twice
+ N; U6 {& E( |) o7 S5 \7 E8 e1 b: ? $value = implode(',', (array)$value); //if $value is an array, make it a CSV (unlikely)) F/ b3 `+ M, z) K/ Z7 ]
if(get_post_meta($post->ID, $key, FALSE)) { //if the custom field already has a value...2 P, l e0 _: b
update_post_meta($post->ID, $key, $value); //...then just update the data# q+ j/ W1 x) f( M3 y4 D
} else { //if the custom field doesn't have a value...
8 P/ w) d3 g b; J add_post_meta($post->ID, $key, $value);//...then add the data, q" j2 [* Y. I$ P2 P7 |: s" C
}! G) W' F: \ s4 s$ G9 [4 b3 w8 G5 t
if(!$value) delete_post_meta($post->ID, $key); //and delete if blank7 o* z3 n$ W( v) i
}
/ ^- u3 X9 n9 L, _9 E2 J" e}
- Y8 D" m J" v" t?>+ ~' G6 h9 p# u1 w# [3 @( |
3 y4 f3 C, P) c( t( i1 C. E' G- V/ `4 J. t4 I
|