WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)
' D, l' e8 D4 O: o% a. Q) n5 I7 d" [ f( g0 Y' H0 G7 Q* |* U( T
wordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐% i, D- Y" w1 |- \) l8 z6 _- y
隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具
6 c& Y( M0 A/ Q# k3 [8 \4 n4 z1 ?) S( u+ o
凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :
( o& J. V2 Y8 `2 S9 w甇文 , 臭誑閮剖銝訾批嗆蝡賊 :
8 U2 d( y/ B9 A9 _2 R* d, L! l甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :
6 _# r1 ?2 T0 h) V. ~- [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣 3 N# z0 V& |8 u
2 b' m8 ~% g/ K3 B5 q
1. 蝪∪桃凋誨蝣
5 z3 h) B3 \8 I) ]% U2 o2 @祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :
! f9 E+ U( V) w5 c8 ^: X: Z閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :
: y s" V# V" @: {, Q- 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
- 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
- 閮餃賣賊蝯針ordpressaction銝) | L( a* a' @' ~* F1 ~: @1 ]! i
+ S! O8 s4 w5 T) ^. y T
祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准% \) ]+ p0 v" x, F, f' F" k
1.1 撱箇餈賣; E* Y, P; }5 M7 H
嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡8 ~; U0 _) ]6 W2 |0 ~7 Z2 z. @
- function recent_posts_function() {
7 D$ m3 E0 o, A3 G! N+ b& J8 j - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));/ k8 G+ K" h9 G- B' G
- if (have_posts()) : @$ E* N7 O8 l% R: M+ A; |* S
- while (have_posts()) : the_post();
7 [) S+ {5 o$ N0 p6 V - $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';
9 l7 {/ N [ s; d - endwhile;
/ [4 C% p s7 e# o2 w/ J - endif;
3 L! c& f ]$ R+ i m+ k - wp_reset_query();
$ Y9 M" I) U' {% t0 @9 w - return $return_string;& L" y. B6 d( a( j
- }
銴鋆賭誨蝣 憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯- Q8 ~7 k7 \! @& S
0 W1 s7 {* N p4 q# b F
6 E8 s8 x% N& R7 e4 D4 C/ k$ `
1.2 閮餃凋誨蝣
5 G9 \% C- o; N$ k" O曉剁閮幃ordpress賣豢臭凋誨蝣潘- function register_shortcodes(){1 q! i0 N$ T/ ]& }3 G/ a) ?. [
- add_shortcode('recent-posts', 'recent_posts_function');
/ c3 e0 C- i [$ a( m3 a- _ - }
銴鋆賭誨蝣 嗅冽蝡批捆銝凋蝙函凋誨蝣* y. k- g3 Z3 S0 |) F0 |* D
撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴
. E5 \5 t1 b: y _1 }( M& ]* u8 W- C; l
1 e5 r, g. v. `& o8 s% W1.3 Hook into WordPress& Y7 l9 \! x% V" m' I
9 L L7 j0 l& z5 `9 k' s4 b, A) H& J
箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准
4 j5 U4 N4 p; B: g3 w/ b F' o3 R- O- add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣
6 k2 ?4 _' N3 e a1.4 皜祈岫凋誨蝣$ Y1 P" K+ C4 B( U
蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :2. 脤凋誨蝣 2.1 凋誨蝣澆 凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻 閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼 撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 : - function recent_posts_function($atts){
; e+ p$ ^& I* n, e- u$ A - extract(shortcode_atts(array(' f! l% N+ B2 p9 f( \
- 'posts' => 1,
# s9 J* r1 z4 ~# _ - ), $atts));
: t/ y1 A4 d$ ]% y -
" b. e; Z3 C- Q* r' P - $return_string = '<ul>';
1 Q0 M% V* K- t) e, k9 v - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
& r3 G2 H# A, M3 i5 Q- `7 D - if (have_posts()) :1 f0 o E T9 Z
- while (have_posts()) : the_post();! w. ]# G l. ~' W) Q- y) `
- $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
5 t9 n1 I' I: A$ b1 j/ z - endwhile;: _% A" F1 G" M# B# ^) G
- endif;5 I. `7 c3 `+ R* d- j$ _9 q$ `( B
- $return_string .= '</ul>';
V5 i1 A. c$ G+ S& b -
" _( |0 F4 C8 n" M1 D2 C, T0 x& q - wp_reset_query();% S9 T% f1 k0 \" M }
- return $return_string;- I( e; n4 H+ A, q. h1 T5 V
- }
銴鋆賭誨蝣 憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :8 s$ [( s! y5 z
2.2 凋誨蝣潔葉憓摰 臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗 - function recent_posts_function($atts, $content = null) {! ~* C/ S2 V2 m7 Q* W7 i9 A
- extract(shortcode_atts(array(7 w6 ?1 ]( }8 C/ g3 {
- 'posts' => 1, o, }# h' T* R! t2 ?) }# a/ V. e
- ), $atts));: T: u# D8 G, U" K: J, t% F$ [9 A" W' E
- 3 f" A; J D; ^% ], j: `
- $return_string = '<h3>'.$content.'</h3>';
& O) q! T2 V2 T$ l9 D b2 K - $return_string .= '<ul>';& M" L/ u1 {3 O7 ^& Z) X
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
2 h- d1 w; l; ` c0 Y - if (have_posts()) :
0 ]" n1 O! _: p3 `3 i4 K - while (have_posts()) : the_post();+ j" S6 q6 N% u+ V. v3 M/ m
- $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';3 J+ V/ q0 ^+ [2 k/ I# j0 L
- endwhile;
* u: U+ i+ v( x( p! l& {6 V1 ] - endif;! K" i; C V* W+ p& S6 P$ E' K( R
- $return_string .= '</ul>';" \/ t+ p$ ^) v* p4 m1 W, q0 E* j; m
-
& t ~3 y5 _: p/ D8 v3 a1 R+ [ - wp_reset_query();/ w& R% {1 B, ], N
- return $return_string;+ ~2 D4 w1 g: M
- }
銴鋆賭誨蝣 蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝- [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣 支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗
# r/ R% l, N0 i8 d3. 其遙雿唳寥*蝷箇凋誨蝣 3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣 閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛嚗典臭誑撠甇斤撘蝣潭曉典撌亙
: h& ^0 A- N( D臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 : - add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣 曉其閬靽格嫣遙雿嗡唳 , 瑟, 湧甈撠甇蝣箏圈*蝷 - G0 l9 M5 T$ ~- i- J
璅嚗刻隢銝剖函凋誨蝣潘 - add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣 冽閬銝剖函凋誨蝣潘- add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣 4. TinyMCE蝺刻摩典凋誨蝣潭6 S* b3 ~# a# E$ R9 S
) F! d* D( M C- u y) f嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗 - 箸迨撱箇銝 JS 瑼獢
- 閮餃JS隞
) M! \) W! `4 }, a/ R$ _7 O. Y
2 g. l% N1 s* j% ^, @4.1 JS隞嗡撱箇JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗 - (function() {
; P$ e6 ]/ w+ |8 r% V - tinymce.create('tinymce.plugins.recentposts', {
0 d0 Y+ K) C# ]0 a y" O - init : function(ed, url) {( |6 S& B% D7 q* n- f. Z5 ]! D
- ed.addButton('recentposts', {
; r& G# \& Q' ~8 N) Q - title : 'Recent posts',
. y3 g U/ J4 y7 [ - image : url+'/recentpostsbutton.png',
/ i1 }, F3 M$ Q( n% N% \ - onclick : function() {$ g" G, [3 X7 H, k* p
- var posts = prompt("蝡賊", "1");
" a }7 j) V9 \: r - var text = prompt("隢憛怠亙銵冽憿", "唳蝡");, ~( r- t! |; H) K# L( A! m
- n; j" E+ b$ Q2 P% S7 L
- if (text != null && text != ''){
# S) }* [# Q& Y - if (posts != null && posts != '')
0 C9 s) _' F. {9 Y" W9 Z. [; U - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');0 _3 W5 u# L6 {/ J8 h1 x% e$ J$ o0 |
- else9 f! e, b/ e4 P# r0 r1 F( X- o
- ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');
1 U/ r9 ?# `/ U6 b - }+ P! u: z: `' a. N7 s8 j3 d5 K
- else{$ T2 L! P* D4 c( s& @ J! a' L
- if (posts != null && posts != '')
( N$ k! W0 D h; E% _: Z - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');, }) h+ n o. e/ R! c1 K ^9 ]
- else. x8 G; S+ X2 h$ q+ j" E
- ed.execCommand('mceInsertContent', false, '[recent-posts]');
' C% H/ S3 m% J+ d% @ - }
X6 h, `/ f e/ @4 P, { - }
# M1 M9 x% J1 I7 [* {0 H5 ^; n - });
* _2 S3 G9 p1 j* B: V& n5 q( Y - },
* z- T ~3 p7 p, W) p - createControl : function(n, cm) {' G. E3 `2 m. q, w
- return null;, ?" J: {; U; M( N
- },
& P3 B4 A2 T( ~; H* I - getInfo : function() {
4 d) T- ^) i' I* S. Q - return {7 O) q9 {8 J+ C
- longname : "Recent Posts",
4 s+ _( ]* @9 J1 b - author : 'Specs',; a# a' W" }5 Y- m2 Y: ~
- authorurl : 'http://9iphp.com', _! A4 k2 C$ z' H8 ?, X' k
- infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',2 s8 r. k/ l1 G# u' h" E$ A% r
- version : "1.0": u1 j5 _0 Z6 K' b- r8 J
- };( b7 ]' h7 E1 v% i- a4 M
- }
$ e: R" G: t/ v4 w. G - });: B' S: _) U' K+ ^( a
- tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);
* p- {% z4 D4 N0 L1 h( ~ - })();
銴鋆賭誨蝣 澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨 onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉 敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准 & [2 u& i% }- @' X' G/ w
4.2 閮餃TinyMCE隞4 A3 q! N' |- v! e5 F, n4 p: b \
( z+ s1 Y4 k+ C1 e" z
嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉 蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩 - function register_button( $buttons ) {8 M& R+ U% E3 h* r; h: a% f+ A( T
- array_push( $buttons, "|", "recentposts" );
" H$ L1 Q# h! k, E1 d' X, Y7 { - return $buttons;5 d( a; g& g6 z
- }
銴鋆賭誨蝣 蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅
3 L8 G. Z+ g; C4 y9 o( J- c- J7 T; I7 i- function add_plugin( $plugin_array ) {
& ^. t k9 |$ A: L - $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';) D; a. [% T, I; z
- return $plugin_array;' C9 L# i# X6 @/ w
- }
銴鋆賭誨蝣 銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁; [' f0 A8 k: m/ q' Q2 [ K- k
- function my_recent_posts_button() { 8 F3 v/ U) W* b+ o7 z! L
- if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {
: ~2 E/ O8 z7 ? N/ [2 z! a" t6 G - return;
& q* j' D" \# X" e# h5 W - }, I& U2 y' i( d0 {2 l
-
/ Q. ^3 W1 {4 g P D/ h8 Z - if ( get_user_option('rich_editing') == 'true' ) {
' h7 @3 m, e2 ?0 B+ f' V' V - add_filter( 'mce_external_plugins', 'add_plugin' );# u6 E: f9 W. n3 D" f; C8 @
- add_filter( 'mce_buttons', 'register_button' );
; c' U7 x) Z } - } 0 j! S) c9 R. Z6 s! H! c. k
- }
銴鋆賭誨蝣 嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁
6 C4 i6 P' h1 o: z U敺賣詨曉訖ordpress憪賣- add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣 * e* ^7 f7 B) K, [3 G- I2 y& z/ W
4.3 冽% m: q; \0 t* K8 j3 a/ I
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
. {: [1 `- E6 s 暺嚗敶箏閰望霈頛詨交蝡賊嚗
( O; c6 Z/ }3 H8 F( X
頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
- s2 B& ?. p3 P+ y' N 貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
5 w7 r3 L+ ] F( D0 I; _ `+ |% Y4 T
唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆 |
|