WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)7 L3 ^2 k7 z6 A
7 R3 p- z; c2 n+ o; cwordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐
) m+ l7 T6 {- z4 K: ]1 v8 c* `# D隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具9 ^. a0 h! `% U9 E; Q
& Z- t8 t9 n& Y6 D
凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :" D/ F2 m6 F7 @( {7 s
甇文 , 臭誑閮剖銝訾批嗆蝡賊 :. C8 k; S' |& \4 u2 A& V
甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :, a/ f% _8 y; `& p8 R5 G
- [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣
# ?" @7 ~0 `4 z+ u) e) U L) @8 [
2 K1 A8 `$ L, P' E* T* i1. 蝪∪桃凋誨蝣6 ^% j0 q9 @) k, ?* K9 r& @
祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :
- b" n" w" z% `/ }. A( M閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :
, i! U7 _3 r" N! E. z- 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
- 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
- 閮餃賣賊蝯針ordpressaction銝8 _2 H: N) s( w$ l0 ?
$ [- \$ ]" k. _# u祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准! l o! N' \ p' f& u; g
1.1 撱箇餈賣& N! K+ e8 j" G M4 A- |
嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡
7 X: F* B& W1 B$ }: S- function recent_posts_function() {9 }: A8 t9 B6 f. s. u3 D2 D5 j( i4 [
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));
: p8 k2 Q0 s4 ~* E$ b - if (have_posts()) :
/ X5 p7 Y, v3 U# \. u/ D/ ^ - while (have_posts()) : the_post();
5 x* [+ h' i. W# i - $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';
4 k* j" V( ]+ Q! y* O: ^ - endwhile;+ E* p% n/ ^! f7 G3 l
- endif;
* w6 d- ]# Q2 s, R8 D# k - wp_reset_query();$ j* Q- J2 M- q7 r5 z" i
- return $return_string;
7 q4 P0 N' _/ J: x) l8 I k* N. b - }
銴鋆賭誨蝣 憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯7 c$ {3 `5 B+ B
% L9 j5 I% Y M; q" V' L
" P: L% K" T5 p+ S1.2 閮餃凋誨蝣
/ h( n3 M* W" m* A曉剁閮幃ordpress賣豢臭凋誨蝣潘- function register_shortcodes(){+ g+ p" `( Y9 U+ G7 V# [
- add_shortcode('recent-posts', 'recent_posts_function');
& g% ]# p/ a [ - }
銴鋆賭誨蝣 嗅冽蝡批捆銝凋蝙函凋誨蝣6 T! h( u$ u: ?. s L4 [) Z% _
撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴2 h# T3 a4 I) Z/ K+ D
% ?8 q: j( U/ X ~/ A4 P+ H2 C/ p
1.3 Hook into WordPress1 c- ^1 C7 I* H3 [# C
% C0 ~2 l; p6 ]6 z; X箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准6 R, v- s3 ]$ d9 I3 q
- add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣 4 y% ~' Y6 n: L* {
1.4 皜祈岫凋誨蝣
: o( U, _# z# Q8 E( T0 c3 a4 e蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :2. 脤凋誨蝣 2.1 凋誨蝣澆 凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻 閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼 撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 : - function recent_posts_function($atts){
% P$ m% S3 r1 u& _$ O2 A - extract(shortcode_atts(array(/ [8 v: v0 w: Y* ?6 v
- 'posts' => 1,
" _, B; }" H* T - ), $atts));6 w _9 H/ D/ s6 T! Y
-
2 ^& L' z' ?! g; \$ i) p - $return_string = '<ul>';/ G! W! p6 q9 O5 y
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));1 \# @, x: b) E. Q- |# P
- if (have_posts()) :: S$ n. E/ M* Z" y9 N
- while (have_posts()) : the_post();$ K9 o5 P8 J' K S4 A$ H4 ?; |
- $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';/ v/ ^9 [ S2 Z. Z
- endwhile;
) z3 y. l9 x( b9 G& g - endif;
7 H, }- r: z! r8 [+ Q, M6 K; J5 y - $return_string .= '</ul>';
! [; A* `% s6 T; X. \ - ' D& V2 B9 c7 E! z& s. ?/ ~
- wp_reset_query();, q, t# i6 ^" k4 S) l- L
- return $return_string;
& @1 H/ O7 |: f6 X6 I) Y& U - }
銴鋆賭誨蝣 憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :5 Y) ]0 P2 |7 Y
2.2 凋誨蝣潔葉憓摰 臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗 - function recent_posts_function($atts, $content = null) {, V! q2 l. ?5 Z1 M. d
- extract(shortcode_atts(array(" }' ]/ T9 x& t1 s3 B' H- Q
- 'posts' => 1,
+ O$ O8 r, D0 U2 I1 D6 b - ), $atts));
I: u0 e g0 J0 I, m" D - . x; P% v" e1 L
- $return_string = '<h3>'.$content.'</h3>';
% y1 a2 B+ c: H) J) d; L0 p - $return_string .= '<ul>';, t" I4 B7 a; o+ P
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));+ J7 S8 F0 `9 q8 Q" _
- if (have_posts()) :6 x2 Y7 v! u0 c. Y8 Z% \
- while (have_posts()) : the_post();
6 ~. _& O W! g. l) y9 g4 a' t# o7 f# M& F- h - $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';7 F+ H# m; n% x7 [0 |) Y4 m+ P
- endwhile;( c5 m% x) H$ ]7 s* d
- endif;0 c: K; ]$ h# }$ q$ N
- $return_string .= '</ul>';7 p8 G$ R; ?2 V, Y& Q; I; _4 j+ o
-
0 x4 {4 T+ X6 V1 f% k f - wp_reset_query();( C% X; J% K0 H5 _- {4 l
- return $return_string;& I; e: i# @2 y0 x: B
- }
銴鋆賭誨蝣 蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝- [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣 支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗
$ Q6 A7 I9 a: |( |: A3. 其遙雿唳寥*蝷箇凋誨蝣 3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣 閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛嚗典臭誑撠甇斤撘蝣潭曉典撌亙 . }& F" w" w+ T
臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 : - add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣 曉其閬靽格嫣遙雿嗡唳 , 瑟, 湧甈撠甇蝣箏圈*蝷
7 k- |" k" \. E' u璅嚗刻隢銝剖函凋誨蝣潘 - add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣 冽閬銝剖函凋誨蝣潘- add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣 4. TinyMCE蝺刻摩典凋誨蝣潭+ T! I7 @6 T/ p' y
: p" t+ J8 M! T
嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗 - 箸迨撱箇銝 JS 瑼獢
- 閮餃JS隞* p6 j. ~' l5 d" K( ~. g& V5 P3 ^
: |1 N. |. x; V( H! s
4.1 JS隞嗡撱箇JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗 - (function() {
G" S3 z5 `3 F- }2 s* T - tinymce.create('tinymce.plugins.recentposts', {
0 B$ T0 O0 v$ N, e: w - init : function(ed, url) {
1 x' t6 h m" X - ed.addButton('recentposts', {
2 _3 S# k: @* s% ^' \* G8 z, r - title : 'Recent posts',
3 b# |6 _2 o2 T; _1 _4 z) A0 D9 l - image : url+'/recentpostsbutton.png',7 F( I# U; K3 e
- onclick : function() {; V0 I* f8 ]: _" ^5 a" |
- var posts = prompt("蝡賊", "1");: F4 }: h& Q# l' _# N( a
- var text = prompt("隢憛怠亙銵冽憿", "唳蝡");' v8 _, u+ ?. S) N+ a. w
-
, K! r% B' X- w/ w - if (text != null && text != ''){% {" y' t% y0 ^. z+ {
- if (posts != null && posts != '')' A! B4 K H# s: ?. @& m8 `& C# n
- ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');
6 _4 N! r; Y2 c; U5 T5 ~* I - else
* j$ r9 f6 R2 i4 v - ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');5 ]+ }9 n6 r: ~) n4 Z: W+ Z. l
- }& p9 A1 |$ G3 h6 m' z9 @: e& U
- else{1 e0 G5 q P; w8 w
- if (posts != null && posts != ''). X7 D3 \8 q$ ]5 i+ v. O
- ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');1 V* r/ S$ f3 J# k# @
- else @9 N1 q; |4 `! [& D1 \+ c* C
- ed.execCommand('mceInsertContent', false, '[recent-posts]');% N. p5 ] p4 U6 Y( O8 ~8 B% Y
- }# v8 s( P1 C; ]! }
- }
# i% L9 k, w- |& P" h - });5 c4 [# J3 L/ `" c& h) n
- },
- D: `5 w$ \; a+ d P8 b - createControl : function(n, cm) {( V! d1 H0 W3 n2 g8 H/ C
- return null;
7 z0 [0 z! z* H+ k- ` - },/ K. t% D* k7 y& U
- getInfo : function() {
q" H9 a! q$ n - return {+ {& B1 \9 O+ R" [0 p; u6 p% M
- longname : "Recent Posts",. B1 y1 ^# F. n! V: b& M. i% A- i
- author : 'Specs',
0 `( V- M" J' m" n' @5 _5 o v) O - authorurl : 'http://9iphp.com',
0 u% K0 y/ F# e' f1 c3 { - infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',
0 u/ C g; Q0 b* f - version : "1.0"3 P9 \- y" V4 s; `0 s
- };" J0 g: P) @1 A! G0 U: T6 i
- }% V: R) k$ w8 E9 q6 B7 p) c# k' g" ]
- });; u+ N4 f. |9 U
- tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);
9 C% u7 A: @8 p - })();
銴鋆賭誨蝣 澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨 onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉 敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准
( Z1 o+ W) h/ Z f" D9 n
4.2 閮餃TinyMCE隞3 N( l" |4 I; X9 p8 D: u' N
" S+ n3 s7 `! l0 o6 Q
嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉 蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩 - function register_button( $buttons ) {( e8 c, O" I- V8 N% q5 j
- array_push( $buttons, "|", "recentposts" );6 c8 h& o1 }$ t I
- return $buttons;
3 M6 a. _6 `8 B - }
銴鋆賭誨蝣 蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅
6 V9 k/ J# l. e! R+ V5 Y- function add_plugin( $plugin_array ) {
9 \- r4 H6 L0 q3 @5 P8 | - $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';
* A5 i: p6 @. D% K4 q2 i$ k - return $plugin_array;) b2 S# r+ k* D
- }
銴鋆賭誨蝣 銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁* k$ ^9 s% C, T2 o O
- function my_recent_posts_button() { . P3 p! {: s3 P$ q& G
- if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {: y6 ^. i# @+ |' Q' E0 Z! x
- return;
4 z0 z! }8 R2 e3 @ - }
. {$ h/ a) U1 y" a$ @ -
6 e- `5 J2 I, F+ J+ P: z$ E - if ( get_user_option('rich_editing') == 'true' ) {
! o( e V% Q( s4 F, k - add_filter( 'mce_external_plugins', 'add_plugin' );
$ ^/ p S4 U! p5 O8 I" i" ~* R0 g - add_filter( 'mce_buttons', 'register_button' );/ b- @8 L5 `& V/ B V/ P( W, h9 F
- } , d5 ~( X, A0 M" V
- }
銴鋆賭誨蝣 嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁
2 [+ J: S6 h, k; i! A i6 T$ F% n+ ^! `敺賣詨曉訖ordpress憪賣- add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣
9 P; h1 N; @; ^8 `" M# [4 |. c) \- `4.3 冽. y+ u& C% K3 b }4 D9 h; @
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
4 O0 a1 q, W& B% H2 i1 b6 V6 B 暺嚗敶箏閰望霈頛詨交蝡賊嚗
) C* x+ F. G2 V+ m3 T 頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
/ n, r) l. N& Q; Z% l/ v/ y
貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
! d/ L9 N2 Y. F4 z$ s
唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆 |
|