WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)
9 D& b* V% Z6 u* G9 |' S' v9 v
" `/ ^, W. O& M" x, k0 W# zwordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐1 O9 H. n r- W0 T. X$ \. Y; v
隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具
, J* P( T! _/ r$ c, U# F- w0 F1 R+ _ a! ^
凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :
8 R9 n5 s4 H. S甇文 , 臭誑閮剖銝訾批嗆蝡賊 :
9 R9 h% A. }3 J( E; m2 i甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :
( l; t8 V* f9 Q7 q- [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣 * z3 E J. P+ m$ ~
: `: r; @2 k# V' u
1. 蝪∪桃凋誨蝣
+ L) m" a0 k, W2 O' o0 r祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :- A3 R- h" M7 W8 i$ I4 ?( |
閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :' }4 s1 l& m" i' `7 i) U( _# @
- 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
- 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
- 閮餃賣賊蝯針ordpressaction銝
/ X' L& C& A3 t- z $ {% J0 o L0 f$ K# {6 R" j, |. Z. ^
祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准
3 t7 S1 | t; o, F: T' W1.1 撱箇餈賣
' d: _. N- S, r) w( s嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡
8 W! p& [, O* g7 R! O' _- function recent_posts_function() {' ]. p" u" F: f$ o) q' w8 K) p
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));
) _' u7 H; F3 }+ F- K3 k& l+ n `" H - if (have_posts()) :8 i' p! e% _$ V* s
- while (have_posts()) : the_post();$ Z1 W! \9 f& i0 i1 Z8 z+ b& b
- $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';$ M9 K. F5 b/ h3 `8 l! u% |3 Y
- endwhile;! H2 G- @+ z9 ~" N
- endif;6 ]9 L! M) F7 Z0 z
- wp_reset_query();
: q5 v5 v2 X# p4 f - return $return_string;8 |0 @9 @! D3 f! o1 N
- }
銴鋆賭誨蝣 憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯
1 x- Y# {& b4 u' h# N
* b- n$ K" \! C& a1 `. R1 [, E" h, X; j
1.2 閮餃凋誨蝣; w! X. L: N- O+ B0 c ^& t
曉剁閮幃ordpress賣豢臭凋誨蝣潘- function register_shortcodes(){' A* o3 r! |' o
- add_shortcode('recent-posts', 'recent_posts_function');
6 i+ p! ~% ~ }* T8 U+ | - }
銴鋆賭誨蝣 嗅冽蝡批捆銝凋蝙函凋誨蝣
8 r9 e# p: c7 [5 c9 T2 G撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴
& j# l4 V# ?2 O/ T1 L
( \/ B2 T+ `; e
, R( J2 z) ~: w( q! L1.3 Hook into WordPress. c. X9 r0 q5 a+ s/ e% _1 j
0 \% Q- d0 W6 S& A0 F箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准
0 \- k% C" w. K& L$ o9 X# I6 D- add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣
8 [" l3 P' ?7 Q. Y6 \! X1.4 皜祈岫凋誨蝣9 p$ v) \* D# C. @
蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :2. 脤凋誨蝣 2.1 凋誨蝣澆 凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻 閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼 撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 : - function recent_posts_function($atts){$ Z" Q3 n- @1 d3 @, Z3 ?' y4 u
- extract(shortcode_atts(array(3 {. V, K2 J, A- [0 o
- 'posts' => 1,
3 F, G4 C+ @5 U/ O8 A3 F; H9 Z - ), $atts));8 a l8 z) V. q b
-
L. }. G! h. y. P5 ~* } - $return_string = '<ul>';" \9 P6 Z3 t- D' `* e5 o8 W, D" e$ |
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
5 T6 T2 I, n% S9 U. R3 @( G - if (have_posts()) :
* r* g. ]/ `+ x% {, s! n/ ? - while (have_posts()) : the_post();; t; U+ r$ s8 L4 j, U* j
- $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';/ t# _( M, z; \& Y B# s9 I
- endwhile;( o. h L. p5 _! ^$ U& L' s
- endif;
, ~% s( b; Z+ d- l+ ` - $return_string .= '</ul>';
}! V7 C L% A: g' A4 L3 v -
; Q \2 ?' o @; C- O - wp_reset_query();' R# a- K. {7 A8 F) J) G* u
- return $return_string;
" r' m- [- P3 H0 I0 s - }
銴鋆賭誨蝣 憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :' Z1 T5 [# w$ w4 E/ X
2.2 凋誨蝣潔葉憓摰 臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗 - function recent_posts_function($atts, $content = null) {% K7 u( Q0 ~: S
- extract(shortcode_atts(array(* p* _; M0 g2 x" s7 [! s
- 'posts' => 1,
4 Y7 D+ s4 E" A - ), $atts));0 m/ P; X6 {$ S3 D, u1 @
-
5 b6 Y# t7 U8 ?) |7 V7 L1 H - $return_string = '<h3>'.$content.'</h3>';4 q6 O# T! k! {- h+ B. v
- $return_string .= '<ul>';( D" x8 ^- `# e: M
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));9 X/ y! o/ H5 U) z* g: j
- if (have_posts()) :* [' X) k+ C, G4 a0 `1 @1 O
- while (have_posts()) : the_post();
# @9 M) g( `/ e# K5 Z C2 C - $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
* ~' z) f% [9 \( n/ [ - endwhile;+ N7 K2 ]5 N5 ^
- endif;
* u: n% R5 n8 M - $return_string .= '</ul>';
" C; }* j/ J4 n: |0 l. g - 9 [+ L2 A `% l$ I( n
- wp_reset_query(); G' p# L* w8 \
- return $return_string;% ~0 t4 E0 ^6 j6 c$ E- f8 ~
- }
銴鋆賭誨蝣 蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝- [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣 支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗* x8 V# `! u7 k6 @
3. 其遙雿唳寥*蝷箇凋誨蝣 3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣 閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛嚗典臭誑撠甇斤撘蝣潭曉典撌亙
- D/ Z' o) B5 |0 x臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 : - add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣 曉其閬靽格嫣遙雿嗡唳 , 瑟, 湧甈撠甇蝣箏圈*蝷 8 O; r. F% x5 W; M
璅嚗刻隢銝剖函凋誨蝣潘 - add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣 冽閬銝剖函凋誨蝣潘- add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣 4. TinyMCE蝺刻摩典凋誨蝣潭& z) v! {, j' f
, e' p& \9 J' ~7 y# N+ p$ l! A
嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗 - 箸迨撱箇銝 JS 瑼獢
- 閮餃JS隞8 @: e4 Z( c0 [- v2 [% `
: T' M/ m# q. s3 C; x ]4.1 JS隞嗡撱箇JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗 - (function() {5 J8 S+ B! x9 H( \4 b5 H6 ^& V
- tinymce.create('tinymce.plugins.recentposts', {
" W0 [- I- E. w: T! r - init : function(ed, url) {* Q3 _) z( V( S9 E* S, m
- ed.addButton('recentposts', {) S3 s N( A" m0 j
- title : 'Recent posts',# V' x3 k0 O: E9 z
- image : url+'/recentpostsbutton.png',* F5 A, H# @: }+ O
- onclick : function() {7 w5 I. P7 \* o
- var posts = prompt("蝡賊", "1");5 H1 B: d! U& S# l
- var text = prompt("隢憛怠亙銵冽憿", "唳蝡");
* V# y6 r8 \& Y2 ~9 S - 9 Z3 _0 O; p& o$ M, ^
- if (text != null && text != ''){8 X2 j8 g5 g6 ?1 i
- if (posts != null && posts != '')' X, s$ r7 J8 G; a
- ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');: l2 h. `9 K: N7 h1 @5 P3 B
- else
' P7 ~1 g: w9 f! ^0 e - ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]'); T$ v# V% {/ o+ [0 t
- }; ^. e+ r- ?, U1 S
- else{" I: X- [4 v+ y' p* k S
- if (posts != null && posts != '')
' J& T. r+ a2 r5 x: e - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');
4 o. |* y: \4 d7 K5 k - else
1 f' \+ w, @/ D& Z3 \- d - ed.execCommand('mceInsertContent', false, '[recent-posts]');" t$ {! A2 p4 I1 k4 N! Q# _ ?
- }
6 h. p8 x- n6 z# d% v8 U! ? - }
# u/ ]* H; z7 i: @3 b - });6 y. N1 \' W h9 f
- },) J$ V9 X+ q2 ^) s! O
- createControl : function(n, cm) {( @3 \' d! p$ u
- return null;0 s u, G/ M1 _( }
- },
2 D- l- i' Q, k - getInfo : function() {
r1 [. o( `; D" v4 X - return {! @5 `: o, o9 x) E
- longname : "Recent Posts",
4 t& h8 Q4 d+ n) v; J( v! g - author : 'Specs',
- d8 R- s* _1 K2 w* G# h5 n - authorurl : 'http://9iphp.com',
4 l, T8 T' a# `; c& `2 ] - infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',; q" s3 _# t, V# B# M/ a: ?4 u" ~
- version : "1.0"
2 U/ [* U: `' n3 w. t7 T& h - };
M% u7 d, `, U/ o! h% B0 T - }$ t- f9 @' s; z! @8 W9 j
- });% {6 Q: Q: d/ C6 R/ l( f
- tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);+ E- i4 ]7 Q" U8 |, J" `! J
- })();
銴鋆賭誨蝣 澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨 onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉 敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准 : N/ X' h6 g- d1 g z4 s
4.2 閮餃TinyMCE隞* T/ D7 k; T8 \$ V2 [ Y
$ f% H4 e: |/ _+ r' @3 C0 b 嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉 蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩 - function register_button( $buttons ) {& `! |# r# y* [; E, B$ x T
- array_push( $buttons, "|", "recentposts" );
5 Y' ?' p- l+ i3 R - return $buttons;
4 r. x9 z4 L4 v$ M - }
銴鋆賭誨蝣 蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅
" r" v* J+ A0 V) ~3 A0 V- function add_plugin( $plugin_array ) {$ _3 \, `) T# I; o- Q
- $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';1 T% d7 K% N- k/ b- H- u5 f% {* a8 H
- return $plugin_array;
8 Q8 ~7 T, H, F/ F2 y - }
銴鋆賭誨蝣 銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁
7 [& V/ j9 e9 _- function my_recent_posts_button() {
$ Z7 E& u! j K. U P - if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {4 q8 R2 }( D& v H9 s9 v
- return;
1 |& f, E% g. i7 d" Y$ }& ? - }% z. Q* b! k/ O: I$ B
- ; K* H& C: R# ?+ n
- if ( get_user_option('rich_editing') == 'true' ) {
3 y8 R/ u4 W7 J3 R0 e- r/ u( n l - add_filter( 'mce_external_plugins', 'add_plugin' );
% }' A- B' x# Z- ?" k2 G - add_filter( 'mce_buttons', 'register_button' );
: \& W) K8 }6 i7 D0 I! f( Q+ A, o - } 1 j1 x1 j) f3 ^# C
- }
銴鋆賭誨蝣 嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁
' _4 ]) n: n- Q! ]' n1 r敺賣詨曉訖ordpress憪賣- add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣
* w; W4 p8 ]3 F2 l5 n7 u4.3 冽/ v& K# u9 D, p. |$ Z
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
& `6 h! X* H* D; k- D) [
暺嚗敶箏閰望霈頛詨交蝡賊嚗
3 y# T1 K j% R5 Q) H# i3 a2 r' L
頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
& ?( D$ z6 I% Z. Z7 G2 ~
貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
4 k( a! u! ^5 l; N* A2 T" Z. H$ m; ~ 唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆 |
|