luguokankan 發表於 2013-6-10 13:51 
' t* ~, N* `" X# C6 U8 r- Epost裡面要設置featured image 6 q g, e% [$ X' D- b# @
在這裡面設置嗎?
/ r. i0 A9 d4 D# _- p" d# d6 s/ U. a1 w
. M7 M" G6 @( X8 d# y/ O<?php- _9 h* X5 v/ N) h* Z* ?' M9 x; c
/*" T- ?; Q: ]' N2 {. x5 o
Plugin Name: Single Post Template$ ^' _* g1 z9 P2 u, [
Plugin URI: http://www.nathanrice.net/plugins7 x3 O* z( h( u
Description: This plugin allows theme authors to include single post templates, much like a theme author can use page template files.7 l! n3 R$ c5 ~0 Y) }: x
Version: 1.3
" z$ G2 d9 x# j, i4 q( X7 aAuthor: Nathan Rice, F( I: c7 Q: ?- m- ^
Author URI: http://www.nathanrice.net/
* k; T+ i, u/ q2 w( I" Z% i! A% _& U8 |+ t8 s- L
This plugin inherits the GPL license from it's parent system, WordPress.3 z2 }. n9 _) D2 \* ~8 Z
*/, H$ ~ b% e, t$ P
" q) h- j8 ^" K6 _! ]; f2 F$ U
// This function scans the template files of the active theme,
! W& @& j- v! U8 i$ q// and returns an array of [Template Name => {file}.php]- s) k7 X9 L" x9 v
if(!function_exists('get_post_templates')) {2 t' h' A+ n3 \; `
function get_post_templates() {
2 Q, L; J) w+ y$ J5 g: Z $themes = get_themes();
! g v0 [2 f& ^! L $theme = get_current_theme();
/ p9 T6 E+ a& K8 K% F $templates = $themes[$theme]['Template Files'];+ y% V7 Z4 g0 H' c5 ] b
$post_templates = array();2 @3 g( g L7 Y% G! U
" b7 |3 i* W5 U; I# h$ [
$base = array(trailingslashit(get_template_directory()), trailingslashit(get_stylesheet_directory()));# k$ U8 _% x( @$ w6 Z9 U2 {
( f: x( V' ~, r& H foreach ((array)$templates as $template) {
- ]3 o! E$ C+ ~6 ? $template = WP_CONTENT_DIR . str_replace(WP_CONTENT_DIR, '', $template);
+ T' \, a+ ^* `' E: @ $basename = str_replace($base, '', $template);
, h, V: F: G* g9 t; {1 h% l8 P V
// don't allow template files in subdirectories
4 c; e$ p4 z' p H7 \* l if (false !== strpos($basename, '/'))5 }: n' W0 g) p# ~( M9 g
continue;% m* \, [/ b" J0 |5 L7 M% [) f9 F* K
, E& O7 B% Z# M, R+ g9 | $template_data = implode('', file( $template ));2 v( V: x/ H: g9 q/ c2 ?, h
- z3 e! ^2 }" l X
$name = '';
( ]" A1 \) R& y/ t3 z if (preg_match( '|Single Post Template .*)$|mi', $template_data, $name))
7 S/ d8 _! t( ~/ g $name = _cleanup_header_comment($name[1]);2 b; L( |' c l5 B' @1 i+ e
7 L0 } H' y0 d
if (!empty($name)) {* E' [; {! |4 f
if(basename($template) != basename(__FILE__))
* J- B2 y% d- [6 b* z* u+ g7 P $post_templates[trim($name)] = $basename;
9 S' H9 Y' {- O6 @5 X- \/ ~6 Z }
2 s7 d* z9 L6 \. d2 | }. E- K9 k$ y) x6 Y' a
2 c: H9 Q4 F) r; }' J$ l return $post_templates;! f$ f* N& U7 F& Z% }
3 h6 G: I7 i) j
}}
! k5 r; M) e! x) O7 _# {0 e7 D$ f
# H$ {1 y& \* ~' x, n% y! z// build the dropdown items* [: ^/ I- f8 t; S% u2 u
if(!function_exists('post_templates_dropdown')) {
7 c) m- z8 J" K4 f9 yfunction post_templates_dropdown() {
2 r5 `1 q0 g0 G9 {) B- Z$ V global $post;2 u5 p( O0 I s; \8 W) V! |1 J
$post_templates = get_post_templates();
1 { [8 }- F2 I0 ? 9 F* G# Q, @ |. S/ k. | k
foreach ($post_templates as $template_name => $template_file) { //loop through templates, make them options
! j. B" d2 Q- s% U1 Z; U if ($template_file == get_post_meta($post->ID, '_wp_post_template', true)) { $selected = ' selected="selected"'; } else { $selected = ''; }. _- U, t7 J% s; o! b7 t. i
$opt = '<option value="' . $template_file . '"' . $selected . '>' . $template_name . '</option>';1 O7 c7 E# ?+ d9 d' P
echo $opt;1 q5 y$ T! t& S; b# |
}5 Y( v7 y: v/ e' I* e
}}
0 D; d; y. L( w( M
2 D& f- \( ?% ?2 N) L// Filter the single template value, and replace it with
" A( Q3 c; u$ w0 t- l/ }+ W// the template chosen by the user, if they chose one.5 q# j4 x/ D# }( n
add_filter('single_template', 'get_post_template');
9 W4 B- V# q2 ~+ fif(!function_exists('get_post_template')) {
) q7 n w8 s* v& g) c {4 rfunction get_post_template($template) {
* |* K8 f! ^/ u global $post;$ s8 c6 n$ }/ g, P
$custom_field = get_post_meta($post->ID, '_wp_post_template', true);6 _# k9 y" K: _* r' k% e2 _2 }' D
if(!empty($custom_field) && file_exists(TEMPLATEPATH . "/{$custom_field}")) { 6 ]2 A. h" c8 s, F! @
$template = TEMPLATEPATH . "/{$custom_field}"; }4 ~$ W4 }$ t$ N7 K6 z% |5 S( W' R
return $template;# t8 J$ h2 ~% \
}}
( K* D7 |- S0 B0 W4 m0 R- Y ~0 n, o& G2 i* @/ Y1 V
// Everything below this is for adding the extra box' U. A# k4 Z1 \0 @/ g" _
// to the post edit screen so the user can choose a template# i& Q, x+ k7 p9 }' R& h, I
; g x r4 w, p1 ]1 c3 t( k) G// Adds a custom section to the Post edit screen
8 b" B0 H ~& p4 D% F: \add_action('admin_menu', 'pt_add_custom_box');* o! i5 p! D; ]: t9 y* e* `( ]
function pt_add_custom_box() {
z- u, B. O( V* E if(get_post_templates() && function_exists( 'add_meta_box' )) {- @ Q+ C: P1 R. Y1 X
add_meta_box( 'pt_post_templates', __( 'Single Post Template', 'pt' ),
3 m3 {5 Z. [0 N8 D7 B" k+ L3 L5 o 'pt_inner_custom_box', 'post', 'normal', 'high' ); //add the boxes under the post* ~( Q$ L- F9 c
}
, t g: Z7 ~' l! M* x7 y3 Y}
( H& C" T4 x( C+ z& J% {! c& o7 S! t, Q
// Prints the inner fields for the custom post/page section
* k" s) V& ?& v# d2 @; |function pt_inner_custom_box() {
2 @2 ^6 |( [4 Q2 J, Q$ u$ H" S global $post;/ } n6 W; A: H' p8 O
// Use nonce for verification9 n- s2 p1 ?- r ] D! y f
echo '<input type="hidden" name="pt_noncename" id="pt_noncename" value="' . wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
1 [4 G) L1 Q2 P // The actual fields for data entry
$ T- T. X' ^$ g; u$ u8 T echo '<label class="hidden" for="post_template">' . __(" ost Template", 'pt' ) . '</label><br />';' f( P/ k ~. m: e0 Q
echo '<select name="_wp_post_template" id="post_template" class="dropdown">';: A4 z8 i# O; d3 ^
echo '<option value="">Default</option>';( N: h+ S$ A/ _$ Z1 v
post_templates_dropdown(); //get the options' R7 x& k A, `6 h: ]( }9 _
echo '</select><br /><br />';
1 U- R! W# C. T/ `, w+ ~# g 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% k5 w1 M1 `2 s1 _}2 w2 p0 Y3 P9 z
0 o6 s) O3 |3 _: e
// When the post is saved, saves our custom data9 e# P( n4 B( X; {7 o0 d) m
add_action('save_post', 'pt_save_postdata', 1, 2); // save the custom fields* t; S2 }+ e @' `
function pt_save_postdata($post_id, $post) {
/ \! [ h2 Y# j. V+ W. \
& e. S5 Q7 F9 d2 P // verify this came from the our screen and with proper authorization,
% Y& ?2 b! [: B: W U) \ // because save_post can be triggered at other times
5 H0 z% @! v3 U! {& ^ if ( !wp_verify_nonce( $_POST['pt_noncename'], plugin_basename(__FILE__) )) {
5 k9 j1 N' j$ B+ s return $post->ID;, I& Y9 c" M' I1 L4 U
}
* f T# S( p1 Y5 ]- [
% b/ `3 b3 ?# j# Z/ k( c5 n S' s# I // Is the user allowed to edit the post or page?
$ C) _) o- m& ^ if ( 'page' == $_POST['post_type'] ) {
O4 b/ M+ a2 |$ ^ if ( !current_user_can( 'edit_page', $post->ID ))
Y. F4 B4 g7 u' \' }3 P; a return $post->ID;* q! ?- e0 u& w B' C3 Q( C1 r
} else {3 c4 z. z7 ?6 T6 o3 X) q
if ( !current_user_can( 'edit_post', $post->ID ))7 ^3 b0 r% u2 c$ q7 [
return $post->ID;
3 }. L( U& d4 E2 v }& q ^* ~! u9 E6 ^, O( e1 T
- p2 m1 E9 ^+ l // OK, we're authenticated: we need to find and save the data
1 H! {& H: H0 w4 F0 H V5 Z2 R5 ~4 O( A2 Y
// We'll put the data into an array to make it easier to loop though and save8 q4 M b: u8 |
$mydata['_wp_post_template'] = $_POST['_wp_post_template'];
. `3 j1 }; f+ O; B' v, m, Y // Add values of $mydata as custom fields+ p% s: Z# l! H8 `6 u. ^
foreach ($mydata as $key => $value) { //Let's cycle through the $mydata array!. B* e: a f" E: @2 O
if( $post->post_type == 'revision' ) return; //don't store custom data twice
0 F, i# p* Z9 l2 h ^( [ $value = implode(',', (array)$value); //if $value is an array, make it a CSV (unlikely)3 w' T6 ]/ X& C# s) ^0 r
if(get_post_meta($post->ID, $key, FALSE)) { //if the custom field already has a value...
( j4 Q8 s+ |& {, D update_post_meta($post->ID, $key, $value); //...then just update the data
, g3 O1 C V* C7 W# C3 H% h R0 D% a } else { //if the custom field doesn't have a value...
8 n; h; v, U4 D add_post_meta($post->ID, $key, $value);//...then add the data
! S- X3 c5 \4 f2 q, |" @ S }* s3 t) P1 @; z8 V8 }/ z: g
if(!$value) delete_post_meta($post->ID, $key); //and delete if blank
6 G) w0 T, o/ o, A } C2 N/ d2 c' I
}5 x }5 N) ~! w8 W% `
?>( n" m8 N) A3 r* D5 L0 V ~. T
: |( }6 q6 g& C
1 X8 T2 n2 p* ^- d# e0 U: \ |