WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)3 z0 i8 Z7 J& T$ z8 ~- S2 _! a
N% E, `1 g: m# m* |
wordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐
F2 S4 V' J) l' X隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具
2 f5 B R8 q) N# O- D' o3 M% z+ @+ h- J+ g5 P4 s0 G
凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :. q0 q$ x& r4 G% g# N
甇文 , 臭誑閮剖銝訾批嗆蝡賊 :- K4 O1 p. g. P: G9 U4 k
甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :4 K$ Z. j* K6 C; i8 M: d
- [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣 % R, G7 s3 G' n0 U0 V: L
6 f+ U) I3 s* Z* E4 i) t7 G1 Z
1. 蝪∪桃凋誨蝣: N" `! m0 g9 y; u
祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :2 ?1 ^1 D9 Z/ d7 o* t2 y; ?
閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :
7 e3 G: B# @: W/ I1 l' }- 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
- 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
- 閮餃賣賊蝯針ordpressaction銝0 x! z9 g) P7 Q! v+ O' j
" A/ L, ~" J O1 v+ j祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准3 L1 i' }2 g0 h, Z4 ]" O9 d
1.1 撱箇餈賣
0 T4 M7 O% p9 T( B9 h' M- H2 q嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡
0 _% u& x9 h9 t8 a( i- function recent_posts_function() {
, X+ S1 G0 C# Y( d - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));
( O7 e$ y7 X; y# q- Q, W - if (have_posts()) :
, G7 a" m+ Z0 D7 \( \' `: C - while (have_posts()) : the_post();
3 c' R& m t0 w& Q - $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';& Y" P6 r8 P5 L, k+ _/ s
- endwhile;2 G2 U& p9 V5 n5 R
- endif;9 C# c: N% Z$ ]# L
- wp_reset_query();3 J5 K7 ~' E& w9 }& `! T
- return $return_string;" {2 c9 y" `: h7 C4 Y- o0 |
- }
銴鋆賭誨蝣 憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯
1 S5 t# T8 T6 a9 P
: M1 A# O( \ @- z5 T: A0 b
# e4 b, Q- ?" _1.2 閮餃凋誨蝣
+ r) Z ]3 A' j x: d e0 `5 O曉剁閮幃ordpress賣豢臭凋誨蝣潘- function register_shortcodes(){8 `& P [' S8 v6 H" e
- add_shortcode('recent-posts', 'recent_posts_function');2 q% m5 k! k: C! r
- }
銴鋆賭誨蝣 嗅冽蝡批捆銝凋蝙函凋誨蝣* l; Y: a* N8 m: l2 E
撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴% T6 Q* I$ ?" E: W
& p. b0 `; r, v- F# W2 G2 o
T2 U; U5 `& g
1.3 Hook into WordPress" I6 M. ]) a+ N2 Z$ [) O
3 D' k* _2 q5 v1 Y; G* L6 I; Z9 v箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准
& L3 D7 r0 o0 R0 \. O- add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣
9 a$ Y: L: d& F4 R2 K( W1.4 皜祈岫凋誨蝣
( G- {, [3 V' @6 e6 T蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :2. 脤凋誨蝣 2.1 凋誨蝣澆 凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻 閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼 撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 : - function recent_posts_function($atts){0 v5 O* d! W7 o, N3 `( T8 p# q
- extract(shortcode_atts(array(
( v3 n1 g8 z. M/ }7 X4 [ - 'posts' => 1,
2 t; |2 I& \* x! a* O8 X( d T! [ - ), $atts));5 K3 h: K W( H( O) M- Q+ `
-
; A5 _: y6 J0 }1 N+ O - $return_string = '<ul>';
+ z9 A& |! R' u: ~- V+ ` - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));6 l+ O( z; F ~ v; m' Q, h3 l. U
- if (have_posts()) :" w2 A* V% g. m. h
- while (have_posts()) : the_post();" e2 p \9 Q9 o' s/ U
- $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
8 l- Z8 `0 g2 `" l$ } - endwhile; k. P, j/ D1 Z& u% K+ u4 x: W
- endif;
& |$ N; g; u) K7 k* F8 ]) P - $return_string .= '</ul>';
( Y8 D. _% C: \- P -
4 w: |2 Y- _) |, E# X/ F: H - wp_reset_query();& u W2 ? C0 }7 b9 e
- return $return_string;
0 p1 n& J/ s. b( B- ^5 V# v+ n1 [7 n - }
銴鋆賭誨蝣 憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :
0 d* @1 n( ]! T7 \2.2 凋誨蝣潔葉憓摰
臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗 - function recent_posts_function($atts, $content = null) {
- d0 }$ w3 u, Y - extract(shortcode_atts(array(
+ o6 d* b: c& m' g: i - 'posts' => 1,
( F/ i6 B9 T3 Q. |% b3 b: r' N - ), $atts));: y) [9 r! T3 K
- ; t( C& g+ S$ P M
- $return_string = '<h3>'.$content.'</h3>';/ [0 j5 c6 {! x% _* v& G
- $return_string .= '<ul>';7 Z' a$ Q& ~& c% z+ q9 `) }
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));) w: W, o/ ^9 H& H1 n" m0 A6 c6 K
- if (have_posts()) :
. K0 s) E3 P+ U" h" E Y - while (have_posts()) : the_post();' w$ O1 d) d/ p' W5 ?
- $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
; |2 ~3 P V# m2 O0 X3 a - endwhile;- O; p2 L2 \( x, n8 b9 \
- endif;5 v0 g6 ]) r! w$ h1 v9 q
- $return_string .= '</ul>';
* ~; b& [7 v3 t; R$ ` -
3 M# `7 a, O! G: \ - wp_reset_query();
% u/ b8 C" H: ] - return $return_string;
( J+ q1 e" `% W6 Z" m8 ?( x - }
銴鋆賭誨蝣 蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝- [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣 支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗
7 q$ }5 |, E) j2 B# f' A- }3. 其遙雿唳寥*蝷箇凋誨蝣 3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣 閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛嚗典臭誑撠甇斤撘蝣潭曉典撌亙 " u3 }4 a; V0 q0 F' ]
臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 : - add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣 曉其閬靽格嫣遙雿嗡唳 , 瑟, 湧甈撠甇蝣箏圈*蝷
) u) y- F/ R! _! n% a0 l璅嚗刻隢銝剖函凋誨蝣潘 - add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣 冽閬銝剖函凋誨蝣潘- add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣 4. TinyMCE蝺刻摩典凋誨蝣潭8 n5 Y( o: E' p/ ]% r) G
4 u$ q& d3 f4 W; |7 I( { K
嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗 - 箸迨撱箇銝 JS 瑼獢
- 閮餃JS隞3 s; ?7 R3 Y4 N0 t9 l$ ?
1 @+ o( r+ z- v" [7 B! ?2 {- v5 z3 {4.1 JS隞嗡撱箇JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗 - (function() {
# q+ u7 O! b% }/ L- M3 i+ D- g. Z - tinymce.create('tinymce.plugins.recentposts', {* k# V0 C1 o4 t: a6 c3 z/ |0 x
- init : function(ed, url) {5 Z% l0 y3 P4 ~5 |( f! C. G
- ed.addButton('recentposts', {
: H* W7 p G2 Q! E - title : 'Recent posts',
! `9 B3 s3 U6 s - image : url+'/recentpostsbutton.png',
9 v9 v/ r1 h) M$ @# N: v" h- R - onclick : function() {+ K9 J: L1 S: V) b
- var posts = prompt("蝡賊", "1");$ W2 ^: P1 d- o$ J( u2 ]& ^
- var text = prompt("隢憛怠亙銵冽憿", "唳蝡");
3 w4 a1 q6 y; {/ L - , C4 F( u3 H4 V; |$ j+ x
- if (text != null && text != ''){: ~+ z9 d1 a6 I. Z
- if (posts != null && posts != '')
9 O$ p, |: x) Z6 b! i. {% _/ H - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');. @4 i2 r0 E1 B8 D. x4 v4 W+ Q
- else1 Z+ t7 O% O* t2 Q- D+ I& {- ~% _
- ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');
& V9 x- A* b8 K) V+ K - }+ l' O6 H \$ `1 Q9 f5 K* n
- else{
6 i' r2 y. c# b0 P/ F* P" ~ - if (posts != null && posts != '')
/ }& X, Z( U" x3 m" p - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');
' @2 b. L1 a) }: [& @4 {) l+ e7 W - else+ d9 Y: {& q [3 g3 N% |' k
- ed.execCommand('mceInsertContent', false, '[recent-posts]');- H' \ c$ y l7 _5 h
- }
6 X/ N/ x; u, g8 ~) P# |2 L5 W; i0 T$ Q - }& u& A8 K2 |! H, ?, y5 l
- });) h' N8 f' h0 I! }' S
- },2 I* a# f o) _* ` Y5 ~
- createControl : function(n, cm) {
6 j) j0 q$ {- ?5 D' O - return null;. D/ h/ t4 {' P9 j
- },! Y) G. U+ M. M a3 d9 b
- getInfo : function() {
' P/ Y" W& Y- R) C! n - return {6 @7 n8 E" G0 Q* x3 ?, u3 b3 B. l
- longname : "Recent Posts",+ H3 u# H6 N4 A: P6 k
- author : 'Specs', f( k6 Q2 O/ k& p. l0 S% s: j
- authorurl : 'http://9iphp.com',% ?7 s1 n% y9 O3 Y- g% d" q- c
- infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',
3 X+ B/ h0 ~* s - version : "1.0"! B. u1 M- C N4 {* A; t4 B# v7 T
- };6 g2 y/ R% R/ ^1 D2 w9 k
- }# N4 \) R* n& \6 a* \. L
- });
' U9 B( j2 I: q9 l; s - tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);
: b4 p. @' ~; s7 h; D& z$ Q5 c - })();
銴鋆賭誨蝣 澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨 onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉 敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准
5 ~2 d' F9 I* \4 Q
4.2 閮餃TinyMCE隞' C7 l/ L N. l# }1 X
5 h$ C" G! `2 }; w M 嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉 蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩 - function register_button( $buttons ) {
4 O3 ?$ F1 N0 ~ - array_push( $buttons, "|", "recentposts" );
' l2 H8 f, A, v0 I - return $buttons;
) v; H- O: I( N# |* D) ^/ e2 O - }
銴鋆賭誨蝣 蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅
* y- w8 ?) R- _: n9 V0 z- function add_plugin( $plugin_array ) {+ j, V: I6 s6 G' z. j. X
- $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';0 z, r! y0 y e& d u, b9 u
- return $plugin_array;
$ M: ], y8 g% b - }
銴鋆賭誨蝣 銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁& s+ G" b6 p/ K4 j- I4 G( U$ W
- function my_recent_posts_button() {
6 N2 d) i4 q( C$ C( R' F - if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {
n; i2 N8 q3 A: o# H - return;2 B5 `9 b( f$ H0 l) c
- }4 Q! O8 S# o+ F- t
-
8 Y+ S- W! y; {2 c+ F - if ( get_user_option('rich_editing') == 'true' ) {5 U7 X/ E- J+ W+ L
- add_filter( 'mce_external_plugins', 'add_plugin' );, W$ |! c1 ~, ~* c) u$ _- c6 f
- add_filter( 'mce_buttons', 'register_button' );9 }* O5 _( _( @. n( B ~* n) Q
- } ' v9 A- R& }& Q' K
- }
銴鋆賭誨蝣 嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁
* d/ t* x& ^; E- r6 Z6 Z敺賣詨曉訖ordpress憪賣- add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣 . L% }% m6 l* J( p, |0 x
4.3 冽% F; w4 S7 s* j/ V0 Y; M
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
. p5 l! n& `+ z5 e/ y3 A
暺嚗敶箏閰望霈頛詨交蝡賊嚗
! Z o0 M+ M, w* r" q 頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
! N8 ]9 H$ }1 b$ `+ r 貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
# [1 ~5 \- L: U: c+ F 唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆 |
|