WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)
2 ?1 W; Y: z0 }, Z" ~ a3 M/ b: |% t* r6 H' i3 A
wordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐6 C2 e4 W: F& c& [3 f; |! u
隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具
. s M- u! h) h0 O: m+ |* O1 y2 Q2 B6 v4 ~" ~$ J$ P& O+ q
凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :
2 K# @; J6 G6 z7 d+ O' {! H! U( a甇文 , 臭誑閮剖銝訾批嗆蝡賊 :# o0 f# a+ h! K9 r
甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :/ t8 s! H2 F: v0 ~4 J- `. Z. c
- [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣
7 V3 O ^6 y) E9 e
$ Q' M$ v" P5 X( |$ T1. 蝪∪桃凋誨蝣
) G0 F5 g" x# A& q, k$ s( R祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :
7 i+ E0 l' T9 I$ ?' W( x閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :
( o7 L/ M2 _$ u: C7 O! t- ~' z- 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
- 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
- 閮餃賣賊蝯針ordpressaction銝0 u5 _- o. `9 E+ s
1 L+ \5 e0 M1 g e祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准. O, Y7 c: ]# s* K
1.1 撱箇餈賣7 f: M4 G/ n$ W1 J# @ k9 f) q! @
嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡
& K% E- k* w& q" C6 t$ R6 i: W- function recent_posts_function() {
/ v3 |7 o+ ~$ s3 H+ @+ C - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));8 `& i, P9 q6 [ ~$ q$ p( ]8 U7 c
- if (have_posts()) :: I: b- x* C. G+ ?9 h
- while (have_posts()) : the_post();
' E& \! y4 T- ]0 Y& n - $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';' ~1 `6 {& _# D, C' X
- endwhile;
4 r3 v: l5 s! B/ }9 o$ t" L - endif;
S* U+ M2 N: ]! I' p1 p" U - wp_reset_query(); x7 J' w5 z3 M, ^ B# X: ^
- return $return_string;% k# @4 |1 C* z) k: Y' i9 r
- }
銴鋆賭誨蝣 憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯
0 s8 [2 c1 u* O( Q0 j9 z4 E: k4 S# e2 s, D0 C
, [" Z: j* |+ a1.2 閮餃凋誨蝣7 w9 W2 N+ t; t' w; J& I# B) d
曉剁閮幃ordpress賣豢臭凋誨蝣潘- function register_shortcodes(){2 a0 d8 E" w9 B7 R
- add_shortcode('recent-posts', 'recent_posts_function');
) [' h2 k; g8 |( M - }
銴鋆賭誨蝣 嗅冽蝡批捆銝凋蝙函凋誨蝣, i0 v: c% y6 W/ T( s$ N
撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴
8 A2 @7 i# h+ P3 i% i4 _+ k7 P) Y8 h W: C- `2 c5 f7 A" n
+ E5 c ~9 z8 x! i9 l/ r7 S1.3 Hook into WordPress: H/ Q) q3 N! a$ d% j
3 n- b7 {$ X8 f/ ~" I
箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准
& Y' |6 r" ?6 G1 g4 B- add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣 ! f1 \! z) b8 O7 \, G, m% h* j
1.4 皜祈岫凋誨蝣# V/ Q/ [# W2 O
蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :2. 脤凋誨蝣 2.1 凋誨蝣澆 凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻 閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼 撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 : - function recent_posts_function($atts){
2 x" u/ ?3 u5 ] S$ U - extract(shortcode_atts(array(
7 t7 W0 }- ]( i8 |9 f9 x- ^, D - 'posts' => 1,7 @" B$ i+ X8 w1 t9 Z' L
- ), $atts));
4 B; V3 _' M6 U - 3 v/ q% w3 s. K2 W1 G; M0 c
- $return_string = '<ul>';
7 L+ ]( c$ ~' b* z# `& W q - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
7 A3 Z% t3 \9 ]/ `- w+ A - if (have_posts()) :9 F; k8 v+ D0 v) V' C
- while (have_posts()) : the_post();6 ?+ e$ D+ q3 S+ h! s' Q
- $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
% R% g& J' o; O - endwhile;
- V/ X" o; d2 N: i* P+ |7 G - endif;
! D+ o: i) K( B7 H# X - $return_string .= '</ul>';
, P" c6 a% v k; Z - + R% `2 R/ D2 h
- wp_reset_query();8 `( a+ n/ B# l0 u& u- ~; g
- return $return_string;
2 S& ~% i2 M7 S N9 |% ~5 L' B - }
銴鋆賭誨蝣 憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :- D; S0 C% {7 }% E
2.2 凋誨蝣潔葉憓摰 臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗 - function recent_posts_function($atts, $content = null) {: k7 l" L' }- n
- extract(shortcode_atts(array(* O/ y1 r0 a2 {( ^# ?" U
- 'posts' => 1,
% l, R1 Q, L. P. o8 A, ?7 a; w - ), $atts));$ J E9 H' x6 \8 h
-
6 B( Q" e: V$ m/ t) K - $return_string = '<h3>'.$content.'</h3>';! `3 c4 q0 C. f2 p
- $return_string .= '<ul>';
. C/ X) U4 y- C - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));7 g( ^' E& l/ J) n2 N% X R# o, J. ?
- if (have_posts()) :+ d/ H& w2 K& Z( p2 z6 q1 y" b. w1 I
- while (have_posts()) : the_post();
" ?5 ]( {% q0 w - $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';( M* @) A3 I! N/ J8 a
- endwhile;
) _* N" E2 W6 g4 n+ a! [ - endif;
$ ^9 L2 O1 ]' P" h6 t - $return_string .= '</ul>';
3 H2 d3 a ], A( C -
! k3 R- l$ y* P2 P* ^7 t - wp_reset_query();4 \% K. {! x: E: \' H7 g3 c* g u
- return $return_string;
4 ]7 u P# k7 n3 }+ y& z8 \8 t+ [ - }
銴鋆賭誨蝣 蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝- [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣 支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗3 z2 f* [2 ^- T$ N3 {! E3 m, r) W
3. 其遙雿唳寥*蝷箇凋誨蝣 3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣 閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛嚗典臭誑撠甇斤撘蝣潭曉典撌亙 5 x- {5 a1 K5 y2 ~$ Q
臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 : - add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣 曉其閬靽格嫣遙雿嗡唳 , 瑟, 湧甈撠甇蝣箏圈*蝷
1 W, R: r) F) c9 O2 @/ [/ ? T璅嚗刻隢銝剖函凋誨蝣潘 - add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣 冽閬銝剖函凋誨蝣潘- add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣 4. TinyMCE蝺刻摩典凋誨蝣潭 w. q; ^+ k; s5 ]& }* ~* }5 O9 c7 t
. O2 N0 F& x6 P3 I0 H* e+ q; S, H嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗 - 箸迨撱箇銝 JS 瑼獢
- 閮餃JS隞
) f! m2 n& u$ U
% q w F5 G- p# R: e4.1 JS隞嗡撱箇JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗 - (function() {
& v1 X1 Q- F7 b! G- ]$ v* R - tinymce.create('tinymce.plugins.recentposts', {
( {+ {9 E O8 n) k0 H5 F - init : function(ed, url) {# W f+ @9 _( b
- ed.addButton('recentposts', {
& V4 C" U8 b. F - title : 'Recent posts',
# H) k- ^ c" m5 `# N* f - image : url+'/recentpostsbutton.png',7 q: r6 m" `/ ^+ J5 G
- onclick : function() {+ k6 C. j2 { I4 G: O. {
- var posts = prompt("蝡賊", "1");" N6 D/ \) N7 `* t, d1 X1 w: y$ O
- var text = prompt("隢憛怠亙銵冽憿", "唳蝡");
7 A7 Q% c- j1 |/ I2 E -
% r# \4 e8 W3 u' F4 v - if (text != null && text != ''){4 C9 l7 C4 M [1 P, I* r
- if (posts != null && posts != ''); |& j3 \( W7 N/ @) v; l
- ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');
: `: w/ r; t/ n6 w+ {# k* m9 c - else
3 K$ i8 T2 [& o( @ - ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');
/ n( o1 ~1 z3 t0 `- ^# k; a) X% b - }+ h+ q4 o: L4 a( |0 ^6 z$ n2 {- j2 G
- else{- `) O" o+ _/ ~) o; V
- if (posts != null && posts != '')
! t Q0 i q! {% M! G2 y; z2 ] - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');( @: C8 K# v- h$ N$ f# U
- else
0 C& Z6 J" U! l8 g( h/ y - ed.execCommand('mceInsertContent', false, '[recent-posts]');
7 H2 E4 C, U( }" X: t8 ?- f/ n - }
0 f; X2 Q% e5 X# n+ d - }) }# X; \4 [7 ]& f. f2 ?$ s4 A% d6 P9 o) t
- });9 ]; D$ r! p. [7 D" w6 }4 I0 P- ^! q
- },2 r; H5 [2 v1 {7 V( O# h2 k' q
- createControl : function(n, cm) {
7 c& B" N2 I( z5 _/ K - return null;
. s( q! z( X' p; s8 k: a! D2 R! L - },2 A3 |- p7 [+ d
- getInfo : function() {
5 A, t3 c8 ]# B5 t/ h - return {
' H2 U7 B/ I. H9 M2 P X - longname : "Recent Posts",
6 T% G' d! r7 n( U7 ~2 [8 o - author : 'Specs'," z. W. B1 S4 {# u
- authorurl : 'http://9iphp.com',
! h. o3 z7 f- k" W - infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',1 ?) v9 O5 o- H5 O) B, Z, P
- version : "1.0"
6 b8 y: o0 }, B" D* Y - };
* ?- A/ A3 E* k1 p1 z4 \8 i - }$ V9 v% D3 a4 g2 N
- });; ^1 G2 T4 e9 N: I- d7 q
- tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);4 d% T$ L/ j) s5 A% z
- })();
銴鋆賭誨蝣 澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨 onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉 敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准 8 H3 z- H0 z; z& s
4.2 閮餃TinyMCE隞0 V2 {, h0 o- {, ?( ]! E! `) ?. U
" y, R7 s& |9 e: U7 a
嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉 蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩 - function register_button( $buttons ) {
' U9 s2 y; d, ]. m - array_push( $buttons, "|", "recentposts" );
4 D( W) b8 G, |0 j - return $buttons;5 v4 ]& z. o9 F' m& @
- }
銴鋆賭誨蝣 蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅
$ p$ d% y" z: b' F4 R; V8 O e: I- function add_plugin( $plugin_array ) {( ?: ?* \! U- f7 J: @' Y7 M& X
- $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js'; P+ d- P# D% s
- return $plugin_array;
4 s2 ^5 t1 m: C( S( I# j* o* ? - }
銴鋆賭誨蝣 銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁
8 J- l0 o" u- z, e0 l/ }( e- function my_recent_posts_button() { ; b6 f' w+ y) n; U
- if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {
0 _( v; Q% I* N* f; X( H5 E# b - return;
' G7 r1 }6 W/ z7 a$ U - }
$ b# b/ e% H! p& \: g0 i, E3 S -
. i' f5 n& P0 t2 b6 x - if ( get_user_option('rich_editing') == 'true' ) {
5 K3 N" ^( v. u6 w7 V9 [! s - add_filter( 'mce_external_plugins', 'add_plugin' );3 Y0 ?# `1 E' [4 ]1 Y9 }: u
- add_filter( 'mce_buttons', 'register_button' );6 g4 N& l5 C5 Q/ ^. f
- }
0 n: v4 z+ H1 B' X- w4 `9 i) v& G - }
銴鋆賭誨蝣 嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁
1 v" V# E+ z9 q1 u+ U敺賣詨曉訖ordpress憪賣- add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣 . y' T: m$ k/ r9 w3 D, }* j
4.3 冽6 ]0 P$ s( s) U8 q$ h" f
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
2 v( O. L* |$ V" t- f+ I 暺嚗敶箏閰望霈頛詨交蝡賊嚗
# N9 h1 y/ M( B8 F# N! j
頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
. j! F# Z: ~) M9 T3 z3 I% d9 D( ~0 }
貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
+ A& l. {6 V, B% m
唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆 |
|