WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)
3 V6 k! }4 b% h" v3 O/ M, g5 b' Z# g- q% v& V; P2 Z
wordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐. V! X- G1 X: c( K7 h. W
隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具8 Y( i) v' y7 W( x
( F: u9 x. `3 V( t! E2 P
凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :
/ ^& u) p' U1 P) t甇文 , 臭誑閮剖銝訾批嗆蝡賊 :
: v; v0 B( j W; L甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :
9 Q8 R9 D( ~3 f& ?7 P5 Y" ^$ n, x: y: Q- [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣 ' V9 A3 C; z: u0 a
# [! C I' [* H4 c
1. 蝪∪桃凋誨蝣! j- K6 H# V- D% S6 I- o# E* g: j+ F
祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :
3 B+ o1 L4 t* b4 ?1 e1 ^0 k閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :
+ Z `# _) B& G& ~- 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
- 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
- 閮餃賣賊蝯針ordpressaction銝
( \& q" r' E' M3 I( Q+ I( M 2 f; X. `- D. S# }
祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准& `- ^0 y/ b9 ]. ~- \0 H$ q
1.1 撱箇餈賣
. h, r' n1 ^! e4 F( d" C: D嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡
' N2 g9 j% @5 p' ?* E- function recent_posts_function() {3 W0 I' R# i& Q5 J4 }/ L
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));
/ J1 y" z7 g, a% M. H9 o4 u - if (have_posts()) :& J- z# d! M( p0 Y4 H& s3 |
- while (have_posts()) : the_post();4 U5 p( a, k# q2 k5 H) w* x
- $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';
Z3 {% ]5 c) f- _, v - endwhile;
2 H# s; t1 W x1 Z% N - endif;; s" v) g3 G! W! J) y1 G; r
- wp_reset_query();
7 Z( \0 b- s- j# R - return $return_string;
9 g9 a: ]* \+ T% w/ }$ Q8 ~ - }
銴鋆賭誨蝣 憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯
1 `' ]7 G' x5 P" |: g
( }. U# n6 r* H" [% u. m1 X
3 D& \! l" T# ~$ M( o! _$ Y1 C. W1.2 閮餃凋誨蝣
, }+ t( R1 R4 h! z8 Y曉剁閮幃ordpress賣豢臭凋誨蝣潘- function register_shortcodes(){
6 l: u0 `' L, C9 m+ ]! h - add_shortcode('recent-posts', 'recent_posts_function');1 [1 G0 o. N1 }0 t
- }
銴鋆賭誨蝣 嗅冽蝡批捆銝凋蝙函凋誨蝣
D2 ~" e* i+ C4 @" N& D( ?2 V撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴
+ P9 }1 T" r4 r% g% k1 a; o+ O0 T! j& p$ H2 W) B/ h; A
$ I0 F/ I Q) e6 H1.3 Hook into WordPress& k: n8 l7 G, `) S: f
0 o. \. h* c/ Y9 P7 r
箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准$ F9 w, n `" N B- W+ D8 j
- add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣 ) P7 p: M2 d: G% e" @3 _7 G
1.4 皜祈岫凋誨蝣1 O2 t$ `4 U2 |3 Z
蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :2. 脤凋誨蝣 2.1 凋誨蝣澆 凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻 閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼 撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 : - function recent_posts_function($atts){$ E% S( A" j; X9 ~! p
- extract(shortcode_atts(array(6 p g" d/ J2 H% V6 O3 B5 D$ V
- 'posts' => 1, Q3 W' O- ?( D7 h$ p
- ), $atts));
( ^9 N, Z% K# M$ }, B - " r3 r; x8 ]) T7 u; m
- $return_string = '<ul>';' G6 t! b0 j0 A8 _& G8 ^
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));* t$ t0 l3 S( X, E5 p, d5 \9 o! Q
- if (have_posts()) :% L! _: P0 k4 V/ W0 t1 }
- while (have_posts()) : the_post();0 b2 s' R" f" |! ~$ t: ^4 G
- $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';; x7 V6 H5 F# R! v! H" C
- endwhile;* ^7 N0 w! J' F( \& ]7 z) ^; v% k
- endif;3 T/ W, L; {' X% U3 d' H+ X
- $return_string .= '</ul>';
$ P) R% H1 l- g5 {3 `, C - 7 t/ O' C/ B: `2 Y3 b: z
- wp_reset_query();2 J3 V# j1 Y5 b- S3 E& M. R
- return $return_string;
0 W7 d$ y/ ^2 W) R5 ?! Q6 [* T3 f1 V - }
銴鋆賭誨蝣 憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :
4 b1 {; _' E! D. P4 J$ a" {2.2 凋誨蝣潔葉憓摰
臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗 - function recent_posts_function($atts, $content = null) {) t; m! t; Y$ Z1 p) P6 S, j
- extract(shortcode_atts(array(8 D z2 @9 h/ b1 c7 m
- 'posts' => 1,
3 A* P, n! G& k" S - ), $atts));6 {) @- g ~+ d# u& j; [
-
' B7 O9 E* w8 x+ s' J5 S - $return_string = '<h3>'.$content.'</h3>';
+ [$ Z6 |% y- b! M& x9 `& S) l; X# c - $return_string .= '<ul>';5 N8 [+ A3 s6 W1 [
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));5 P5 ^0 Y2 f$ t6 C' N6 W
- if (have_posts()) :
( i8 }5 J P& K* q7 H$ i: ^+ M* Z9 M% R - while (have_posts()) : the_post();" ^: e1 d0 l* g; \
- $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
6 d( a2 E0 w* P5 d - endwhile;8 T/ |6 M/ O9 X3 F
- endif;
6 D$ y3 ~1 q7 R& E3 P9 h - $return_string .= '</ul>';
( |( k2 i9 @8 V7 J. v' b -
' L0 g# X% J4 p; ?# f' K( r - wp_reset_query();
) `1 L. C3 b/ |# J - return $return_string;6 t" O# Y' D# C' K" F. B# |
- }
銴鋆賭誨蝣 蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝- [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣 支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗3 I* I% `8 c. C- t0 P7 I
3. 其遙雿唳寥*蝷箇凋誨蝣 3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣 閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛嚗典臭誑撠甇斤撘蝣潭曉典撌亙
5 b) S, m3 D2 e' }# \9 e% x臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 : - add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣 曉其閬靽格嫣遙雿嗡唳 , 瑟, 湧甈撠甇蝣箏圈*蝷 * N$ r% m6 K: h) P$ Q& M: Q
璅嚗刻隢銝剖函凋誨蝣潘 - add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣 冽閬銝剖函凋誨蝣潘- add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣 4. TinyMCE蝺刻摩典凋誨蝣潭9 H( f; ^4 e3 }; f8 [
) M1 l" }3 _, o% n# r嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗 - 箸迨撱箇銝 JS 瑼獢
- 閮餃JS隞' q: F. a! n# q$ r- Y/ F% ^
) }- `" n3 h/ J3 y# X5 ^: l
4.1 JS隞嗡撱箇JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗 - (function() {" T. D" r( \& S4 N) G# [/ R# b
- tinymce.create('tinymce.plugins.recentposts', {
& C4 \- x8 U6 y' m, d! X3 f - init : function(ed, url) {% I8 W b7 }6 i/ Q+ g% n8 X. ^2 ~
- ed.addButton('recentposts', {0 n" ^' _! A4 C2 f- r' u- B9 p7 ]+ ~
- title : 'Recent posts',
; w, o! w9 m; c4 }. i - image : url+'/recentpostsbutton.png',
; b7 E, X9 u3 N+ T/ h8 C. V; k - onclick : function() {
7 k* o: J, C, s - var posts = prompt("蝡賊", "1");
% F0 c4 J# ~' b1 d! H( r& A$ j - var text = prompt("隢憛怠亙銵冽憿", "唳蝡");, n; ^3 {+ H: ~; U; \7 x
- 7 t2 |) J9 z. Z; q5 I( N# ?; d
- if (text != null && text != ''){; f9 G7 ~: u+ G" C
- if (posts != null && posts != '')0 ~+ f7 m$ C# T2 a% g9 B/ l3 o
- ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');
: ~% ~7 [/ |2 F - else
# S7 [: Z! ]8 w1 o - ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');. a4 |* [$ o- ~8 W8 w- M7 [- P
- }% J) f2 f5 l" I' B
- else{* p( L* q6 T) |7 B8 B
- if (posts != null && posts != '')
: P/ c! z8 Q# s2 n - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');9 D! s- d3 _. L( H0 J$ i
- else
: t$ _* }) `" d6 s9 @ - ed.execCommand('mceInsertContent', false, '[recent-posts]');
2 P+ W% F/ T# D1 a* L - }( v& t8 d, o9 z/ S; V' `
- }
1 z. A" e/ Y3 q1 q+ p6 \. o. h - });5 |) t- [+ e7 K
- },' {5 U t0 Z0 z" X9 j
- createControl : function(n, cm) {
0 C0 t/ A6 @" E0 \( I, v+ z' t - return null;) [; w* G. S! O$ O* ^
- }, V4 ?" \) z& ^/ r( M. ~, K/ n
- getInfo : function() {8 M; J% \8 K3 D3 x4 U5 d* e
- return {
& j# S' L7 k2 z - longname : "Recent Posts",$ J7 `0 e B8 D$ f4 m
- author : 'Specs',
- u- e( z- @' ^9 G2 L2 q4 Q$ i# B - authorurl : 'http://9iphp.com',* t* q5 X* ~5 f% S
- infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',
/ u6 p2 {* o+ P3 e/ y5 s - version : "1.0"3 p. ]. g, @' }8 o$ Q( S% M; I
- };5 A8 F$ a0 W& \
- }
/ ?; A. E: q; n% Q2 Z1 X - });1 N' p( w/ A. A" t% U( ?$ A. O
- tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);
- V4 g; R% v( P9 `, y/ v5 M+ W - })();
銴鋆賭誨蝣 澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨 onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉 敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准 . x/ b3 S- \4 f Y# i
4.2 閮餃TinyMCE隞
& O2 R1 g& M- D* e: V
2 j9 `* t/ p& r: `: w 嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉 蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩 - function register_button( $buttons ) {. I- ?$ R2 H2 O5 T! K. ]
- array_push( $buttons, "|", "recentposts" );
# H' y# G: p6 D, X. b - return $buttons;
5 Y4 L& @+ H* H5 s; A3 q7 p - }
銴鋆賭誨蝣 蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅
- P) h, v# D8 C2 d" ^3 c- function add_plugin( $plugin_array ) {
Z7 J5 ?! a$ f# R! k - $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';9 N* I8 o! C3 j; _8 p
- return $plugin_array;+ k! L1 J$ R3 S2 |( y
- }
銴鋆賭誨蝣 銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁
/ `1 D+ n9 ?+ r0 {6 {, e- function my_recent_posts_button() {
9 s: u q) F6 q U" a3 B! O - if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {3 R0 n. e0 C4 q( h5 `4 z
- return;
- k+ e; w5 i7 b- C0 X: H! f1 _8 b - }
8 d6 M! w' B1 q( r -
9 P7 _1 G4 ?. s" J5 B* q - if ( get_user_option('rich_editing') == 'true' ) {
: k2 Z; ]* Q& L+ K. D. R' } - add_filter( 'mce_external_plugins', 'add_plugin' );
3 o! v. t) v9 ]5 D2 _5 ~ - add_filter( 'mce_buttons', 'register_button' );% y* {& K/ P8 v E; }5 ~! i
- } ; F: U+ C# l) x# H
- }
銴鋆賭誨蝣 嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁
2 k* A8 H2 F5 j' G敺賣詨曉訖ordpress憪賣- add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣 ( f/ o7 G4 D H* q. [- h
4.3 冽' m7 r! Y- u9 y; P
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
/ S# p, ~0 t g0 L* d
暺嚗敶箏閰望霈頛詨交蝡賊嚗
& |; {) c, e( z5 R( H/ h
頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
/ R+ p4 J" @$ D9 D# \* l+ `' P% f 貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
. G6 E* T! z! a, ]$ C2 [/ o! B
唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆 |
|