WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)) v9 V( x2 K7 K% e1 b. _& P
7 j7 }! h4 }) X4 u* T5 N2 L) Rwordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐
/ J5 M* s2 ]4 r, Q( q, j! D隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具
; {3 k3 f/ V1 b% }0 h- M: D7 ^8 }* r9 C% i1 b4 a. x( r; x* w2 V
凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :
3 z3 x6 G2 j& b' \8 Q! m( @甇文 , 臭誑閮剖銝訾批嗆蝡賊 :
# w8 F7 C; D/ j' u' }& H甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :; q1 Z, f0 o# }& L' ]
- [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣
% u' D2 e1 c# T0 F0 M. {; Q1 O* E9 E: b
1. 蝪∪桃凋誨蝣: `, p! q5 T1 ]
祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :
: _+ `/ u: A! N9 w0 E閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :" ^4 H3 h! y7 |9 l( v0 b' _
- 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
- 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
- 閮餃賣賊蝯針ordpressaction銝# C) J$ |% B- T! u t
3 c8 Q$ ]. @1 X7 N9 b/ d& |祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准
: t0 S6 w7 Y. |3 R; E& N. v2 n4 d7 X1.1 撱箇餈賣4 N2 ? r4 W6 N6 ~2 R$ R" e
嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡( \, N2 H- D2 o0 h$ T+ f
- function recent_posts_function() {
, o2 t- w: Y' w% d4 a - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));
3 @5 k, z# I/ p2 I: @# U$ W4 M - if (have_posts()) :; q @3 a9 e+ P, k2 h
- while (have_posts()) : the_post();0 h X$ n, c+ d H4 v r+ E. E
- $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';7 I$ \* y5 O& F6 ?3 l
- endwhile;8 S" k" Z( ?. D% g/ L' M* B9 V& {
- endif;
6 [# z3 y& A0 j2 l - wp_reset_query();
4 P$ l3 L* k9 d4 y# @ - return $return_string;% g- u- y+ Y2 m6 `
- }
銴鋆賭誨蝣 憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯
) f" l1 w+ F8 q' Y$ n' g, ]0 A! a" ^1 Y. [1 F( E9 |
! f, `1 z6 x6 D1.2 閮餃凋誨蝣( U+ @& y, a# w" ~- J
曉剁閮幃ordpress賣豢臭凋誨蝣潘- function register_shortcodes(){9 M) c2 w# |' B3 V/ X. C
- add_shortcode('recent-posts', 'recent_posts_function');
. i& e& B5 J' q1 J" o8 Q- E$ e _ - }
銴鋆賭誨蝣 嗅冽蝡批捆銝凋蝙函凋誨蝣. o# |, I. V3 K! u& y( i
撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴
# a* p! X# E3 |5 b4 ^
, H$ {) E, G; p! P; j2 L& r8 k5 J% p7 u5 L/ w+ z1 I( H; J2 [) i
1.3 Hook into WordPress
: a) K/ f% a5 P L
: O+ F% A/ i G8 l( Y/ I箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准
$ ~: N3 t, y. d. H$ C. g- add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣 8 }5 A' U# N' \' |
1.4 皜祈岫凋誨蝣* X v7 g9 W# d
蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :2. 脤凋誨蝣 2.1 凋誨蝣澆 凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻 閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼 撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 : - function recent_posts_function($atts){& O u% l! J* c0 `0 c
- extract(shortcode_atts(array(
2 [' w3 L# n& j0 M3 m, M8 Y - 'posts' => 1,
% V% V |! w2 a! M7 A1 t- m3 r - ), $atts));: q) h2 f/ b# e6 \/ O. R
- - i* Z) s, Z* O! C( G t; S: N
- $return_string = '<ul>';
4 y+ Z# Y& H1 u, X( X - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));; n4 y6 n! \; y8 a7 O7 x) S
- if (have_posts()) :) I6 W$ ~' x; ~( e- [0 V) y
- while (have_posts()) : the_post();
2 v! k. D9 M$ Z6 l; b - $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';' W/ a* v/ g+ A8 x4 S
- endwhile;. a8 F0 Y9 f+ k8 r
- endif; ]: a! \; c6 J! g+ \7 N
- $return_string .= '</ul>';9 w5 u8 o. v: y* \4 ~. P; n
- $ g2 b L3 N2 ^ Z7 J+ F
- wp_reset_query();
8 r2 G8 ^) i- k7 ?/ W - return $return_string;
" z5 i4 M# F! \1 M- r% X - }
銴鋆賭誨蝣 憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :
( J- r' c2 X0 o6 G+ T* ?2.2 凋誨蝣潔葉憓摰
臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗 - function recent_posts_function($atts, $content = null) {
8 g" S2 t* a9 S: X8 a1 x3 o - extract(shortcode_atts(array(
8 }. E3 k( I/ i - 'posts' => 1,
, i4 ` u$ E; q4 y - ), $atts));, P7 g" }, j$ K% ~8 X- f
- h. X9 g5 F% p& m
- $return_string = '<h3>'.$content.'</h3>';
7 [1 y5 Z _9 `) Z - $return_string .= '<ul>';
) \, {. [* d8 e; f9 d - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));% x6 n) P& i4 x; J1 _4 w
- if (have_posts()) :
- A0 ~, Y7 O- y& F4 Q) H6 C: ] - while (have_posts()) : the_post();
! Q) Y7 W$ F7 w - $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';' L0 E0 f/ S" Z0 J/ g7 j
- endwhile;
2 ~8 N! c& |9 e. c7 \0 E$ ]$ T" U3 I - endif;
2 h9 I; X' `8 P - $return_string .= '</ul>';0 J$ q9 J6 L c* I; Y
- 6 w2 y% i' _# W# ~" n
- wp_reset_query();& ?( B# o. d' d
- return $return_string;, M5 _ W: ?0 E* {+ J0 ~
- }
銴鋆賭誨蝣 蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝- [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣 支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗+ u9 Q. _( w1 D2 {6 B. b0 n3 w1 }
3. 其遙雿唳寥*蝷箇凋誨蝣 3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣 閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛嚗典臭誑撠甇斤撘蝣潭曉典撌亙
" x) C7 G! u' l臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 : - add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣 曉其閬靽格嫣遙雿嗡唳 , 瑟, 湧甈撠甇蝣箏圈*蝷 ) L9 P# J$ a* Z" T
璅嚗刻隢銝剖函凋誨蝣潘 - add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣 冽閬銝剖函凋誨蝣潘- add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣 4. TinyMCE蝺刻摩典凋誨蝣潭
1 a4 }) s c8 r: \# D0 a, C2 U0 a& m, Q& E: D2 r
嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗 - 箸迨撱箇銝 JS 瑼獢
- 閮餃JS隞
0 Y* x& V3 d6 u6 r# X / x, d' n7 d7 X) Z3 t+ c) `1 }/ i" y
4.1 JS隞嗡撱箇JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗 - (function() {
X7 ?1 a. N1 E( P3 } - tinymce.create('tinymce.plugins.recentposts', {1 s1 w' H* F/ C! w B- M
- init : function(ed, url) {
/ ]! N8 z- E) M- ]2 D& P0 a5 \* Q$ t - ed.addButton('recentposts', { f! P8 I* b5 ^5 O8 V3 d
- title : 'Recent posts',
9 P& ], X4 @* j9 M( b3 H - image : url+'/recentpostsbutton.png',) v3 C* x2 U2 m/ X
- onclick : function() {$ L/ T* i9 u# c4 ?" ?) t% C: w
- var posts = prompt("蝡賊", "1");
" V& n+ ]- F& a& ~ - var text = prompt("隢憛怠亙銵冽憿", "唳蝡");
W# B/ a1 B3 l+ @( O/ _9 ~ -
" V* R+ W" B* w9 y# O0 U d1 y - if (text != null && text != ''){* p0 z8 Y" Y5 O/ \! j9 {, `4 m% s
- if (posts != null && posts != '')
: U. U5 U) s r+ p* Y1 P, K4 ? - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');
. Z3 d/ o& _" _. r8 H - else
- t2 ^: z1 f% V - ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');, D5 f6 R& h; C! V) ]6 T8 _8 }
- }
H; `. V; ]/ D W - else{
0 @* P2 L) |$ _- G# G$ P0 q - if (posts != null && posts != '')3 g7 ^" n1 J6 x4 Z8 Z/ R1 W2 i
- ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');
, W9 U' H9 v4 P5 o - else/ g! J' j8 F5 \' }* _$ P& s
- ed.execCommand('mceInsertContent', false, '[recent-posts]');
! |) W; k7 }5 z! N& p; s4 N5 L - }
( \- c0 O8 P$ x - }$ e* A( X$ O) O6 ^; Q% j9 f
- });% M$ S0 ?% J# O( x
- },* i. y1 @ N9 h- [6 [; U
- createControl : function(n, cm) {7 o! u6 j: G0 n- a+ ^
- return null;2 i; ^( O3 ]! Y7 H
- },# [# F2 ^( L' V3 y4 _
- getInfo : function() {' F$ H' y2 r; q$ V
- return {8 j4 h) n9 d5 |
- longname : "Recent Posts",% q" B9 Q. N+ s ]8 H3 a5 A
- author : 'Specs',
% h( O2 ?& @0 e% G. I - authorurl : 'http://9iphp.com',
# {* u" X! {* E: M `& Z - infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',
4 [. W" G+ O: w e - version : "1.0"
: f7 q) |4 K0 S7 @! u3 T6 ^ - };
8 K7 f! |- L) Q. x+ j - }
1 _; j9 l1 Z1 a3 G( N; j - });+ k1 r- P/ D5 {8 }
- tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);
7 @% c+ L- ?& D l; p - })();
銴鋆賭誨蝣 澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨 onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉 敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准
+ e! f# }1 n9 v
4.2 閮餃TinyMCE隞' J2 O0 C$ T) h# q
% ?0 @0 M# T' t- o 嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉 蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩 - function register_button( $buttons ) {0 n$ g, s I# R/ X7 ?- ?/ `7 ?/ Q2 P
- array_push( $buttons, "|", "recentposts" );
% ~; v# y6 k% Z% c! r1 x; G. E7 I% T. R z - return $buttons;
" Y9 j. V: W2 ~) L6 t - }
銴鋆賭誨蝣 蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅
$ F; b' E* i6 w, a( D+ p- function add_plugin( $plugin_array ) {. o' s7 @6 m) I, W( N
- $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';
9 m/ B+ e' ]- j - return $plugin_array;
q% X* p4 O# A+ q- h - }
銴鋆賭誨蝣 銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁* j3 v. i& P' f
- function my_recent_posts_button() { 7 |& C+ ~$ F2 M1 I, N# u! b! ?$ }
- if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {, R( |# v/ V% x4 M0 a' F0 ~
- return;0 h5 ?! S8 z$ m/ ] T
- }% S' b1 K, y$ C- g3 o* J
- ; I6 [) t$ w% U: k! K5 C3 V
- if ( get_user_option('rich_editing') == 'true' ) {. c% f" o4 l, [# I3 I1 g
- add_filter( 'mce_external_plugins', 'add_plugin' );
6 Q9 O( C* c$ E- G% t+ [ - add_filter( 'mce_buttons', 'register_button' );
- ^) u( f# k. o, ~) v9 g. D7 T - }
$ }2 t: T5 b: ~7 G. i1 f4 t) I - }
銴鋆賭誨蝣 嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁
2 Q8 R9 X6 Z. o2 W7 a3 y7 `( U7 Y敺賣詨曉訖ordpress憪賣- add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣
- d1 D4 f. q' \5 m- ^ @! T4.3 冽
n: l2 P- s* ]
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
5 ^0 r( t& ~4 s6 E' c+ z 暺嚗敶箏閰望霈頛詨交蝡賊嚗
! l3 O- y j) H. l U8 D$ h( N 頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
- d% A9 L! y& s" C. z 貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
# k$ _/ N( h' Q+ U 唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆 |
|