WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button), ]- h0 V1 q6 A9 f$ g2 ^% z" ?
: Q$ }4 p9 m, _1 g' i" Z0 @+ D0 j
wordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐
+ v5 x N7 i- A隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具
2 m: X% F0 H- @: h. x4 O- C5 p. P! Q: r! q
凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :. ^+ w4 B% p0 ^1 B- D* L
甇文 , 臭誑閮剖銝訾批嗆蝡賊 :
. K& D: F D1 v5 i5 {甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :' d+ z1 C+ O. L- o. I0 ^4 ?3 m
- [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣
4 U" j1 r; r- `+ l. m: C- i
& u! |) O1 b5 T7 ?1 g, B7 F1. 蝪∪桃凋誨蝣& G9 o* Z& `5 c. G
祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :
. H# k' \3 j5 x7 p# C: O: o閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :
' _5 d. z0 m" t- 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
- 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
- 閮餃賣賊蝯針ordpressaction銝+ J, O7 |5 b! b2 e. U; r" j
$ W( J" w; w. |* J
祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准
0 e6 z6 |: d0 g K( E1.1 撱箇餈賣
4 n/ Q8 H" E7 Z9 I1 c* i6 J2 @嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡
' {5 N! Z/ ?0 x3 e9 t4 p% L- function recent_posts_function() {1 h5 }; C! B* g# F4 _
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));# z& z' O! C7 a( \# D! K' X
- if (have_posts()) :0 {$ v) ] E2 ]! a2 p8 d
- while (have_posts()) : the_post();
3 m+ K2 e: ^- J4 W! O - $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>'; r9 ?1 K9 E$ Q$ H
- endwhile;
# Q# F: V" o4 w* x1 z1 J2 {1 T$ w - endif;$ A, S" n- U. F( I) ?' G' l
- wp_reset_query();
$ r; D) l0 T, g2 m* @3 r - return $return_string;
& g# w6 K9 I" i! U: [ O- A - }
銴鋆賭誨蝣 憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯
# h b, Z" Q H; S# N9 m# f, m3 f# P- S: z$ u. k0 c
k4 ]3 [0 z. u( U# S' g
1.2 閮餃凋誨蝣
7 r, Q J4 N' l) W5 E: d曉剁閮幃ordpress賣豢臭凋誨蝣潘- function register_shortcodes(){
2 d" c4 P* ^% \. m" S/ x2 u+ d8 j - add_shortcode('recent-posts', 'recent_posts_function');
1 E' a" P% {8 z$ ]' M - }
銴鋆賭誨蝣 嗅冽蝡批捆銝凋蝙函凋誨蝣
3 W$ B' j: {5 G4 F# l6 B% M, U" a撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴
& _) F( B7 U" o7 J% {% |) k( \5 ?( A1 `* E# z; r% i. e
0 D! w3 a f, b( F8 b1 O& _
1.3 Hook into WordPress( ~" ]% F( g* q- J, ]6 F
6 ^* c- g& h7 ]! r% a
箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准1 \! f8 `2 u# e" @& R* B I
- add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣
. l3 l, }( M6 G$ M; I1 _ L5 s/ u1.4 皜祈岫凋誨蝣 k8 U9 S5 G M* `
蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :2. 脤凋誨蝣 2.1 凋誨蝣澆 凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻 閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼 撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 : - function recent_posts_function($atts){3 P0 x; s6 C# i
- extract(shortcode_atts(array(
1 O* e( b) ?: B4 e% O - 'posts' => 1,6 q! P: J; A8 ^& C
- ), $atts));
0 f- I$ ?/ x6 N, j# i3 z' g" d* c -
1 C. l" s9 ^4 j" C/ z3 N4 } - $return_string = '<ul>';
( b1 p( M8 O6 N% L1 ~0 P7 B - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
. v% \& s8 R1 j3 t - if (have_posts()) :" |6 t' y% R- C* {9 U" G; P) k$ d$ F
- while (have_posts()) : the_post();/ w. A5 O8 z6 o! D. [$ E9 C
- $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
4 ^! S+ X5 c( n( s - endwhile;5 |& f5 X7 ]2 f$ b
- endif;, y9 t' p8 B9 \! l9 b( t- @
- $return_string .= '</ul>';, n1 u5 J7 l: o" G- M( M8 w
-
! C9 n' Y5 E6 l1 R U/ v - wp_reset_query();
) U; I, p5 i5 f$ ?6 g - return $return_string;- i1 |( {, A. X) O; W5 P
- }
銴鋆賭誨蝣 憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :
w. w) f2 S h2.2 凋誨蝣潔葉憓摰
臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗 - function recent_posts_function($atts, $content = null) {* t! E/ p4 [6 Y8 r) D
- extract(shortcode_atts(array(' E# ~* q/ U3 e( u+ ^8 ~3 f
- 'posts' => 1,
1 m* w$ S, c; |' ^ - ), $atts));
2 w! o: d3 I/ e% P# U. h( r - 0 T* O% Y; X ?
- $return_string = '<h3>'.$content.'</h3>';
) L. q9 r2 v$ Q+ m% ~4 @+ h( t& |7 o - $return_string .= '<ul>';
# k% {/ ]2 A5 {% r - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));: b9 o. V& R; c7 D0 H, B
- if (have_posts()) :
' Z. A7 ?0 j+ ^) [: K" c - while (have_posts()) : the_post();5 ^1 P; w* K3 P4 L4 \
- $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
3 G* Q& v0 T; W, @2 [ - endwhile;
) t, r0 L6 |$ F3 z2 Q - endif;
) f+ `1 l. h- R9 C( }* \- G$ Y: N - $return_string .= '</ul>';
# ~! c+ T+ E! Q: m& ~/ |1 ^' x- e - $ r8 ? |; t$ l3 C. N
- wp_reset_query();/ ~( g1 i: g3 K* v# n2 u- N8 d
- return $return_string;6 k2 u) T, v5 E' A
- }
銴鋆賭誨蝣 蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝- [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣 支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗/ [, _, X9 ?; f1 J
3. 其遙雿唳寥*蝷箇凋誨蝣 3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣 閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛嚗典臭誑撠甇斤撘蝣潭曉典撌亙 ; B1 U# X {0 C. u% q
臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 : - add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣 曉其閬靽格嫣遙雿嗡唳 , 瑟, 湧甈撠甇蝣箏圈*蝷
2 h$ j6 s- P% z# q7 u3 B5 ^1 `璅嚗刻隢銝剖函凋誨蝣潘 - add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣 冽閬銝剖函凋誨蝣潘- add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣 4. TinyMCE蝺刻摩典凋誨蝣潭8 D, P3 O5 s" X
0 ~; d% _* Q& d
嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗 - 箸迨撱箇銝 JS 瑼獢
- 閮餃JS隞
2 ~1 w" X4 m9 C, A
0 L( d& G( b0 ?2 H8 E6 b; t4.1 JS隞嗡撱箇JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗 - (function() {
* A/ R4 z* m/ e; P - tinymce.create('tinymce.plugins.recentposts', {
* y8 O- c1 m# z$ G - init : function(ed, url) {" W# j9 w! i0 k3 R0 E! V) V6 k
- ed.addButton('recentposts', {+ p) I+ D( a; a0 d6 [$ p
- title : 'Recent posts', p4 L/ e9 Z7 e; {0 i5 P$ r; o* {
- image : url+'/recentpostsbutton.png',1 `( g4 @- M I; j
- onclick : function() {
* P" [9 v% i4 ^4 o2 W$ | l3 d - var posts = prompt("蝡賊", "1");' G8 r4 t, f) s0 [5 O/ P r
- var text = prompt("隢憛怠亙銵冽憿", "唳蝡");# v- }! ^7 I5 P/ h" ?5 _
- $ D% d/ i( D; |5 y D8 f
- if (text != null && text != ''){
- `) @' u- ^" u9 d - if (posts != null && posts != '')
" @- F4 a5 ], O2 }5 J - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');+ E7 O# Q6 B5 r( S5 L6 t* |3 u5 i
- else# Q7 K0 ]* }( x- D. d' e$ w
- ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');8 V3 q y& v- S; p+ f7 R
- }
: J$ `" G+ n' Z, c; s - else{
6 I2 U% T& _0 U J; n5 r; e! L - if (posts != null && posts != '')0 z/ J- G& F. l M" s
- ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');) ~+ [4 p( t% r: E5 t
- else2 p5 r1 f* a I6 F: t1 d a
- ed.execCommand('mceInsertContent', false, '[recent-posts]');5 b& @! n/ r5 t: ^' @
- }3 ^# C; M, {, q/ |
- }# d" |, `) {0 y& H+ s
- });! ]' ?' ?/ U0 s( ^( X, h& {- p
- },
5 a9 K6 x" C1 C8 o/ | s - createControl : function(n, cm) {6 F% T8 D" t8 ~9 ?0 W8 S
- return null;
: d, x2 v) A0 \: t* H6 u7 A - }," U0 A) f) I. M; y& D* C
- getInfo : function() {
8 @! S& v% ^( s6 N# O" q" f - return {0 a" {9 f( O! b* F# T) i2 B0 b
- longname : "Recent Posts",
. {; n2 ?) V5 U, J1 L. N& `4 V9 E - author : 'Specs',4 e! R+ e, P: @
- authorurl : 'http://9iphp.com',
' J* _, Q0 b# u C - infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',, q% S* q. s, B8 b
- version : "1.0"$ A+ x" o$ [0 n
- };0 D6 z5 B: Y; q0 y. m( ?9 f
- }
7 a) N, E: I1 c" Q - });( ]0 j8 Z E( T3 [1 d) R
- tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);" a% q# }' w/ c# Z6 o7 G
- })();
銴鋆賭誨蝣 澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨 onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉 敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准
# \9 f2 `: h+ x# P% J
4.2 閮餃TinyMCE隞& u- M/ z% g5 {+ E
( J) d6 v7 a5 K# W& p. O' l 嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉 蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩 - function register_button( $buttons ) {8 g# K( i! y9 D5 g
- array_push( $buttons, "|", "recentposts" );
) b1 z+ L1 f1 d: J - return $buttons;
1 t' ]! E8 U$ l5 Y; d( l - }
銴鋆賭誨蝣 蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅
5 M; A7 J' X' U- function add_plugin( $plugin_array ) {
' F7 u; _0 A6 y4 u, J$ W) i - $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';
9 O2 w) k, ^- s' E4 t - return $plugin_array;/ r( F5 h/ g5 u7 B
- }
銴鋆賭誨蝣 銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁3 b- T) f4 ~/ |7 \) A
- function my_recent_posts_button() {
7 R3 ?1 X) @! V$ w! G8 {) G - if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {9 R( T, m/ R% i; l) ]2 j5 `
- return;: ~. z* Q6 T3 @8 L" C
- }" ~8 w% a- [- P
-
% ]! G+ t' W* w; c2 s - if ( get_user_option('rich_editing') == 'true' ) {0 \1 s, a/ a. _4 G7 {+ C
- add_filter( 'mce_external_plugins', 'add_plugin' );
3 q! {5 L& {5 u. b3 b! [" h - add_filter( 'mce_buttons', 'register_button' );; U. c7 v% {: f. _3 ?# M% [
- } # n. l. }; i2 Q' y/ o
- }
銴鋆賭誨蝣 嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁
# P+ M; S/ Y% E: M6 g敺賣詨曉訖ordpress憪賣- add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣 ; W4 ]9 R1 z5 U$ ^* B- q b2 ~: f
4.3 冽3 ^1 K4 |& z! ?$ F
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
4 `1 s& u3 w* I. B% E" v& P' U 暺嚗敶箏閰望霈頛詨交蝡賊嚗
0 C6 g" T1 J2 ^+ d) V, | 頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
) s$ o; ^( ]2 a6 x8 O+ ^9 F 貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
- y0 g. j( t; _- W+ x, p1 U: m 唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆 |
|