WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)
* f& y- w9 r6 e- q X
! r9 }! U: Y' c/ I9 W9 xwordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐
P/ G- P9 J; o! c5 b隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具/ |7 ?2 V7 w; t& L
0 Y& W0 _. O. c+ N凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :
3 h6 a/ J* l' b& b4 b! C6 E k( P甇文 , 臭誑閮剖銝訾批嗆蝡賊 :
) Q" W: z: n- S( i* F甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :
7 y \, G( n4 f) f- [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣
5 Z% R/ m% _/ z2 _1 \7 t d' w4 |+ ^3 C2 t* E
1. 蝪∪桃凋誨蝣$ e( g- n: O! G* d
祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :7 ~8 @$ c7 L' t U, z# n6 |
閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 : ?% T& [1 n2 ^1 P+ ]" E( j
- 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
- 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
- 閮餃賣賊蝯針ordpressaction銝2 g8 ^5 j% G V* ^' |& G
) u0 S# Z$ o: Y, B祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准
+ E0 X0 z. y' l% Z+ P1 W1.1 撱箇餈賣
8 \" ^9 @5 @1 Z嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡7 B, [7 S# D/ f% X' `
- function recent_posts_function() {
8 ]- m! D; n5 M" S+ T - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));3 k1 A! L' R# f
- if (have_posts()) :1 a/ W7 M0 `6 i3 I' a' u* K& W8 }
- while (have_posts()) : the_post();/ w M% T% K- T. P+ }4 p( Z
- $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';+ [) I& D# ?4 Q
- endwhile;- X% A! L% R5 T7 M( o& W
- endif;1 d* W) a+ v- ~) j% o/ i7 q
- wp_reset_query();
! J6 n- o7 t6 V: U; h$ i4 y - return $return_string;* k% P4 }& K( q: L
- }
銴鋆賭誨蝣 憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯
, h2 _5 M; A5 N" e) @" l. c1 i5 Y- }: d' ^3 ?: Z
5 L6 }4 ]3 L7 \! B) q2 Z
1.2 閮餃凋誨蝣7 E& t4 n; |8 T$ C
曉剁閮幃ordpress賣豢臭凋誨蝣潘- function register_shortcodes(){
& h; ~( w- H2 { - add_shortcode('recent-posts', 'recent_posts_function');
2 }. N' F/ `2 U - }
銴鋆賭誨蝣 嗅冽蝡批捆銝凋蝙函凋誨蝣3 \! j+ e8 \8 M
撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴) Q" [( W. _- K! D/ {4 t# n: n
. C" |- M: w# K+ z I
; m! V5 ^1 ~: m1.3 Hook into WordPress
7 d! r+ C- V6 H. c& U
0 h9 g* m+ ^/ c; z, T& m箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准* b3 ^8 Y. H) i( y
- add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣 : f/ h0 m& @; J0 E& c
1.4 皜祈岫凋誨蝣6 @( W. \9 q+ t. z3 f: o
蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :2. 脤凋誨蝣 2.1 凋誨蝣澆 凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻 閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼 撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 : - function recent_posts_function($atts){4 h3 {. T% ]& Q7 O
- extract(shortcode_atts(array(
+ c2 z1 @; y/ x+ k# ^0 q$ S( O - 'posts' => 1,
4 F0 y- Y4 f# c$ E0 X- A - ), $atts));
/ O0 `" @0 P I) [# T -
' K2 _/ r9 w& ^2 N. w! G+ _ - $return_string = '<ul>';! g6 J# B; x: }
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
5 `! ~( k- A- m* s - if (have_posts()) :8 P2 @, Z! k. n$ \ s$ E
- while (have_posts()) : the_post();
2 c% ?7 S/ c2 O6 a4 M$ Q) Q0 L - $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';% q+ {: F1 G* j! |
- endwhile;- i! G9 p3 D3 q* t1 j
- endif;, [9 \, n9 u5 l
- $return_string .= '</ul>';
9 O. y) Y- \: {- D% H5 T; f O' P - $ |7 @( ?% Y( l: Y
- wp_reset_query();+ p+ @+ J' i' _6 h8 S+ b2 }# N
- return $return_string;
7 `2 [0 \) i4 @' @* h - }
銴鋆賭誨蝣 憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :
7 h# d& b% P' p2.2 凋誨蝣潔葉憓摰
臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗 - function recent_posts_function($atts, $content = null) {
7 K# }6 [- t. s - extract(shortcode_atts(array(
; a* i8 T$ z4 x* v - 'posts' => 1,7 V( S( T0 P) A# t
- ), $atts));+ f+ M' Z* v: j3 E. [' {! F9 o
- 8 L7 X3 w5 Z& m2 d! S" ~) m
- $return_string = '<h3>'.$content.'</h3>';
% x% B5 y2 y: i8 S - $return_string .= '<ul>';4 c& f9 X# t5 _8 [% {
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));% L9 A/ i( g# `7 x4 r7 Z- h
- if (have_posts()) :
2 ]/ t4 J8 ~. m" R - while (have_posts()) : the_post();; |: i7 O; K. ? ^- n
- $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
1 p5 L! \% g1 b! Z' A4 V* G - endwhile;
* ]( G( |/ ~8 S - endif;
2 k/ j0 A G4 M/ Q8 Q5 O( E% c" O - $return_string .= '</ul>';$ y7 s5 d0 z* E3 {
- s% v5 ?8 R* E: @8 A6 X
- wp_reset_query();
; W8 Q' p& A, A$ t7 U; u4 H2 R/ b; ` - return $return_string;
+ A% r8 o, ~, s' w6 c+ K - }
銴鋆賭誨蝣 蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝- [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣 支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗/ l* @: X: q" R& ^
3. 其遙雿唳寥*蝷箇凋誨蝣 3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣 閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛嚗典臭誑撠甇斤撘蝣潭曉典撌亙 % F) U6 G" E: g- F
臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 : - add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣 曉其閬靽格嫣遙雿嗡唳 , 瑟, 湧甈撠甇蝣箏圈*蝷
! t: o1 @1 n( O8 A) i2 J2 a璅嚗刻隢銝剖函凋誨蝣潘 - add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣 冽閬銝剖函凋誨蝣潘- add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣 4. TinyMCE蝺刻摩典凋誨蝣潭6 M9 H$ L6 z. i6 q8 D }# b
4 k+ Y: n8 u: \/ g
嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗 - 箸迨撱箇銝 JS 瑼獢
- 閮餃JS隞
+ Q) q8 K0 R" ~" [ $ w; C" p( }! `% M0 u/ e4 Y
4.1 JS隞嗡撱箇JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗 - (function() {* E- i9 s( }2 _) b5 P+ T% F
- tinymce.create('tinymce.plugins.recentposts', {) ?! [: } H, B _
- init : function(ed, url) {
/ e$ h4 w& ~& |+ j8 {/ N4 a6 } - ed.addButton('recentposts', {2 j( x/ x7 i6 W& a$ t, M
- title : 'Recent posts',
% G0 {' H* q7 w! | - image : url+'/recentpostsbutton.png',
/ i+ r( p Z5 n5 D2 S - onclick : function() {
0 `* p5 T5 g1 g! n$ V& j - var posts = prompt("蝡賊", "1");0 D9 T" W; N. `6 F
- var text = prompt("隢憛怠亙銵冽憿", "唳蝡");
+ ~6 o% G- r# Q& u9 `0 e -
- W' ]* s& P+ X) B( P - if (text != null && text != ''){
2 U0 |. f+ ], g1 W9 C - if (posts != null && posts != '')8 s8 x m4 a! j* A# F& Y! [
- ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');# a: e( c# G- m/ I# J1 L% c: R
- else: Z- I8 }) D! y) ?* i
- ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');
" \0 I% ]4 w6 k Z. P6 k - }- H4 J9 o8 ^% g. f' L. Y9 j5 D+ [* m
- else{
2 f# F& _. K8 p" k5 N8 x# a O - if (posts != null && posts != '')
& Z7 j4 l2 ^8 s# s0 z - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');' r+ t. m/ p5 D! [' f
- else
* w5 }/ M) h4 x+ v - ed.execCommand('mceInsertContent', false, '[recent-posts]');( }( w( d/ q# t
- }
9 }9 o$ g$ u6 {+ [5 [9 ^9 A0 Y6 ?8 a - }
g! E% G/ M/ k. q- b+ x" k) D) ` - });4 [) i0 o* X7 ~
- },
; `& f" @2 @2 T+ g - createControl : function(n, cm) {1 N+ B7 X1 o) [' t: g
- return null;2 O" `7 ^5 p$ {. v# B% q9 W& z
- },. P. h1 ^6 N1 O/ P' t/ J
- getInfo : function() {) A6 I. e' v, |& Y- `- a5 @
- return {
6 F( B6 h: X1 w9 |/ { - longname : "Recent Posts",
9 a. }. f6 A6 N! G% C - author : 'Specs',, J& ^0 D; M2 {3 q8 u
- authorurl : 'http://9iphp.com',
: D' {9 P0 y. G' ^ W+ c: V2 G - infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',- q- x5 o, W7 U6 W- u; s
- version : "1.0"
7 m" W; b5 i i+ s+ S2 y5 } - };
3 a( e: L; S1 g5 t( y4 P* h - }
+ @- M6 Y9 S9 a4 c) L1 o! ]3 l$ V: } - });
* ^/ b3 C8 Z+ K* U$ H7 p$ { - tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);
/ G: B* R% r9 K7 O# q4 r- q: h - })();
銴鋆賭誨蝣 澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨 onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉 敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准
) `& N* [* J: i% ]2 @. G& F
4.2 閮餃TinyMCE隞
8 x8 X2 \- {4 E
/ T/ N; [( B N1 ~* |; c 嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉 蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩 - function register_button( $buttons ) {
k9 r% i) a. t& ^$ l8 Q - array_push( $buttons, "|", "recentposts" );
- t i0 [: N0 D, x - return $buttons;' o0 c$ Y0 f. w2 K$ X. b$ `
- }
銴鋆賭誨蝣 蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅
6 O/ ~+ f$ W; _+ p, Q8 c7 r% S- function add_plugin( $plugin_array ) {% D5 X! H6 V) o% n+ L+ ^+ a2 c
- $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';' d6 D2 B4 Y M# P% \! g1 ?) f
- return $plugin_array;
+ k% D# o1 Z' h4 n, A - }
銴鋆賭誨蝣 銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁& N# S1 o; J' n2 v- x) v8 }
- function my_recent_posts_button() { ) f$ G }) r9 n1 o- h& ?! R
- if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {
. k! O+ _9 _, F" i" z" b; t; V - return;( [& K6 Q: v, C: q0 p# A: N* Q0 w1 S
- }
1 x) R* j; {: m6 h$ A -
1 U1 Q: ~) I- S4 C1 P - if ( get_user_option('rich_editing') == 'true' ) {" s4 `4 c8 P2 s! q7 s- `2 Q
- add_filter( 'mce_external_plugins', 'add_plugin' );
5 j6 p# T$ L' G - add_filter( 'mce_buttons', 'register_button' );+ z+ ]" m* {; J' Z
- } ) U0 j0 h( Y" ~* n1 a" n. t% F3 s
- }
銴鋆賭誨蝣 嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁' D1 V& V# _$ Q( t5 }) Z
敺賣詨曉訖ordpress憪賣- add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣 ) Y4 G+ c$ F) W4 G8 O
4.3 冽
8 C4 v N# c0 d% F5 I% p6 }
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
& V/ I4 |! U$ j5 L7 R# X
暺嚗敶箏閰望霈頛詨交蝡賊嚗
( w2 |4 u% H% B* ^ C
頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
+ _# {. z. \4 `! y
貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
8 ^7 J8 d8 U7 t" a$ p( A% W 唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆 |
|