WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button): F/ ]: f# q1 h8 g
, q4 ^" U% g- C6 H0 C, _, awordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐1 G% I- p4 x& h0 S& a
隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具0 G3 I0 i. V% W2 S# f/ G6 [( A# Q& P
$ a# H1 t y) T8 z3 g2 q$ z/ d) u7 d凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :
5 _, w' }1 h+ T甇文 , 臭誑閮剖銝訾批嗆蝡賊 :4 W8 p4 t- I8 n! `8 v
甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :* m0 ?; @5 x/ i" y2 N3 L; i9 r! ?
- [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣 s1 j- X5 ?2 K3 `
8 C1 o* |: I& d' T! @; m$ m1. 蝪∪桃凋誨蝣
8 O0 l! [4 }/ C, U/ i+ D/ `祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :& {% \8 i9 r0 n. P& W8 r7 R' b' {
閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :( T" ]$ Z8 D. H" V* X' l$ h
- 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
- 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
- 閮餃賣賊蝯針ordpressaction銝3 Y) ^9 Y' B- L" f _" G4 U s
0 N" ]) I8 P8 ~6 }
祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准/ G9 r8 I8 K- m% A& D Q, ]
1.1 撱箇餈賣
! Q( {& t& W( F9 y% M嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡: ^) n: ]! R/ Z6 X
- function recent_posts_function() {
( i8 p- v& T& C( A2 ?5 G5 ?! a - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));
+ M4 k- P; }4 v/ r# j0 |8 X, a4 O - if (have_posts()) :& O. \6 {6 S( K
- while (have_posts()) : the_post();
( ~; y7 o( @6 s& V4 n4 C - $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';7 S/ |8 h, Z) k( D! k
- endwhile;
( K/ U8 M" i3 c; L0 W8 S# L2 N) y - endif;0 P! m% R" U( g; w# l. h5 _
- wp_reset_query();' s( `, x+ N1 L
- return $return_string;
+ S7 }4 }! T, P/ f+ y" E - }
銴鋆賭誨蝣 憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯4 Y1 B8 p2 U, O3 }5 B
* z/ `$ ^+ U/ B& s' U
6 U2 r- c. d6 b
1.2 閮餃凋誨蝣6 k* ^! _9 V0 U& I) w+ x1 H L% F
曉剁閮幃ordpress賣豢臭凋誨蝣潘- function register_shortcodes(){
, F4 f) {% a$ p - add_shortcode('recent-posts', 'recent_posts_function');
1 f9 l# x$ @$ W1 ^5 [+ A - }
銴鋆賭誨蝣 嗅冽蝡批捆銝凋蝙函凋誨蝣
. J/ [. K L- D$ l/ ~# y8 N撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴9 z+ H3 b8 h; r8 ]$ }
9 R2 n. N' c1 I& A+ \+ N9 C
3 P- `" F$ I# D! O- y1.3 Hook into WordPress
% O! k, B3 g8 @1 d
5 o( n; W5 [, G; c+ A. _箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准
: J7 O4 v1 X7 R. N. o% B- add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣
5 u& T, g" h$ ]! k5 X" z6 K1.4 皜祈岫凋誨蝣. v. \' A# S* I% o! ~0 |% S4 ~/ \
蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :2. 脤凋誨蝣 2.1 凋誨蝣澆 凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻 閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼 撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 : - function recent_posts_function($atts){
9 g! i4 J0 T* o9 ?; h - extract(shortcode_atts(array(
5 }: Y+ E9 w* P8 ]+ Z1 }( {3 D - 'posts' => 1,
1 V6 y7 S7 X. t% ?5 j: f, l- [ - ), $atts));
3 n1 _$ |1 R, C -
9 D* X. L; h( M/ A/ o2 g. W \ a8 j - $return_string = '<ul>';7 `& [( r. c. I( v2 B c( [, N
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));' I' K) B/ ]1 g7 S5 j8 ?2 t
- if (have_posts()) :3 A* k7 w! ]4 r
- while (have_posts()) : the_post();. ]3 p3 R5 ]' M6 l% j6 q
- $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
# c/ X4 \6 S- Q0 ]+ F# B - endwhile;
* B6 r! w8 G4 y1 _ - endif;
* K- _( h3 o% }: q- z# p - $return_string .= '</ul>';
* n! W5 B' | k& ? - 7 y3 `! s9 S' ?+ K
- wp_reset_query();
& D, T$ k1 c, ~9 q - return $return_string;: _6 \ p1 a& z! A4 [* B' ~7 r
- }
銴鋆賭誨蝣 憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :6 x* A/ a. ?6 E* B6 G. d* h# m% d! l
2.2 凋誨蝣潔葉憓摰 臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗 - function recent_posts_function($atts, $content = null) {
/ k( r; g$ T# p/ B& |0 w - extract(shortcode_atts(array(
. S- }+ a0 m0 A% B5 k - 'posts' => 1,, o3 g8 }5 F2 v
- ), $atts));6 G: j4 C n$ K! W: ?. f! `
- ( }' m [ Y6 X' j7 u
- $return_string = '<h3>'.$content.'</h3>';* ~! b( ~/ I0 i: J. W& K
- $return_string .= '<ul>';2 u! [% Y2 O3 ^! {" q) {
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));3 ~+ Q K1 J" H/ Y- ~
- if (have_posts()) :
) P# B; ^) \( r) u - while (have_posts()) : the_post();% e6 ~* d4 H; a
- $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
6 g0 V3 D; o B2 \ - endwhile; L# X4 V# r. T6 D, z4 t$ ?7 Y
- endif;) P0 Y7 S1 k' X3 n
- $return_string .= '</ul>';
, }. u4 P, `% P @$ U -
& A# r0 {6 b8 K, Y4 G( {7 D4 { - wp_reset_query();
6 Q) A5 t8 t a4 }; h+ X - return $return_string;' f5 r) G1 V3 v2 A; g d
- }
銴鋆賭誨蝣 蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝- [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣 支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗
" [$ c% k/ H7 u2 \0 P+ X$ f$ m3. 其遙雿唳寥*蝷箇凋誨蝣 3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣 閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛嚗典臭誑撠甇斤撘蝣潭曉典撌亙
* p) ~: l6 i) J臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 : - add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣 曉其閬靽格嫣遙雿嗡唳 , 瑟, 湧甈撠甇蝣箏圈*蝷 + d' Q8 u* E {# ]* ]1 c
璅嚗刻隢銝剖函凋誨蝣潘 - add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣 冽閬銝剖函凋誨蝣潘- add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣 4. TinyMCE蝺刻摩典凋誨蝣潭
6 r7 k0 } |" j1 V* `$ o
5 J2 D: z; e( @4 b0 m嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗 - 箸迨撱箇銝 JS 瑼獢
- 閮餃JS隞; e! J4 m; K) ~7 B7 x+ S, W, h" R
K7 N/ O' y7 z- J2 X. x
4.1 JS隞嗡撱箇JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗 - (function() {
5 {" X8 g5 i- ?9 b) z" ?7 v& Q2 O - tinymce.create('tinymce.plugins.recentposts', { d- t8 b2 N+ a* P/ R
- init : function(ed, url) {; p3 T8 q/ Q$ C) H
- ed.addButton('recentposts', {
2 `7 x! V2 E s: ?) P2 @! I' x - title : 'Recent posts',
( Y: b% p3 X( G6 M: v0 Q& H7 G - image : url+'/recentpostsbutton.png'," I- h5 Z! E8 z- q4 f6 a2 H
- onclick : function() {9 K' {/ J$ E1 M% h& o
- var posts = prompt("蝡賊", "1");
9 G0 U" ~2 O8 w - var text = prompt("隢憛怠亙銵冽憿", "唳蝡");9 r; A* n' Y# S7 S$ z
-
; n0 \" P+ c% L7 @0 Y I. T! g - if (text != null && text != ''){" j* i- k( f' e4 K
- if (posts != null && posts != '')) j1 x# d3 U4 F0 H+ M
- ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');
1 h+ I7 ~1 U* _3 f% d, k - else
: k+ E" f2 b5 d( b) u - ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');
# T6 v- m7 D. `+ p" e4 P8 Y - }$ G/ c* M, a/ C; g" T8 ?# y
- else{0 P8 O5 ^4 U; L. D
- if (posts != null && posts != '')8 i% I5 i, R# D+ ^9 g
- ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');
5 K' p% u) G/ U2 p4 O! U" U) S - else
3 j2 q, x" o( Q - ed.execCommand('mceInsertContent', false, '[recent-posts]');
. _8 l2 X/ d3 U, y" K' q7 Z - }
+ f, J. W: u6 G" R% D - }
& C5 b' j3 z5 P$ O - });( M- T$ r+ M/ Q9 Z1 ~
- },
% ^ V$ ]6 u& g& g7 F7 [ - createControl : function(n, cm) {
4 s4 p7 k y) S8 ]% [4 ^+ j, Z - return null;
& C; j$ `) }) w" Y8 V, {' ` - },7 l! n: F$ \: u8 w! ]
- getInfo : function() {% u. k% T3 f% _* h [6 K @8 v/ \
- return {3 l, s) @( T7 R U( ^" ]; F( p
- longname : "Recent Posts",
% |7 N# J2 t8 Y$ ?7 w! r& t* q5 B - author : 'Specs',4 Y% c8 y) L9 b0 y& {6 w8 t
- authorurl : 'http://9iphp.com',
P7 P" F, I4 F/ I. U - infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',
6 M- t; d9 o( ?, x0 i) R& P8 P - version : "1.0": Q) C" B6 e! z% w: l
- };1 L) L+ W& s$ Q. |+ a
- }
/ T# j Z( h, @& K- ~ - });% f! o* S! ?+ S$ |
- tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);
8 O9 g) H6 g9 o% c& g9 C5 h - })();
銴鋆賭誨蝣 澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨 onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉 敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准
, K7 q$ c2 C0 q, @
4.2 閮餃TinyMCE隞$ c4 G9 w" \% A5 H
2 b+ F* `9 _- A: w* T, | 嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉 蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩 - function register_button( $buttons ) {+ G C c; ~6 l# K u( X$ S) y3 t
- array_push( $buttons, "|", "recentposts" );
1 d+ B/ U4 m! H4 h2 x - return $buttons;+ j, n$ E3 b: e+ z: O
- }
銴鋆賭誨蝣 蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅) i# `. i+ B+ U$ u. f0 L! D' x$ c
- function add_plugin( $plugin_array ) {2 {' I/ \. x1 J4 f
- $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';
$ p0 B2 U# U+ P1 S" u" k - return $plugin_array;
7 y) l! \* W% G# `3 j - }
銴鋆賭誨蝣 銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁
3 c% l/ d3 k( ^8 A) ?5 m9 l- function my_recent_posts_button() { ) m2 O4 e: C5 u g1 n7 f+ M
- if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {
& q1 F7 z7 A' F5 l' ]2 c' ?: u" J3 O - return;+ z9 ^7 E, a5 }* _1 @ {* I' O
- }+ \* U4 N( _' O( C# p3 x) d1 s
-
, E4 F, C& f$ G( D - if ( get_user_option('rich_editing') == 'true' ) {
: K/ j2 ?0 z k - add_filter( 'mce_external_plugins', 'add_plugin' );
# Q1 s) ?+ e4 G5 g9 F8 A - add_filter( 'mce_buttons', 'register_button' );) R- w8 q& _! y9 h* d
- }
" n H8 R! W6 r' l" n8 C - }
銴鋆賭誨蝣 嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁" s f& j/ f/ a/ C4 b
敺賣詨曉訖ordpress憪賣- add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣
% @% n9 ]! U N( l1 F* ]4.3 冽! a7 l2 L8 r6 R, j1 b4 g7 u7 X" z* g
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
. c8 a- I3 v; ~% Y: j! B+ j0 s
暺嚗敶箏閰望霈頛詨交蝡賊嚗
1 U& U6 D8 F3 v3 A. s! B
頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
( v" t& c) K) N0 v6 y( ?" k9 \ 貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
4 D8 ^9 _& n/ `, H U4 G5 j+ }
唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆 |
|