luguokankan 發表於 2013-6-10 13:51 ( J: q, i& O" c8 G9 X
post裡面要設置featured image
4 P7 N: n V4 Y) s! J; f. P7 c在這裡面設置嗎?
8 X0 T4 O4 q9 D4 O, c, G3 M
( S+ t5 B) l3 v0 [! g) Y
. e6 s3 U" s1 v; n* M<?php
) X* V2 w% l1 E9 m- W2 m/*2 F7 r* u# ~7 [
Plugin Name: Single Post Template
3 z# `1 X. A$ a* w/ uPlugin URI: http://www.nathanrice.net/plugins, B; t: L$ {$ f( B; ^' X! s& u
Description: This plugin allows theme authors to include single post templates, much like a theme author can use page template files.
5 _( K% `6 u8 H5 MVersion: 1.36 G9 R4 h" {3 V$ ~8 Z
Author: Nathan Rice
( U, E) [% Z* F7 B. LAuthor URI: http://www.nathanrice.net/
# K" i) [! r& r$ [0 i5 K8 y ~) Q5 g6 e8 E9 t. ^. Y' X% ?
This plugin inherits the GPL license from it's parent system, WordPress.. n% B4 C9 b3 w& \/ Y6 }
*/) m# P) v/ u/ {1 w$ ]% l
) R. m" `2 e. p& q4 k: N5 Q// This function scans the template files of the active theme, , @$ U/ R/ S* W6 I/ o" E
// and returns an array of [Template Name => {file}.php]. O0 @" @4 d( |! b e2 A3 N
if(!function_exists('get_post_templates')) {( V( x7 _: {$ V$ }! W! U
function get_post_templates() {
& t& M7 D. p4 l( y" n $themes = get_themes();4 W7 A9 h* G7 \& F: G2 a
$theme = get_current_theme();* l1 }4 a. h( z$ U8 A' F
$templates = $themes[$theme]['Template Files'];
" G s3 f5 Q- j $post_templates = array();, g5 K/ N; |) L
- {6 w) {5 H% [- D7 @1 q $base = array(trailingslashit(get_template_directory()), trailingslashit(get_stylesheet_directory()));
6 r3 H: k: B( @3 Q" U) L1 R& l9 t. C& X: A# ~* ]0 j9 `
foreach ((array)$templates as $template) {1 h& N1 ~0 `4 m- w* k
$template = WP_CONTENT_DIR . str_replace(WP_CONTENT_DIR, '', $template);
7 ~$ N0 I c3 l c/ M4 t- T $basename = str_replace($base, '', $template);. D$ V/ S! q! B7 a T& n5 Z$ O
. u9 f% {+ o( s* J0 { // don't allow template files in subdirectories4 W. |: S3 f9 A5 D1 L
if (false !== strpos($basename, '/'))# f0 f- `/ e: n
continue;
) E/ }( U A, I/ F9 m
' ~* b: y% l7 V& d! L3 y3 c5 K. H $template_data = implode('', file( $template ));: N t9 V; k/ [7 q3 g+ n+ Q/ S
f" Q0 B2 h9 N/ _& R$ v) I- d $name = '';" x$ e, L. G" l
if (preg_match( '|Single Post Template .*)$|mi', $template_data, $name))3 M5 z; Y3 l$ } S1 G
$name = _cleanup_header_comment($name[1]);
( I3 a3 h0 V G, P( g$ Y* O! R9 h2 M
% K4 ?+ X& V) x' L: N: w) I if (!empty($name)) {3 p6 L. y/ F2 M. b; v+ l$ Z5 M
if(basename($template) != basename(__FILE__)). W, k, L8 z! O, y( C' V
$post_templates[trim($name)] = $basename;- |0 n; @/ d& y$ Y
}. t1 e# [0 F1 }% x+ k
}% e7 k# O" P8 t" }
" w% l- e- l9 M( N
return $post_templates;
$ e; B4 I8 S1 x4 B
5 V" i% R$ a% s" `, x}}4 |* [: @$ {( ?3 _) o
_/ w9 b* g$ F
// build the dropdown items' i& K* x; p: G. |
if(!function_exists('post_templates_dropdown')) {6 {$ v' m8 G+ x* u; i
function post_templates_dropdown() {
! y! O, v: m3 [7 f global $post;. y# V; P% P6 F' i& e
$post_templates = get_post_templates();, m& v. @( U% K- Z
. \" [9 `1 D8 D) K5 m# D" V3 L foreach ($post_templates as $template_name => $template_file) { //loop through templates, make them options
2 }7 z1 m9 v" }7 Q if ($template_file == get_post_meta($post->ID, '_wp_post_template', true)) { $selected = ' selected="selected"'; } else { $selected = ''; }/ _$ q( Y3 c4 K; U; m$ X
$opt = '<option value="' . $template_file . '"' . $selected . '>' . $template_name . '</option>';
|5 _5 b3 H+ m echo $opt;
7 e5 r6 |2 ]( t } t a4 l$ K+ C
}}+ b D. U0 V6 J" K
& Y; I: I6 I6 M
// Filter the single template value, and replace it with, g: W7 E+ f- `. @
// the template chosen by the user, if they chose one.- z! X/ H* }8 ^( u
add_filter('single_template', 'get_post_template');
. J. \( U' O- k# N$ v- h8 Yif(!function_exists('get_post_template')) {2 Z1 f5 i* ~7 D) F0 r
function get_post_template($template) {! Y$ f+ O2 x7 @
global $post;
8 G& r H( n# W x4 v$ p s& B $custom_field = get_post_meta($post->ID, '_wp_post_template', true);
: H4 _% x! L; h if(!empty($custom_field) && file_exists(TEMPLATEPATH . "/{$custom_field}")) {
4 e, k7 P i6 i2 T$ F( ~/ s: Z+ r $template = TEMPLATEPATH . "/{$custom_field}"; }6 s9 V$ J7 E! e( O4 K
return $template;
5 n+ i" d. P I Y0 I}}
, ~/ e( H0 P/ `2 Y- M& i
]+ \2 @; S Z: _' I// Everything below this is for adding the extra box
. P4 |" ^8 L8 ]; m// to the post edit screen so the user can choose a template. \! G& w* Q7 W7 y7 h2 G
G o# w) ?9 E/ {0 f* C( `
// Adds a custom section to the Post edit screen* r& I' n+ m0 f- a3 \- N
add_action('admin_menu', 'pt_add_custom_box');1 t7 o% N8 }& H( b
function pt_add_custom_box() {3 @. ]8 E" W- Q" c5 c+ y
if(get_post_templates() && function_exists( 'add_meta_box' )) {
, [! N, O" q& Q% a( R2 X add_meta_box( 'pt_post_templates', __( 'Single Post Template', 'pt' ),
4 Z6 T% \5 ^5 H c$ a$ ~ 'pt_inner_custom_box', 'post', 'normal', 'high' ); //add the boxes under the post
6 j" l: v( v: V \" e }
; K$ b% a3 _: A( Y1 R F- I}, s+ W- k# u% ?: {% ~$ }$ Z
; H! y4 x J) T: D& k$ r
// Prints the inner fields for the custom post/page section
6 z# Q0 B. p/ A9 |5 gfunction pt_inner_custom_box() {& V+ t q8 F( w! _. n
global $post;
1 K7 O$ B3 s1 h3 j // Use nonce for verification
% [4 P. `4 p/ w8 g+ N echo '<input type="hidden" name="pt_noncename" id="pt_noncename" value="' . wp_create_nonce( plugin_basename(__FILE__) ) . '" />';3 n& F# A) ^# S: B; B
// The actual fields for data entry7 r1 z; e! ?" Z" W
echo '<label class="hidden" for="post_template">' . __(" ost Template", 'pt' ) . '</label><br />';+ Y1 r8 O; s' x) s( t T* D: Y9 q
echo '<select name="_wp_post_template" id="post_template" class="dropdown">';- `! q9 z g2 L3 [9 y$ W9 }+ V
echo '<option value="">Default</option>';
/ t: w1 G: ?3 X2 T1 ? post_templates_dropdown(); //get the options
' _9 g. `& b' ]# A1 X echo '</select><br /><br />';
( s0 X" C! z ?, Q. }, }6 l5 J, H2 X 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 />';
/ T3 @' o6 _3 x% u, I( C5 x}
0 `! k& W1 }2 Z) R7 D; g
; a4 C+ F0 e6 N2 a! m// When the post is saved, saves our custom data! L4 Z4 X6 e& [/ l* V: ]- A
add_action('save_post', 'pt_save_postdata', 1, 2); // save the custom fields$ f$ G, H; ~/ g Y+ ?
function pt_save_postdata($post_id, $post) {. Z$ }: ^5 i: s8 C% Q' i
$ ? P* b: l, M8 s3 k' ]- n8 e
// verify this came from the our screen and with proper authorization,2 \6 L8 f" q- | U1 t I# @
// because save_post can be triggered at other times: p7 i: U% C& w0 W# A' V: b* q4 H
if ( !wp_verify_nonce( $_POST['pt_noncename'], plugin_basename(__FILE__) )) { N. s* w) N0 d7 m/ t4 u2 S
return $post->ID;
% I. {; w3 ~. j s- v }3 W% L& W9 w# H9 h8 ~
2 |/ i+ w6 X4 I
// Is the user allowed to edit the post or page?. m* A" s; Z, Z" ?1 I6 R
if ( 'page' == $_POST['post_type'] ) {
- Q. w" E# B. Q% I& g- h' A if ( !current_user_can( 'edit_page', $post->ID ))3 j, Q7 U" T) v1 e
return $post->ID;7 k* u% J+ [+ N. r4 E9 l
} else {
7 I+ }1 t: q8 X( M6 d if ( !current_user_can( 'edit_post', $post->ID ))
+ b5 v4 R y" U9 p& {9 D return $post->ID;# W$ P5 j! n% a0 o& `' `
}$ U' |3 ]3 I/ R9 s
# B0 C0 @' E" s- u% ]
// OK, we're authenticated: we need to find and save the data- w+ {( O8 {- ~% \: W- c/ u4 A
3 _6 Y" T5 i; F' W // We'll put the data into an array to make it easier to loop though and save
4 S0 u; ]- ~+ f: f1 J6 D $mydata['_wp_post_template'] = $_POST['_wp_post_template'];0 \8 c8 n* O7 N, ~/ D5 {6 U8 S& C( t
// Add values of $mydata as custom fields
T/ u, ? Q: Y8 H6 _ foreach ($mydata as $key => $value) { //Let's cycle through the $mydata array!- ^* Y: E0 p0 X0 Q. j$ b3 |
if( $post->post_type == 'revision' ) return; //don't store custom data twice
5 L& u. w/ r- S $value = implode(',', (array)$value); //if $value is an array, make it a CSV (unlikely)
0 j+ ]8 M5 w+ y if(get_post_meta($post->ID, $key, FALSE)) { //if the custom field already has a value...
8 |8 P0 W# t6 b update_post_meta($post->ID, $key, $value); //...then just update the data6 `8 d" S: @: Z, H( ?' t
} else { //if the custom field doesn't have a value...
_' e1 M4 O' j4 {3 k add_post_meta($post->ID, $key, $value);//...then add the data
4 C4 l9 |( ^9 F7 V- K' L% f }/ D4 G% |6 E! _
if(!$value) delete_post_meta($post->ID, $key); //and delete if blank
: Q$ ^5 _$ |7 n' l4 a6 O2 C }1 }! ~9 Q6 S% m' `$ R L
}
4 i& j9 ^2 s: P: g?>
6 ~* s0 V' P8 B3 u4 Y6 I6 _6 }; K" s
) {9 V; A* \' T9 N) a
?2 c, F( ~6 ~0 m& Z" \# m |