WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)1 e) @" @1 V& p$ Y. S
0 @1 Z# R: f7 i! j" @, e% ^
wordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐
v( {: p1 s/ D" ?隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具
3 Y3 S( f- \& C1 R! A; Y Z% i. |8 k' A& C: w8 }1 Y5 c
凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :6 e8 J2 e" b( S2 Q& y( I
甇文 , 臭誑閮剖銝訾批嗆蝡賊 :8 ~) t8 B7 W/ S' G& T) x
甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :7 J" ~0 p z% ?' n6 Y( @
- [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣 % ^% `6 }. A; U9 B6 g
/ d, c* W* p S& V' o1. 蝪∪桃凋誨蝣
' O0 I: s) t( M. o+ p祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 : g9 ?% [, s9 l! Y3 P
閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :+ ?" F) z( Z* F* r, L) C# c
- 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
- 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
- 閮餃賣賊蝯針ordpressaction銝
- g7 ^2 p2 K& _2 c' H2 m6 X( n / j- S' [& Z; E: P- X
祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准
, o& \8 A9 [0 `% \) Y1.1 撱箇餈賣4 ~2 D1 v [$ l- s; m2 H7 Q
嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡$ |- ?* M; I$ {2 R4 b1 F
- function recent_posts_function() {8 j2 x' Z/ {0 {
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));6 m& I) E+ E- h! H
- if (have_posts()) :1 Y" p8 {0 ^# w# t+ U
- while (have_posts()) : the_post();
0 l2 ]* f9 B1 W4 n& r - $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';
% m" E: h; Y! c9 N$ u; [7 i' n - endwhile;
+ M( n# I5 l' j; O s6 p7 S) C' V! U - endif;
2 E# }( U4 K5 l4 E8 a - wp_reset_query();# s# u8 ?) a* W$ V) M
- return $return_string;
; a; z. U d- C) M/ J) V - }
銴鋆賭誨蝣 憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯
P2 R7 u* N. `, z# w1 a
" \8 g+ l- r. O' m1 w) E$ Z) B# {& ^" J% `3 A
1.2 閮餃凋誨蝣
0 S2 ]7 H2 S* \1 G, G: G曉剁閮幃ordpress賣豢臭凋誨蝣潘- function register_shortcodes(){$ ?! X# Q& b$ V2 T
- add_shortcode('recent-posts', 'recent_posts_function');, ^. J' ~3 O* @
- }
銴鋆賭誨蝣 嗅冽蝡批捆銝凋蝙函凋誨蝣
6 x5 Q/ i: {% P5 Y+ h撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴 m+ _3 H7 C1 x9 s% y: [+ D
, m: m4 a1 \: Z- h" ~" R4 S
1 R+ Q8 z/ k5 |2 k0 C8 z1.3 Hook into WordPress1 @3 h. a. f! b& g9 t
6 [5 B9 |& ~3 O {* X B
箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准
9 j! `7 z4 n) K9 q- add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣
R! U" E, S' M1.4 皜祈岫凋誨蝣; ~8 L' P: N$ U% g
蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :2. 脤凋誨蝣 2.1 凋誨蝣澆 凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻 閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼 撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 : - function recent_posts_function($atts){
4 S3 D7 R5 J! p$ g |, \# P4 V - extract(shortcode_atts(array(; B' e6 @ r6 Q: B, x
- 'posts' => 1,; f. @" l% G, y
- ), $atts));
7 @8 X d1 V0 y0 C -
$ W4 Y0 G% r% R( j& ^ - $return_string = '<ul>';
! G: H& E% I y: p" t% a! ? z* Y - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
3 U( \. M1 a. Y4 \9 r - if (have_posts()) :5 H1 U% h( i8 {6 O
- while (have_posts()) : the_post();4 H' T' s5 G, ]$ L6 L
- $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
# u+ N8 H% B& A" b/ e; q% V* A/ ? - endwhile;
! p# ?2 t0 a7 C6 F# y9 c - endif;
- v9 c5 C1 `* i+ | - $return_string .= '</ul>';- @- T+ ^6 H$ b
-
/ }3 U) n9 ~$ o4 {1 \) y - wp_reset_query();
3 k4 h9 s- Y! n2 [ - return $return_string;
8 q* U& R8 G$ q" y* X+ X5 K. r f - }
銴鋆賭誨蝣 憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :% k; j" N1 X) g& s0 G
2.2 凋誨蝣潔葉憓摰 臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗 - function recent_posts_function($atts, $content = null) {+ ]2 |) V/ }! ]3 T- J; d K
- extract(shortcode_atts(array(
4 |3 z3 c3 P, G- ? - 'posts' => 1,
* D* s r; a) d: l7 ?7 a' b - ), $atts));
, j, p. d. c7 X* b4 s - ( Q0 L- H) y, y! N8 m5 }7 L! n
- $return_string = '<h3>'.$content.'</h3>';
" n( E9 l. u. j: | - $return_string .= '<ul>';$ O+ |8 h" c* a3 r+ w) q
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
y2 [7 @: j. { - if (have_posts()) :, u! g0 r) H$ }# \, o$ m
- while (have_posts()) : the_post();0 j. o5 k2 n% _# K
- $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';3 V; _) \# Z7 j9 Q5 c
- endwhile;) I+ B; I9 N9 H3 }3 N1 ]; M2 B
- endif;
) M$ z1 S0 \" \# V# @ - $return_string .= '</ul>';/ ]: o: x- g# Y" @
- ( h! \5 H# a* i$ r0 i& ]7 s5 ?/ X
- wp_reset_query();% b) ~/ @! o/ r( U$ H, x4 A
- return $return_string;
6 D9 k( ]0 D$ y( A - }
銴鋆賭誨蝣 蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝- [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣 支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗3 _ C/ J$ C; s/ R1 d
3. 其遙雿唳寥*蝷箇凋誨蝣 3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣 閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛嚗典臭誑撠甇斤撘蝣潭曉典撌亙 & y9 ~- r$ t4 p% X; ]% {5 N, H) y
臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 : - add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣 曉其閬靽格嫣遙雿嗡唳 , 瑟, 湧甈撠甇蝣箏圈*蝷 # J$ Q& E; T" a/ U
璅嚗刻隢銝剖函凋誨蝣潘 - add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣 冽閬銝剖函凋誨蝣潘- add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣 4. TinyMCE蝺刻摩典凋誨蝣潭. W3 S* `- L* w& K
6 J! V$ Z* U( z! l* g
嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗 - 箸迨撱箇銝 JS 瑼獢
- 閮餃JS隞
0 H- r7 X' g/ s- @ 4 X7 }5 W! J; m
4.1 JS隞嗡撱箇JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗 - (function() {
8 Y* I* K9 E- ]7 T" `, J - tinymce.create('tinymce.plugins.recentposts', {
- W, C! g; _- H, |5 [' G - init : function(ed, url) {( b& x- P' g; t8 N
- ed.addButton('recentposts', {! N% `% O5 e0 d, ]
- title : 'Recent posts',# |% Q6 l* f0 M3 ?" G+ g
- image : url+'/recentpostsbutton.png',
! B7 T X1 l2 K - onclick : function() {
) ?& Z* e# b/ Z; p3 }8 Y% T - var posts = prompt("蝡賊", "1");' V9 O% J# S' P6 ?9 y
- var text = prompt("隢憛怠亙銵冽憿", "唳蝡");& @* t$ U/ j! v) `% o
-
1 v7 {( ?, z+ a; B0 o - if (text != null && text != ''){% U" j% n2 ^+ G) G# {; w
- if (posts != null && posts != '')! a& x2 {3 S, |
- ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');+ _) I9 K, s* \/ A3 `" x
- else- ?1 T8 ] Q' T5 V# m }' X' q
- ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');6 X0 B! R4 o' k. X9 S* y2 g7 E+ `
- }& R8 ]/ y/ W! ?6 S6 |7 s+ p, s
- else{, i7 C! V2 z( f3 ?2 e% Z' {
- if (posts != null && posts != '')
- c& c# E8 C/ y& o% l( t - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');& r4 Q# Z8 ^/ L+ T+ w
- else" t2 V' G+ w. H
- ed.execCommand('mceInsertContent', false, '[recent-posts]');
, n3 c) z Z6 R, \. L. a2 Q9 g - }
" v1 N! W8 c8 z* m4 d) n - }
2 L( |4 g# C& o* R0 M* k - });6 C; l$ ?5 I" P7 ^+ v; }
- },; b4 J/ Q8 y* x, v% P& e
- createControl : function(n, cm) {
8 s) c3 N9 k r+ f [3 j - return null;
1 y+ K/ N0 ^" W0 _& S - },
/ e$ t6 b2 ]$ W( V& [- p9 }' y - getInfo : function() {
}* Y* d9 Q% B9 Y% d. J) r - return {( y# N! E Q( a% K
- longname : "Recent Posts",
0 P7 x; R4 o$ o" R* P - author : 'Specs'," S( B2 c$ m/ B3 h5 ?2 S O! e
- authorurl : 'http://9iphp.com',- |) O* M! L$ q: i8 M6 u
- infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',
5 [7 R. _' W% [* O - version : "1.0"0 v! g; A. z( u, Z% x1 v. n W
- };
; w: q+ D ^ S1 I& K6 e% o4 C - }
3 W" T; p% \7 P! T! a2 H. T8 p - });
; |- H d: c* B - tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);* t' ~* B9 E. }4 N( z8 U" h
- })();
銴鋆賭誨蝣 澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨 onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉 敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准 ' M. X$ q& S+ `: T5 j
4.2 閮餃TinyMCE隞
6 L6 \" i; O# `4 c9 A1 g$ B2 V* O* F$ d1 ?
嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉 蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩 - function register_button( $buttons ) {
" ]+ P; D3 P+ b' Q) |8 e - array_push( $buttons, "|", "recentposts" );
1 ^9 J2 }2 M8 O. u! d, ]3 v - return $buttons;% Z0 f. v/ b: {- K, ?( {
- }
銴鋆賭誨蝣 蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅
) K5 \0 Q8 Y' Y- function add_plugin( $plugin_array ) {( f" O' x B% e$ A/ W
- $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';
$ G; i" X5 T0 d1 w* D" M3 ] - return $plugin_array;8 C- u* x+ w Z1 M5 B; D, u
- }
銴鋆賭誨蝣 銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁
6 W% K3 N5 K: |; G- function my_recent_posts_button() {
. I7 p& r# E/ E - if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {) P) c7 y; J% |5 O4 C+ _
- return;7 h2 L& D" O$ g' G) U
- }
0 {& N2 s3 g4 S2 u5 C% x - I: I1 Z+ r/ M. ]7 l& @
- if ( get_user_option('rich_editing') == 'true' ) {
) h( [" M2 }, Q - add_filter( 'mce_external_plugins', 'add_plugin' );
. Q8 e- E' O0 g( \1 w& f9 T - add_filter( 'mce_buttons', 'register_button' );0 H+ `7 q; }5 h0 G) L* h. C
- }
! H: ~. [2 k. l7 r" _- Z - }
銴鋆賭誨蝣 嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁
~. I& j( ?. x# L2 ~敺賣詨曉訖ordpress憪賣- add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣 % x3 l2 y7 u1 ^/ m" e! m
4.3 冽
1 ]% Q9 w2 x3 ]. B+ x
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
0 h& B! p- d2 M# M 暺嚗敶箏閰望霈頛詨交蝡賊嚗
/ X- t N; C6 b$ u5 _
頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
$ Z! J9 G% C/ l9 c5 _
貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
- T0 G# g' O" `
唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆 |
|