WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)
) W s# B8 Q& r Y5 H* _7 Y0 C& ?
/ q, C4 e& K" _4 d3 o$ ~0 Hwordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐 f6 C. z4 g1 Y3 m, d
隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具
1 e3 M' |' x% r6 }2 V
% O7 Z) @# j; G凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :# X- d7 z! x4 }8 z2 c8 g0 L
甇文 , 臭誑閮剖銝訾批嗆蝡賊 :7 @( H7 s- r% |% f8 \
甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :
# @8 I0 V) j! o" T1 V$ S) y- [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣
0 A- r! z I- ?+ }5 \5 w/ G' t
% h" p6 x; B, f0 t" `5 V1. 蝪∪桃凋誨蝣
9 p; M# C' F. o祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :8 |$ U2 \2 I' J
閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :/ M# C" E; x; r% J( b9 v* ?
- 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
- 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
- 閮餃賣賊蝯針ordpressaction銝
3 G; `. ] _5 V% ^. U' j7 H1 [: ^: d $ T" D+ m, f/ A/ z+ h8 c+ i
祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准% L% M1 n1 [9 {2 Q; V8 K
1.1 撱箇餈賣
& O6 q4 G: f! e: e: @0 s嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡
/ }2 E' b" m5 }; B9 d- function recent_posts_function() {' ?6 \5 | F q+ y0 Z' @0 W7 {
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));1 w' o5 Z# I" y4 m/ y' C
- if (have_posts()) :
/ X# d) m2 I1 O5 c: [3 B - while (have_posts()) : the_post();
+ ~8 a( E# t8 }; T( q - $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';
* ?. N0 Q) h8 e+ r1 |9 N - endwhile;: c) O2 Y. D' `% q# t* F$ g7 Q$ u. {8 C
- endif;
5 ~; c9 W5 W- q( R1 P4 {0 t - wp_reset_query();# I6 c( K: c% H$ ~
- return $return_string;0 }' y' }2 G, ] |
- }
銴鋆賭誨蝣 憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯+ O* z7 x% [: f, u0 L/ A& N8 x
+ `0 ?* m! y9 m
" h2 Y( u: R$ {. L5 v
1.2 閮餃凋誨蝣
( x3 H$ Q2 f6 V- S$ H曉剁閮幃ordpress賣豢臭凋誨蝣潘- function register_shortcodes(){" o2 q7 x6 `% P, K# G
- add_shortcode('recent-posts', 'recent_posts_function');
# M6 [: o: u" e - }
銴鋆賭誨蝣 嗅冽蝡批捆銝凋蝙函凋誨蝣
2 J2 q, ] o G7 j( @; D撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴0 ^* l a( v/ k G
. I7 C" f9 {4 t. E
& b+ {- {) R' x. D! X. o: G
1.3 Hook into WordPress
( H- p p9 S( [9 O. u! M7 b. b
4 K: d5 e5 [+ U* l: B( @( O: L箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准
" \$ F6 u0 p8 G/ P- add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣 / C/ G2 X# u* G. b% w
1.4 皜祈岫凋誨蝣: Q ~* ~; a2 F. F1 u2 N4 F3 X: a
蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :2. 脤凋誨蝣 2.1 凋誨蝣澆 凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻 閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼 撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 : - function recent_posts_function($atts){: a$ I. B' L3 V( A- x; b
- extract(shortcode_atts(array(# o3 ?8 j8 G; F& ]! g! s4 w' }( U
- 'posts' => 1,
u+ N1 V. {: ^7 a - ), $atts));# Q8 M$ B1 ` ?$ y
- . t/ o" B! f( w- M' F$ @
- $return_string = '<ul>';
/ c" c( M) o6 n$ Y( K& T - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
9 _, c# A4 J% a: b - if (have_posts()) :
. h, n& t! H% J/ a) L7 B' v& g) ~0 P - while (have_posts()) : the_post();
& j4 ~) \" ]) m, u - $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';' l; |7 a% \! l2 X0 T1 c: H
- endwhile;! j) y; n# z% ?
- endif;
7 }0 @) m8 D3 {# V& T - $return_string .= '</ul>';7 R- o: |0 l6 X0 b: O" Q
- ) v- n4 J. R# ^, N
- wp_reset_query();
. H9 x0 U+ n N+ U+ H- o* } - return $return_string;
! c- h. _, R' u0 x* Q( d$ n - }
銴鋆賭誨蝣 憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :
( o( h: n, K. J7 c# v1 g, @* y2.2 凋誨蝣潔葉憓摰
臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗 - function recent_posts_function($atts, $content = null) {
6 T2 z8 D2 H2 L* H+ R' I/ o* v7 I - extract(shortcode_atts(array(
" L! n( R4 [( |3 [6 _1 m - 'posts' => 1,: o3 l7 U1 X) h8 j5 X* P
- ), $atts));/ ~1 y( G% |2 R6 b) k
- , x0 N7 J' |* N+ F
- $return_string = '<h3>'.$content.'</h3>';3 W& r) R# c/ L# I! _! n
- $return_string .= '<ul>';* q8 B8 l5 E _
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
* ^! w& X: F$ f+ o$ h - if (have_posts()) :
5 K; X# z2 K0 O# M3 V0 Y/ j0 H# f - while (have_posts()) : the_post();
" |! q s6 _$ M" z; e" k6 } - $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>'; B4 D- y2 n- S5 h2 W
- endwhile;% T; Q9 L1 T' r8 U G6 A# ?
- endif;
, d1 [" x$ V+ O/ P0 m& i - $return_string .= '</ul>';
3 e% b' l7 t: x; ?( _1 w -
8 D# D1 ^* J4 b' H! W+ f0 N8 ?1 e6 K - wp_reset_query();
" i- K3 I% G4 f) J6 o8 |0 H - return $return_string;
* ?6 v) ~9 U6 Y, N2 P - }
銴鋆賭誨蝣 蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝- [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣 支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗
- ^, h& L! ?4 W, V* P# U3 l9 G3. 其遙雿唳寥*蝷箇凋誨蝣 3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣 閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛嚗典臭誑撠甇斤撘蝣潭曉典撌亙
; T6 u! J( q( ~8 ^臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 : - add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣 曉其閬靽格嫣遙雿嗡唳 , 瑟, 湧甈撠甇蝣箏圈*蝷 1 _7 d$ s) E" N/ Z1 a: T2 a
璅嚗刻隢銝剖函凋誨蝣潘 - add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣 冽閬銝剖函凋誨蝣潘- add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣 4. TinyMCE蝺刻摩典凋誨蝣潭5 j8 a0 `, S% z; P" F
/ ?# @2 P, t5 q0 n嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗 - 箸迨撱箇銝 JS 瑼獢
- 閮餃JS隞 ~3 T/ T$ e9 u+ [5 ], u
3 g( X( P: J. f0 R; H# x: E4.1 JS隞嗡撱箇JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗 - (function() {
1 K/ Z1 G8 R) ]+ K - tinymce.create('tinymce.plugins.recentposts', {5 G& \; W' H8 x% T/ B, c' g
- init : function(ed, url) {9 r) V0 o" I3 m. Z" n
- ed.addButton('recentposts', {$ X* p' T( J: _1 o$ {
- title : 'Recent posts',
- ]- V! ^+ J3 Q4 A! W$ m - image : url+'/recentpostsbutton.png',
& y/ {7 P1 X! x1 c/ ^& D7 D) c% P - onclick : function() {
+ y3 m/ B |* C6 u& e* C! n - var posts = prompt("蝡賊", "1");
$ ~$ ?6 y) Z ]- e1 w+ U! \ - var text = prompt("隢憛怠亙銵冽憿", "唳蝡");& V! s3 i' T" Z, u0 G
- 4 y- o# g2 q8 n k
- if (text != null && text != ''){
8 x; \0 r* e9 D" M3 s' }7 k$ \3 @( a. g - if (posts != null && posts != '')
2 \: B5 P7 H9 E K% X - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');, u; P5 C) A N6 G. X; P
- else% T& x5 W8 r9 L( R3 E
- ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');5 b" j5 ]6 D$ |9 s+ z
- }" E- U# U, Z# T8 `' [8 ~
- else{
) m) m- |- G3 R; E& U8 p" H6 \8 D - if (posts != null && posts != '')( W& r4 C; u' X4 \
- ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');% `' M( R/ Q% h* m9 `- m1 Q, _
- else, W) w+ e- {+ ]1 n) @
- ed.execCommand('mceInsertContent', false, '[recent-posts]');& L; w! G3 K% T
- }
3 m2 W& x- m! {; S. ? - }/ n" I+ q8 P- s/ O, H
- });
7 v+ |% u" B" U9 l2 ` - },
( C3 m4 q. v! C; n - createControl : function(n, cm) {- d0 t/ o6 U' T: G8 L3 A1 I
- return null;4 f1 x9 c) D, m7 V/ {
- },$ w' s! o, b4 c1 g0 W0 _3 g Z
- getInfo : function() {& D5 G1 W' A2 h7 J# t
- return {( O7 ^2 u3 }. [) o% {
- longname : "Recent Posts",
! M" V. `: o/ H* b& k7 I - author : 'Specs',3 b s) Z- Z! K1 @2 ^
- authorurl : 'http://9iphp.com',
" z! O0 m* B, X/ r2 v - infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',
' {# ~! c8 ~7 I - version : "1.0"% B$ h1 @: y0 S' Q( [' i; p( u
- };
5 T) k8 K9 S o4 ~ k- G2 L - }
+ m3 h: Z' } o9 d/ h2 Q - });7 D! n5 n9 Q# Y2 g1 P
- tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);& @; M( B5 E- w! r" @
- })();
銴鋆賭誨蝣 澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨 onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉 敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准 5 ^7 Y2 ^. }: z5 q E1 V* O8 B4 u7 l
4.2 閮餃TinyMCE隞
; F v- g$ W5 z" l$ [7 } W' V7 a( {) @4 b f+ ~* I, _% D' S
嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉 蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩 - function register_button( $buttons ) {) X. X, l3 l+ P% G
- array_push( $buttons, "|", "recentposts" );" Q H Q; r p G% p+ \7 @/ q
- return $buttons;
/ ]7 T8 b& @! S( D/ d- r. G - }
銴鋆賭誨蝣 蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅9 y6 ?' t5 o1 K! Q$ ]9 k
- function add_plugin( $plugin_array ) {
0 _8 w- W5 @6 w; A K; S - $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';
! {, q D$ J" a7 o - return $plugin_array;2 A, L/ `+ ? w9 y; E( ~& a) H
- }
銴鋆賭誨蝣 銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁
. J( k X+ j/ ^& M: V4 n- function my_recent_posts_button() {
0 I/ Q. W( H, q5 |$ q& R - if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {9 U) `- Q3 [" o$ Z- f6 r2 \
- return;
6 P H) }8 ]& z4 f' e - }
. g$ b5 \0 M/ P0 u8 l9 U -
! ^1 Y3 |) H6 S; C& \; {! X1 D% m4 f - if ( get_user_option('rich_editing') == 'true' ) {
; ~& B+ a8 x2 C - add_filter( 'mce_external_plugins', 'add_plugin' );9 Q1 u1 h. u; i/ l% e
- add_filter( 'mce_buttons', 'register_button' );# d. x0 |& g+ E7 L0 T1 k- [( c0 h3 m
- }
8 w' |' ?5 ^4 \- D& j! h. e - }
銴鋆賭誨蝣 嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁! i- L B1 ^+ R) `5 Z) L6 U
敺賣詨曉訖ordpress憪賣- add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣
, M9 X2 K l" C, X N0 |4.3 冽# p- z8 Y+ v0 @) ?- k. M- T7 v/ h
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
' Q6 Y6 t$ M+ m S
暺嚗敶箏閰望霈頛詨交蝡賊嚗
: u# e( ?7 [; H: ] 頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
[) f$ O9 t4 @ 貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
' [% y) F, W8 E 唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆 |
|