luguokankan 發表於 2013-6-10 13:51 / J" ~- Y3 f- o h, |' }. P5 B
post裡面要設置featured image
/ \/ j4 ]! P/ ?! p% G/ k9 P在這裡面設置嗎?
. y/ ~9 M! I7 F* i
) F+ C* `0 M0 o+ A. l4 i9 X. w7 x2 {; R7 i# v. K d+ |
<?php, j# F5 H1 e% Y& J) \% H- S3 i
/*
) n0 e/ u; F' [8 Q& J! s0 v2 C4 ]Plugin Name: Single Post Template4 T- i2 R' A: g
Plugin URI: http://www.nathanrice.net/plugins1 u7 I; j8 g6 `5 v! r ]( u2 S2 r) l
Description: This plugin allows theme authors to include single post templates, much like a theme author can use page template files.5 F5 _1 K. g7 C5 T0 P" U) |- ?% B
Version: 1.32 g' I" Z9 Y8 \" \0 P% {8 Q' ?1 q C
Author: Nathan Rice$ U3 n# P8 E( P9 j9 ^- W0 y
Author URI: http://www.nathanrice.net/
3 m# d: R! w9 y5 a# }
9 ^+ o( b# d: V+ qThis plugin inherits the GPL license from it's parent system, WordPress.
: `9 [2 l' b5 f/ A* X*/
4 F7 v6 X4 ^! d* y4 ?! C& y2 N9 `' l% ^( T1 E# n5 b
// This function scans the template files of the active theme, + s/ P# Y0 _2 t* F2 n. _
// and returns an array of [Template Name => {file}.php]
9 P' H% G6 K: n. s: A5 _- D0 jif(!function_exists('get_post_templates')) {. i% n% U" a& }5 f& T! n. E7 O
function get_post_templates() {
+ S5 ^, ?, f7 O5 @ g" k $themes = get_themes();
! R7 { ?3 v! D ~, g $theme = get_current_theme();
4 J. ~, @& G0 M! ] $templates = $themes[$theme]['Template Files'];
" i& u( h. \9 O- o* W" j $post_templates = array();
/ N( B% n9 I" G8 N# K8 g; q) W# ]! R* e3 F" S# T
$base = array(trailingslashit(get_template_directory()), trailingslashit(get_stylesheet_directory()));
2 F, i) p# d' G) I
. y; ?. u* d [ `5 W" A foreach ((array)$templates as $template) {
, A/ ?/ J0 c. o! a4 y $template = WP_CONTENT_DIR . str_replace(WP_CONTENT_DIR, '', $template);
: T: i4 [1 w* {% O $basename = str_replace($base, '', $template);
. F9 {+ {' L5 R0 f8 P7 A) F2 C+ V$ w! D, R+ g
// don't allow template files in subdirectories3 K# |! B5 c7 c. _2 r, r
if (false !== strpos($basename, '/'))
7 n, h0 h: ?6 e continue;
) Z6 h2 u" H F9 K+ E9 R/ u9 W3 }) ?5 P( ?3 c9 V+ F: e
$template_data = implode('', file( $template ));# _% s( ~3 r( k# l \
/ L1 }7 H F! L $name = '';) u8 o! P1 r7 `
if (preg_match( '|Single Post Template.*)$|mi', $template_data, $name))
. I# ?4 C. C7 P $name = _cleanup_header_comment($name[1]);; n3 Q0 u/ e; ] p+ n
4 X8 N* U2 Z$ P* E# @ K if (!empty($name)) {
* k h+ ~6 J7 z, D4 X if(basename($template) != basename(__FILE__))7 e5 T) |+ m% n9 t- c
$post_templates[trim($name)] = $basename;' y( m, Y5 y1 ? a- t
}; {, L& R8 a* i- m& |; y
}
; K& }3 n d( T+ F) c; [6 Y, y% e; V7 z, V' d I
return $post_templates;
! z8 H- J! n8 ]3 b% H- k
& A$ m( ?' e( Y4 T4 u0 q& f}}
* h* ^7 H6 k( m+ ~+ d O d& W# J1 E k6 e4 O
// build the dropdown items
( L/ m- X. B1 P8 w+ V, X; sif(!function_exists('post_templates_dropdown')) {
" E* n% n- ?: a! n( {2 efunction post_templates_dropdown() {
! R) N3 Y, N! k* a, W7 v3 K2 k global $post;; n4 u D. x6 O" e# H. H: @
$post_templates = get_post_templates();( i5 ^% [. p: T* n
' p: t; R+ [9 \; h foreach ($post_templates as $template_name => $template_file) { //loop through templates, make them options
( X0 L \3 e2 n& C) w if ($template_file == get_post_meta($post->ID, '_wp_post_template', true)) { $selected = ' selected="selected"'; } else { $selected = ''; }
( l/ l2 ~9 j2 E8 d u $opt = '<option value="' . $template_file . '"' . $selected . '>' . $template_name . '</option>';5 G2 p* B' n5 `$ a6 ~+ l7 I6 p2 a
echo $opt;9 I* f4 e0 A3 B! f- m" n! X7 T
}
2 t. ?1 {5 l* D3 _6 A}}3 a' X' `% a0 h% u6 r
1 m1 j) K; R0 o9 z1 s
// Filter the single template value, and replace it with9 R2 ?1 V" T& |3 r4 Z
// the template chosen by the user, if they chose one.
5 N" ^/ g1 K7 `4 W$ [6 Eadd_filter('single_template', 'get_post_template');6 v: [. Z1 M8 [2 j
if(!function_exists('get_post_template')) {
, m' O. u; {6 J. tfunction get_post_template($template) {
, y0 d; x$ y& D% Z' z$ C global $post;
" I2 p- L# m+ H5 t $custom_field = get_post_meta($post->ID, '_wp_post_template', true);
# S% D, H8 N3 F4 v9 K1 @2 P5 Y if(!empty($custom_field) && file_exists(TEMPLATEPATH . "/{$custom_field}")) { 3 S7 A2 K3 g8 y* _0 O2 N# Y2 X! N
$template = TEMPLATEPATH . "/{$custom_field}"; }; p s* `7 P# b) u" [4 J
return $template;6 D4 [ V' a' H' |; o) V* F$ q) [. v
}}3 C% L6 t0 E8 j6 R1 a+ x& |
( a( i- T' ^7 i2 x+ p
// Everything below this is for adding the extra box. z( T0 z. l1 v- P; V- w0 M) M
// to the post edit screen so the user can choose a template& T* t6 I5 }6 l& g' h! {) K9 ^
6 s7 [ h7 J4 d3 z# J// Adds a custom section to the Post edit screen) f- g6 g& V* {. ~ P0 a+ A5 B
add_action('admin_menu', 'pt_add_custom_box');
- l' i$ C' ]8 x8 |- Ifunction pt_add_custom_box() {: L, c, u$ z) k+ Z+ Y) u, j( X
if(get_post_templates() && function_exists( 'add_meta_box' )) {. H: `1 M+ Q8 _: J. E% _, k
add_meta_box( 'pt_post_templates', __( 'Single Post Template', 'pt' ),
2 l( C0 M5 ]0 E# [8 z. f4 h. y( b% f 'pt_inner_custom_box', 'post', 'normal', 'high' ); //add the boxes under the post: O( J" M R% e8 J' \! b
}
% C$ f' Y+ d$ s* c' J! R4 V+ k}
D" j% a* |9 P4 i' ]& f0 `# d3 Y2 g- x L: J
// Prints the inner fields for the custom post/page section$ {6 W; N. v, Y
function pt_inner_custom_box() {5 S3 ]% g2 b! Q% a: N( j
global $post;. C. g q1 O' I0 v# z0 C6 H
// Use nonce for verification: g7 S. y7 q2 q' h4 p; u& m
echo '<input type="hidden" name="pt_noncename" id="pt_noncename" value="' . wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
' q# A y- s. a# ?# ?, E // The actual fields for data entry; O) v/ n7 O( Q ?$ Q
echo '<label class="hidden" for="post_template">' . __("ost Template", 'pt' ) . '</label><br />';
8 }" r4 ?' n$ o2 L: a echo '<select name="_wp_post_template" id="post_template" class="dropdown">';
5 g3 {- B% T7 V. x echo '<option value="">Default</option>';
9 j" [4 ^, S1 ^( A post_templates_dropdown(); //get the options
8 a8 c' L' J3 Z# z' U) G+ z echo '</select><br /><br />';
7 G" m1 @. c) Z& X7 w" 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 />';
9 G$ b: Q9 v; w/ P6 O* }: |, t}
, Y# |( T: l5 l; H( m0 ~* R
5 |( S6 ~% C/ i! n+ n9 p// When the post is saved, saves our custom data4 C8 k1 v; S( n% v$ I
add_action('save_post', 'pt_save_postdata', 1, 2); // save the custom fields0 O, ~; ~2 y4 \6 j) u' A7 i0 P
function pt_save_postdata($post_id, $post) {9 N) s* Q8 U1 M2 R4 D
2 Y/ {- z6 j7 u5 B
// verify this came from the our screen and with proper authorization,4 d* i: U) p; f9 t- |2 a8 m
// because save_post can be triggered at other times/ d4 }, R2 Q7 C) h1 O: I }1 S
if ( !wp_verify_nonce( $_POST['pt_noncename'], plugin_basename(__FILE__) )) {
( S+ {! M: l5 r& u# g1 J9 @4 w return $post->ID;
* {* a7 ^! S, @$ ^ }/ i( p' V. A0 N- f9 y
[3 d0 W1 b8 E; J5 ~9 J! q // Is the user allowed to edit the post or page?
0 O5 I {$ ~ F if ( 'page' == $_POST['post_type'] ) {
1 Q) L3 C# p- c. P- H- e; V if ( !current_user_can( 'edit_page', $post->ID )). n. Z9 @' Y) B+ }! G
return $post->ID;
6 h& G9 p; k8 \+ B } else {5 l. j& a6 f% g0 P* y, j9 E
if ( !current_user_can( 'edit_post', $post->ID ))
/ c9 p+ R' h: V6 L% q return $post->ID;0 I3 j7 |3 n$ F7 G& ?3 L( X/ }6 Z2 J
}' Y$ R( X7 _8 M: @0 d
; I9 w2 c1 }4 F$ ]: }9 W$ P
// OK, we're authenticated: we need to find and save the data
" e. c# a' T3 e: y( a3 i
$ {0 G$ T N0 c: B- G // We'll put the data into an array to make it easier to loop though and save
9 n9 S2 o# u; h( H G $mydata['_wp_post_template'] = $_POST['_wp_post_template'];# _& z2 j/ r. P
// Add values of $mydata as custom fields
0 f9 v# _. Q: A& X# e( m+ Y, p& N foreach ($mydata as $key => $value) { //Let's cycle through the $mydata array!$ H1 }0 e& \+ D/ E3 j
if( $post->post_type == 'revision' ) return; //don't store custom data twice
. ^( R3 p* [; b8 Y, T2 M $value = implode(',', (array)$value); //if $value is an array, make it a CSV (unlikely)
/ T+ M" ^0 m: D2 N# E0 C if(get_post_meta($post->ID, $key, FALSE)) { //if the custom field already has a value...# S0 Y$ n+ Z5 i$ |! ?
update_post_meta($post->ID, $key, $value); //...then just update the data
' f# V0 X( r2 |6 h% l6 G: _ } else { //if the custom field doesn't have a value.... C( c3 C& v( j: q
add_post_meta($post->ID, $key, $value);//...then add the data6 E0 d2 T5 m$ ^
}5 B; J& F& ]" }" l! D- p
if(!$value) delete_post_meta($post->ID, $key); //and delete if blank
9 w1 j* T, S2 P+ V$ r }
$ i# j: T! a; G0 ~' e}! P) S4 L8 b) K5 P& K9 }3 [+ X& T
?>0 p' l+ d9 K$ `! Y. q$ [" s
8 k0 b( c0 C. I# ~5 ~* ?# }. d& i+ Y: O9 v# F1 q' \3 m, m
|