WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)
: p& W3 Z/ ^5 P6 r n( Z+ c' H' D9 v8 P
wordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐
+ x- j3 i5 `% [# J& m$ ~0 V. O隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具
" K0 q! Z5 T2 A/ _3 g1 L; a
6 w6 i. T4 z9 K V3 V凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :
( U7 m. R2 h+ R0 F甇文 , 臭誑閮剖銝訾批嗆蝡賊 :
# g+ e, X* J9 g. j. f6 D8 x0 s& T甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :/ I% `9 j) J( l# i
- [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣
5 T( x$ N9 x& l/ d; k
% _) f( P# M3 s. ?/ s; t2 D" s$ i1. 蝪∪桃凋誨蝣
0 m* ?. ]' ]8 W$ M( U1 Z. P# h祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :" N* N( x* S: q# M/ a
閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :& r% ]7 L0 [! g" E7 U6 G4 K
- 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
- 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
- 閮餃賣賊蝯針ordpressaction銝. T; O; |- R! C* T
# X* l6 @( }+ U- i( I( T. K& c0 J
祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准& h/ e, _6 P% q
1.1 撱箇餈賣
+ |0 F$ ]( |% e, g2 i% X' a( ]% Y嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡% ^/ I! ]+ c, k% ?, C/ r. L/ c* t
- function recent_posts_function() {
/ e& M' D' h9 b9 b - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));" Z0 T; Y: q% H1 X8 N. ^. [
- if (have_posts()) :
) {/ U4 Q9 U; d9 l - while (have_posts()) : the_post();) X& t% j/ @ ]+ q2 y
- $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';8 w$ Z$ C8 P' J; b7 n, N
- endwhile;$ b K: W; |0 N& s- L. M) Y8 Y
- endif;6 d& W- P6 \$ N2 [6 S
- wp_reset_query();) r+ o$ M: u9 S/ H6 Z
- return $return_string;
/ A% Y. _/ R# ] - }
銴鋆賭誨蝣 憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯
0 b/ f3 X; l) o+ p% \3 i+ g! m/ u+ l+ ?( w4 J p
1 |# g8 ?% U; T0 x2 S
1.2 閮餃凋誨蝣5 b1 T5 V# M! R2 P
曉剁閮幃ordpress賣豢臭凋誨蝣潘- function register_shortcodes(){
6 K! F0 b: F E+ ^$ ?4 Q2 C! } - add_shortcode('recent-posts', 'recent_posts_function');/ [. J2 ^8 Q E$ J8 Y) D7 m7 B
- }
銴鋆賭誨蝣 嗅冽蝡批捆銝凋蝙函凋誨蝣
+ P- u0 o0 o3 a撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴
2 p3 a7 m1 b5 Z+ w3 Q5 v8 {
/ K" E- u9 `: t! N1 A7 M* g: N! K9 l' Y& }# h0 y
1.3 Hook into WordPress
2 D0 j( Y1 N8 B% S/ S/ Z/ S' y6 G% \. V. \
箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准3 j* _* L) P/ J7 X
- add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣
, ~' Y/ p0 p( [& }. @1.4 皜祈岫凋誨蝣# P: x0 Z1 O# U
蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :2. 脤凋誨蝣 2.1 凋誨蝣澆 凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻 閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼 撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 : - function recent_posts_function($atts){
# n$ R& f- f, e, v1 N' u - extract(shortcode_atts(array(
- j, ?4 U' |- d2 V - 'posts' => 1,+ o4 x6 ]/ Y; P/ I! J6 n
- ), $atts));
- b! o D2 h4 I# a9 ]& A, ]% V3 ] -
. N0 i9 M4 [ L. c4 V3 @ - $return_string = '<ul>';/ @! r3 d3 m" Q: \% h+ w
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
7 E" }$ M" m# d* W+ o$ N: x - if (have_posts()) :
6 V) @3 h) L2 F: w3 d - while (have_posts()) : the_post();
% ]6 W# W S6 u0 _ - $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
, V7 I% |4 i1 T8 v H& j5 O, B - endwhile;7 S) Y9 {- a2 _+ O8 y% v* `# j
- endif;2 N$ o$ Z( O3 y' D! c+ T9 }
- $return_string .= '</ul>';
$ r5 \) h( L9 @ D7 b" y! N - 6 x6 }0 `" q( }6 I8 s1 D5 Q
- wp_reset_query();
4 r0 U G* X X) D- k! C( J5 o - return $return_string;, L" I# E. q5 a9 ?
- }
銴鋆賭誨蝣 憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :7 r" G+ o; b& r& y0 s z5 L3 l
2.2 凋誨蝣潔葉憓摰 臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗 - function recent_posts_function($atts, $content = null) {# Z6 o8 e5 x9 d4 _; P# Q
- extract(shortcode_atts(array(
1 w4 ^$ B; U& g% _, s& j9 N1 V - 'posts' => 1,
/ ~+ A# |* `, ]& X8 v/ Z - ), $atts));; P6 o# A. F+ @3 P
-
% q4 a6 ~( P4 ]" A* A" D - $return_string = '<h3>'.$content.'</h3>';; l- X" Q) E0 W- c* z3 p
- $return_string .= '<ul>';
- |5 y' [% v4 q: X( t - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));7 T% d* a q/ `' H/ B
- if (have_posts()) :5 a1 F0 s/ z" R5 L/ U
- while (have_posts()) : the_post();
7 D- C! h5 ]2 d3 V- B; c; ?3 h - $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';+ a; W7 F' R! A
- endwhile;; n( T5 o: A$ @
- endif;
7 s* F9 e8 ]; r' t - $return_string .= '</ul>';$ t% u' F. e- _9 }. C- H* P
-
( m. m! G( w; J2 z7 k! [( R9 B5 l - wp_reset_query();
4 G A& ^- A$ G4 c - return $return_string;- \/ P! j; {9 Q2 v0 O v" V9 m- _( u a
- }
銴鋆賭誨蝣 蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝- [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣 支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗- q. H2 R0 U1 R
3. 其遙雿唳寥*蝷箇凋誨蝣 3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣 閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛嚗典臭誑撠甇斤撘蝣潭曉典撌亙 6 P3 ^& r- u+ x9 t
臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 : - add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣 曉其閬靽格嫣遙雿嗡唳 , 瑟, 湧甈撠甇蝣箏圈*蝷
3 g' b; z! N' G* w0 q% p% j璅嚗刻隢銝剖函凋誨蝣潘 - add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣 冽閬銝剖函凋誨蝣潘- add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣 4. TinyMCE蝺刻摩典凋誨蝣潭. a- |6 p9 U/ d
, @- G7 Z! Y9 Q0 H+ |+ t
嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗 - 箸迨撱箇銝 JS 瑼獢
- 閮餃JS隞( k4 Z- A5 C# ]4 c3 k: C9 `
$ x' t* R* l. ]; O1 M" b
4.1 JS隞嗡撱箇JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗 - (function() {4 C1 R% x3 B7 M5 Q
- tinymce.create('tinymce.plugins.recentposts', {
/ x4 E/ t8 Q" k& m* c4 k) d1 l - init : function(ed, url) {
. Q$ t/ D, k- u% v" c$ ~ - ed.addButton('recentposts', {
7 P* z% j- V! G" X, d9 I$ X - title : 'Recent posts',; t7 N2 u4 a, c
- image : url+'/recentpostsbutton.png',
( ?( r' E4 i0 c! c/ h - onclick : function() {- h" s' z% ~8 k$ M$ \& R' m
- var posts = prompt("蝡賊", "1");( T2 T5 q+ X. b
- var text = prompt("隢憛怠亙銵冽憿", "唳蝡");+ F% |( g; U/ z( S) u" C) a5 Y
-
: Z% c) P1 b( X' [4 P& i( E3 D$ a - if (text != null && text != ''){
3 W1 s% @! d* t - if (posts != null && posts != '')
% Y, }1 m% }! G% G7 K3 X& L$ D2 g5 K a; z - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');) U* I$ B# W2 p8 N6 j3 b- @
- else* b! @# N$ ]) W, m6 O* t0 t
- ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');0 P4 J! }0 ]( t' m, z
- }
4 t. x0 _# I Y" P/ M( ? - else{1 W/ I! \1 ]. b4 q! a
- if (posts != null && posts != '')+ \3 U/ _. s7 X% V A, q1 \' C5 c
- ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');8 a$ U) a; s; c3 r
- else: f3 D" U, ]% B
- ed.execCommand('mceInsertContent', false, '[recent-posts]');7 e- d/ @5 p- V8 ]
- }
! W% q2 d# b4 q! [; \ - }
. ^* V2 a2 C; |) g9 T8 a3 N) G - });- w, x. f" ?# i
- },
1 N$ @4 T( }# j8 K8 w( \ - createControl : function(n, cm) {& L' w9 r) m2 O+ H, v
- return null;/ ~7 ?3 g6 v) M1 x5 ]+ R/ k
- },' e1 X9 h0 M3 {* Q! p l2 k
- getInfo : function() {% q; N& N0 k# ?/ C. b
- return {
1 r" y, s" [4 G5 G T - longname : "Recent Posts",
% B* o9 `6 \/ k! d - author : 'Specs',2 J8 g f0 H! q; p. E! m8 [ K5 w
- authorurl : 'http://9iphp.com',
0 F* U9 l" s) Z( p3 ^ - infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',7 M' U( u1 x# ^2 C: m
- version : "1.0"
. I1 e9 C5 H& Q# S% Z; S; V - };
8 q% \$ M; N- r" ^8 I1 o - }
) @0 k/ `) i, g" @: b4 h/ _ - });
' j# e! R, Z0 B6 F* r - tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);/ e1 p) y( \& r5 c8 P
- })();
銴鋆賭誨蝣 澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨 onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉 敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准
1 \9 P" _4 A( h1 n/ g
4.2 閮餃TinyMCE隞
, w7 s2 K b( z' U
* E' @- f' _- k$ C* i& i+ Z 嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉 蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩 - function register_button( $buttons ) {$ a! r" Y4 b* K0 F/ I% T& S
- array_push( $buttons, "|", "recentposts" );
" v" C8 A$ n' y0 ~- I( |: ` - return $buttons;
4 }" u3 F) z5 W- O3 G3 | - }
銴鋆賭誨蝣 蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅9 ~$ A \1 s8 a5 `9 w, H' P
- function add_plugin( $plugin_array ) {
0 p* k. ^: y7 S. @ - $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';
4 [: G4 k# D) ^: r( w; W; t+ t - return $plugin_array;6 u7 k( m0 _) h0 u( B" l
- }
銴鋆賭誨蝣 銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁
1 {$ d) p4 ~* k# r% ?1 W G h- function my_recent_posts_button() {
" r* n/ Z G9 E" I. W9 o - if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {/ L( M4 v' `% d' Q
- return;
" J6 F5 B; {! F8 L. Y - }
5 V% O, a! N- u - - a( @4 q6 Z8 \" W
- if ( get_user_option('rich_editing') == 'true' ) {! d. i, D" x5 x) p, P3 \% q8 [
- add_filter( 'mce_external_plugins', 'add_plugin' );
# [5 g% z) |. `6 X9 k$ k - add_filter( 'mce_buttons', 'register_button' );; L! a4 L! {0 t* J" b( z* F+ C) n
- }
+ F7 T! ]! v& i' W" b, q - }
銴鋆賭誨蝣 嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁
8 \& c$ Z8 V# M* I" L" j敺賣詨曉訖ordpress憪賣- add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣
- U" C1 F$ x$ u; m% j5 `! m" E8 N4.3 冽
- L( x3 S. N5 }9 C
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
4 O3 x4 _; E; r. w+ W3 Z ~ 暺嚗敶箏閰望霈頛詨交蝡賊嚗
t S: k+ r' _' x- I/ s) _. } 頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
& _3 f0 r$ t8 d- i
貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
\# x/ V+ _, f, u7 `; D 唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆 |
|