WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)
: v2 y1 g$ l7 k( r6 d
: x) m7 U% U0 ?6 o, ]wordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐- e! F( ]% |; [& g. b0 V4 \
隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具. @5 X- }" q0 t" y8 o1 W2 ^, u7 S
' T0 K+ W6 E6 o# c0 T7 {凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :
6 q* G5 ] k0 s3 @' m/ J4 b/ `甇文 , 臭誑閮剖銝訾批嗆蝡賊 :
* E/ R8 W( D& R+ i- Q* S; P8 I0 h甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :
% n9 O0 Z# s7 O3 J2 y- [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣 ; N, @, L1 v! I6 ^% u* v: \
# G! y% F0 U" B/ p, h4 C6 S1. 蝪∪桃凋誨蝣9 h9 J4 _7 O4 X& p
祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :
. j9 Y/ O, S: D: F z閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :, P5 b) F. T, U1 m. B0 i1 l. S4 {
- 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
- 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
- 閮餃賣賊蝯針ordpressaction銝4 j* {5 z& k+ G
& ?: I6 N7 S. f1 W9 o
祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准
& D0 w3 T. j" x. Y# ^1.1 撱箇餈賣) R; d* E$ [$ a' W6 ~4 |# u8 Q
嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡
" V* c/ J2 x; q6 Y4 \+ W1 {- function recent_posts_function() {
* r* m8 ~* B' S5 A7 |) { - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));
5 o9 u2 e4 u' i: r" D3 t - if (have_posts()) :, c& i m, m: ?1 N
- while (have_posts()) : the_post();% ~: y5 A. Q" v) J/ a! L
- $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';
" ?7 J, Q8 q/ N$ ?1 G( {) c - endwhile;, L% t0 _0 j# t& X4 t
- endif;
/ z$ k2 m% J" @ - wp_reset_query();1 j1 R: M" J1 ^9 w) s
- return $return_string;/ L5 l0 i7 Y8 S9 @! M# {2 z
- }
銴鋆賭誨蝣 憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯: S# T2 q) R9 G( U1 I& l0 c9 C
5 ~# v8 S0 m8 {+ H% U& Q- ?
9 Z1 ~. W% |8 d/ O+ u2 c5 K7 }1.2 閮餃凋誨蝣6 Z9 x1 i% Q9 x- u
曉剁閮幃ordpress賣豢臭凋誨蝣潘- function register_shortcodes(){
; f( ~# C, @+ r - add_shortcode('recent-posts', 'recent_posts_function');% ^+ j! a/ j) }0 J
- }
銴鋆賭誨蝣 嗅冽蝡批捆銝凋蝙函凋誨蝣9 U# |$ L1 x& @9 O; s% S# w
撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴5 f, c6 o8 v8 V/ V
5 \4 d5 m! r8 o" k7 v
4 X2 }: T+ _- [4 K; v! \1 |1.3 Hook into WordPress4 \: _+ J, H2 D8 F
5 q+ B/ o: G( }
箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准
# L6 P- S3 G* {* H9 V- add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣 ( o, F2 @* `' d
1.4 皜祈岫凋誨蝣
5 n, o* }" X3 n9 f蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :2. 脤凋誨蝣 2.1 凋誨蝣澆 凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻 閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼 撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 : - function recent_posts_function($atts){ ?, H: [6 z( w1 R$ N; s
- extract(shortcode_atts(array(1 [5 Q! E$ _, j" c {
- 'posts' => 1,
6 Z {) `+ o. q" l) _" s - ), $atts));
7 F9 z% D) k( x -
; D3 o) G' g2 W$ t6 p! S( B - $return_string = '<ul>';9 O! {* d; K/ K) u2 o! E
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
$ y- Y) m7 L; X" E4 h! H7 | - if (have_posts()) :' W( F4 c1 w8 R# I, }* u
- while (have_posts()) : the_post();
! j& Q O* n% `5 G% s( p - $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
\8 |5 p; ?0 d3 C1 G4 l - endwhile;3 R t! p. g, K
- endif;: F5 f. K" |( G- ?
- $return_string .= '</ul>';
' w7 A' h& x' t5 {: ?2 ] - % D/ A5 B: g, [0 ~ D5 Q; r
- wp_reset_query();
# j9 t3 x: U1 ~4 e& t - return $return_string;: F+ k7 ^* e4 {0 R, L, {. }; d
- }
銴鋆賭誨蝣 憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :
, s; \ C' Y5 @6 `9 Z5 B/ d2.2 凋誨蝣潔葉憓摰
臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗 - function recent_posts_function($atts, $content = null) {
' _2 f. }5 T5 M; u! P - extract(shortcode_atts(array(
0 z9 N9 a" w# }; h2 r - 'posts' => 1,9 X( j- H/ A6 V1 z: R
- ), $atts));
6 z4 x: d- K- Q+ K$ Z [ - 2 S" {3 ^1 d8 R, ~9 M: ]
- $return_string = '<h3>'.$content.'</h3>'; K1 r8 W, O. {3 a; N3 Q! i
- $return_string .= '<ul>';& m& i: S$ i7 _# C
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));' Y' Z" N5 _8 K0 b. b3 x* g
- if (have_posts()) :" B# |. ~/ w7 N3 e8 m5 ?
- while (have_posts()) : the_post();; Z4 D% S/ w2 K R# \4 w+ M
- $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';# E* @" c; V- s8 k$ h' `* L
- endwhile;, E6 H- C! `# W% ~! T
- endif;
7 f2 k2 I( u: i7 @: W - $return_string .= '</ul>';9 C4 J& c6 @! c s) H) c
- 9 L8 Y4 w2 e3 K/ L' i% z
- wp_reset_query();7 }3 B0 w& f9 U I
- return $return_string;0 o# f# m$ Z; D5 L5 z( g6 m# w9 { P9 _
- }
銴鋆賭誨蝣 蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝- [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣 支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗
' c" y* t" c$ V3. 其遙雿唳寥*蝷箇凋誨蝣 3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣 閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛嚗典臭誑撠甇斤撘蝣潭曉典撌亙 5 Q) j# v! g7 T! ~; O
臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 : - add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣 曉其閬靽格嫣遙雿嗡唳 , 瑟, 湧甈撠甇蝣箏圈*蝷
5 d- ]6 k7 w4 J8 L璅嚗刻隢銝剖函凋誨蝣潘 - add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣 冽閬銝剖函凋誨蝣潘- add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣 4. TinyMCE蝺刻摩典凋誨蝣潭, G7 D7 w" E. i! r
' b1 F4 u" ?1 r. f9 P. Z
嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗 - 箸迨撱箇銝 JS 瑼獢
- 閮餃JS隞) Z0 F) r7 j! i3 [
9 u3 s9 X! H: x+ \) N9 h4.1 JS隞嗡撱箇JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗 - (function() {
8 ^! y9 ~5 C( p2 B8 ?& b - tinymce.create('tinymce.plugins.recentposts', {3 [: |( Q) b$ n* j K- W3 U
- init : function(ed, url) {
5 d2 I7 Q7 g8 j$ L4 u0 G: ] - ed.addButton('recentposts', {
8 r% c B0 y5 y/ @9 I - title : 'Recent posts',
# R: t) o" q2 I6 D6 N! F - image : url+'/recentpostsbutton.png',
; E0 i) I" q: N. w3 S - onclick : function() {
) t7 I/ `5 ]9 d6 n6 j - var posts = prompt("蝡賊", "1");
5 O4 {5 M- s# I# T/ z' L4 d4 F - var text = prompt("隢憛怠亙銵冽憿", "唳蝡");
. X7 I ^1 d8 ^; }/ }! b0 q+ k - / o0 `9 g J$ L/ {5 A. }/ X; }
- if (text != null && text != ''){
# U* _9 Z. p5 z! H/ `, R. N! G - if (posts != null && posts != '')8 U3 p+ _5 E2 e4 C0 f2 d) k6 G! f7 y
- ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');; x/ W: Q' }7 ^8 I( e
- else M' }5 T9 m! K6 I% y
- ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');9 K3 `' A2 |4 j7 A
- }
8 _6 } S+ X) ]/ {9 h, D2 j - else{3 a0 o4 Z8 a# |6 X# G5 j+ a
- if (posts != null && posts != '')4 Y6 R7 \4 r: z% F! w& n
- ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');7 a: t" e9 j* }& Q& z7 [
- else
: J1 A z) _( X Y$ Y - ed.execCommand('mceInsertContent', false, '[recent-posts]');3 J& m, A1 a3 y! r' w6 t5 Y
- }- [0 p/ O0 r E/ i8 a2 a1 S: l$ F
- }$ r+ o/ o4 y; }) M N, J9 J
- });+ b, p+ m8 Y$ R$ p$ Y$ B; m9 Z
- },
: ^6 L$ {# C, D3 b m - createControl : function(n, cm) {
. ?3 K5 K0 }1 F% z; t - return null;+ C& o$ X; x9 f: I6 `' `3 G
- },
6 }0 ~# M0 c# C! C4 S5 ^( A2 M - getInfo : function() {
( `; a& |' j6 P+ q - return {
4 `, J; l N' n) m4 h - longname : "Recent Posts",
" ~9 `# U6 G! K6 X8 p9 \: { - author : 'Specs'," ]* f$ }' A8 L- v
- authorurl : 'http://9iphp.com',
& g# P6 q7 d: a4 K! U) O& d; O - infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',
: P: R" D4 E- C8 t - version : "1.0"
. B7 d% W0 }" D% w: F" e3 l; y - };
& [7 [6 J! C0 } - }
0 e; p( u& H6 b- b7 I {1 g& \ - });1 V; l) u, x5 P
- tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);
) n- p8 y- G/ t7 F: \ - })();
銴鋆賭誨蝣 澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨 onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉 敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准 + J% U- g* g( o0 P8 `# R5 U% S3 ^2 ^
4.2 閮餃TinyMCE隞
! C! r$ R' I8 X: a' w
+ y1 E. \: Q( f& \. s) s 嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉 蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩 - function register_button( $buttons ) {5 i, Z; q6 }/ w+ K$ D* p) E
- array_push( $buttons, "|", "recentposts" );
+ `5 Y, M6 C: i: Y- V ~ - return $buttons;0 j9 s1 e1 C6 P, ]
- }
銴鋆賭誨蝣 蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅$ g9 h6 o8 e7 R8 W3 b/ B% L: _: x
- function add_plugin( $plugin_array ) {
" s9 c) C) o8 ?- ?# }$ k# M+ X - $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';
6 W/ o. Y' w9 Q# }8 @ - return $plugin_array;3 c% q7 F9 U$ }, ~( H9 H( s0 y
- }
銴鋆賭誨蝣 銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁) ]4 j F; w1 N' A" W
- function my_recent_posts_button() {
; a9 w* e$ Z9 |& [ - if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {' R H; v8 n" h) y
- return;: b; E$ \5 Y. F/ D* ^, @) T+ Q
- }
; ~- ]1 L/ S W3 S: f) Z -
+ t7 A8 D2 U- n5 H - if ( get_user_option('rich_editing') == 'true' ) {
) V$ Y( n$ S2 g z9 A& o - add_filter( 'mce_external_plugins', 'add_plugin' );
# B+ {' F3 R: f$ n$ m - add_filter( 'mce_buttons', 'register_button' );/ Y5 e% Q% b7 x7 |
- } 7 ^6 M0 U4 }' j- @7 M2 }
- }
銴鋆賭誨蝣 嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁" _6 E7 H; B) I! f& p& b3 j0 [$ d
敺賣詨曉訖ordpress憪賣- add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣
4 e+ C" L0 U( T5 {, ~4.3 冽# f1 b) J1 Z+ S. E# F
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
) U J/ @- j* r% Y
暺嚗敶箏閰望霈頛詨交蝡賊嚗
" j6 m* q3 p8 E
頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
+ g! U/ I, V, Z! B, g6 g 貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
; W6 m( @; y+ s/ D5 v 唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆 |
|