WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button): d Y. l9 T. M- K
$ P8 z% y0 J* ~) M
wordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐+ I: P$ M0 P" g2 }4 p8 d
隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具. B G$ }, D5 p& z0 ^
& Q' C3 r9 p$ }4 W' R. r2 p
凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :
; }: n7 m. i! d, L甇文 , 臭誑閮剖銝訾批嗆蝡賊 :
$ f3 V4 \6 }' e% m7 d甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :
6 t* ~. ]3 \6 P- [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣 1 e9 |3 |8 Y" C1 c
' y3 P( [2 Q) R) @' H; H! {1. 蝪∪桃凋誨蝣
1 T4 y- i1 x3 e3 J祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :' Z% j$ Y1 [9 L. T$ B
閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :
& ?6 o% |9 [- t9 X# ?7 |- 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
- 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
- 閮餃賣賊蝯針ordpressaction銝
. N5 ` A$ X" Q4 F
u( P, j( f3 Z0 p. v祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准
h+ O) h; q. G/ J* s1.1 撱箇餈賣5 s2 a* v5 W$ \% r% H' n
嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡
" D$ m* r$ _# }3 A+ T3 F- function recent_posts_function() {( U/ X2 a' z& w6 t6 u
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));
1 T7 a8 k: l1 a2 N, S - if (have_posts()) :
) B0 i. x5 m/ V) y - while (have_posts()) : the_post();; L" W9 S: x1 \ F2 r0 O1 g
- $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';9 Q' p, M0 X* m8 L* F+ a$ X! Z
- endwhile;6 f1 v" q! q' i
- endif;
( \5 A- g* ?& }2 R1 i2 T2 [0 s; C - wp_reset_query();
! O, e, S5 D- i5 \! k6 c - return $return_string;
8 Y2 k9 c/ z: \" N) m - }
銴鋆賭誨蝣 憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯7 c- L. ?( |1 `9 p: h n( M
# Q. u; U9 e% n" ]$ W& L
8 I. w' z0 q B) E' _$ r
1.2 閮餃凋誨蝣
" \; t9 F2 a* r! e曉剁閮幃ordpress賣豢臭凋誨蝣潘- function register_shortcodes(){
* n+ `" E( w2 M0 M - add_shortcode('recent-posts', 'recent_posts_function');
. m* r M1 n% @# {8 I0 ] - }
銴鋆賭誨蝣 嗅冽蝡批捆銝凋蝙函凋誨蝣 v: u; k) {4 j( c( `' [
撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴$ P0 O' l v9 ?" i' y* v& V8 ^: [
% X5 j! {$ i+ w! A$ n! s9 H4 A$ Q
' q3 R8 j3 f G ^, |% l" h9 D1.3 Hook into WordPress/ R( I+ b* k& ]- v
( m7 K- f' ]6 V$ ^* U0 q/ @箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准
, N6 B* C8 b$ [' |$ m/ y- add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣
, x& X, [1 U, s$ \9 p. P1.4 皜祈岫凋誨蝣
" ?% D- |: |5 m: Q+ l9 X( e5 P蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :2. 脤凋誨蝣 2.1 凋誨蝣澆 凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻 閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼 撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 : - function recent_posts_function($atts){6 X4 u' x7 {: K. I& C' C% h
- extract(shortcode_atts(array(
7 q3 g' W4 {2 `; | - 'posts' => 1,: W7 W2 O; t8 g: `5 B7 r+ j9 Q
- ), $atts));
% `; V% m7 O- D -
. M+ E& v: I# b# Z' `/ P - $return_string = '<ul>'; f/ G: F# @- j+ w, s; b
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));3 J& \5 O% ?; \* S
- if (have_posts()) :
; u$ Z C% ^# Y, p z - while (have_posts()) : the_post();! H7 F: t% C7 ^% c- k( A
- $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
' c, P c' I/ J - endwhile;0 P/ l {8 ~4 C( H: `* I3 `
- endif;, g% R( S: U, r) w
- $return_string .= '</ul>';
; |0 T5 C9 t4 S! S; b - ^3 Y1 x& G7 `; m
- wp_reset_query();
6 w- V, b% ~2 s6 u- Z - return $return_string;
# P- x& C% d8 o7 `9 z6 i, r4 Q - }
銴鋆賭誨蝣 憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :# ~5 t+ _3 I0 c5 R! w$ ?* X( x
2.2 凋誨蝣潔葉憓摰 臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗 - function recent_posts_function($atts, $content = null) {
1 ?" `# y& y o& E+ {" j - extract(shortcode_atts(array(. |2 d8 A a7 M0 x9 Z
- 'posts' => 1,
' O$ ^0 F- S: S, u% O1 ^ - ), $atts));5 U' x6 j ]" i; ^6 h
- 9 M4 L/ M* q: u9 b' w2 [& J- G
- $return_string = '<h3>'.$content.'</h3>';! I: K& H3 e# }' R1 t
- $return_string .= '<ul>';3 G8 I5 a7 K! v2 E- R& L
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
5 g0 Z: O) E# s/ J2 W2 E - if (have_posts()) :
) x3 O7 w' m \% K - while (have_posts()) : the_post();
3 \. N, U2 M9 e - $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';/ }9 x4 E: _$ H+ T( q
- endwhile;
" E& j0 f% `& W. `( q - endif;2 a$ x1 N1 H2 x) l+ b
- $return_string .= '</ul>';& U' _0 W: r% w7 }+ z H# l3 [
- ( }$ q; I6 ?' o/ H; E3 {/ e
- wp_reset_query();. I2 k" c+ C5 |; h
- return $return_string;
# m4 `! g% w/ a! i - }
銴鋆賭誨蝣 蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝- [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣 支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗4 j& h m; {& p1 p: ^! E3 ]* R
3. 其遙雿唳寥*蝷箇凋誨蝣 3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣 閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛嚗典臭誑撠甇斤撘蝣潭曉典撌亙
3 V/ |$ H6 z1 f; K1 l- Q臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 : - add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣 曉其閬靽格嫣遙雿嗡唳 , 瑟, 湧甈撠甇蝣箏圈*蝷
0 Q+ w, v3 L5 K璅嚗刻隢銝剖函凋誨蝣潘 - add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣 冽閬銝剖函凋誨蝣潘- add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣 4. TinyMCE蝺刻摩典凋誨蝣潭/ L2 U" r- f. R" b
- \% |$ s' s( f/ S" H& O
嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗 - 箸迨撱箇銝 JS 瑼獢
- 閮餃JS隞4 `4 S* `3 U7 | Z4 E j
0 O' N) s- ]1 ~& B* [' g
4.1 JS隞嗡撱箇JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗 - (function() { S% G0 `# p. _2 U
- tinymce.create('tinymce.plugins.recentposts', {4 k; m6 j7 A. K- B6 _
- init : function(ed, url) {
9 P& j' p- U$ \% l& X0 Z2 [. S - ed.addButton('recentposts', {4 b2 o, N% \5 |3 N# O: ?* W* u# v4 v$ E4 G
- title : 'Recent posts',8 E9 c! {% X7 z3 {% ?
- image : url+'/recentpostsbutton.png',
6 ^- G( O6 B$ Z, L" O6 ] - onclick : function() {
) t' D* r! d5 B: R+ Y4 T$ t) [ - var posts = prompt("蝡賊", "1");2 i! P! V8 S3 }- I( b
- var text = prompt("隢憛怠亙銵冽憿", "唳蝡");; w% A2 x+ p! U/ ~5 [( P# G
-
1 i( P/ E7 Y d, |+ X - if (text != null && text != ''){. x1 H; l, O# C% K) w: h
- if (posts != null && posts != '')8 \# Y) E" J: a/ X
- ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');
8 K, j* n" r8 \' b( _ T3 G - else# K f: N8 P8 p6 h
- ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');
9 h6 B# R* B; d6 u. Q! Z - }9 ^. k4 q& f S+ g/ i# P- Q
- else{
8 R, B$ {2 l4 r) d# A+ P - if (posts != null && posts != '')- `( l s2 `$ e/ S0 K6 ^- m
- ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');
6 t. t5 D* T2 m4 p. Z - else( L* ]' y' r$ Y9 K% n& ? w5 ^+ Z
- ed.execCommand('mceInsertContent', false, '[recent-posts]'); c8 A2 i6 L: |/ W
- }
3 M* n2 o9 Y: w/ g- S - }
: o: z2 L1 B7 u9 ^6 u6 ] - });3 Y. @! g% e: b f" X
- },
- |3 C2 d& C: \. F+ O - createControl : function(n, cm) {8 i7 u: f; w7 I! k1 L9 K* E. x
- return null;) c7 N1 p, R9 |" f9 A3 H q- Q
- },8 V9 u; W+ j0 N9 F" G/ u
- getInfo : function() {8 Z7 R5 K/ o4 y
- return {
8 |2 x: e+ ^' u: O, @/ D - longname : "Recent Posts",2 l( }6 n+ z8 b& `8 n; K
- author : 'Specs',
: i$ T' ?0 L% w. a: [7 H7 a - authorurl : 'http://9iphp.com',
( Z' X: y3 Q6 U; `( f d* T) w5 F) ` - infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',
# V2 q, ~, ~/ V5 Y6 a% X7 V - version : "1.0"
4 v) @. i3 E9 a+ x' h - };
. [' s4 y. z u - }
. U# A: t( E9 \- C) x- Z% ]% p- U - });
, y# P* L& o, y) A& Y" C6 E' U - tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);
4 k. N: `1 l/ N8 d4 |1 Z - })();
銴鋆賭誨蝣 澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨 onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉 敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准
, U7 L2 E* ]# _7 ]( z- l* P" t
4.2 閮餃TinyMCE隞
# B3 n* E! u0 z8 A- X
2 J5 s; z) y( d/ o5 p" K/ X+ B 嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉 蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩 - function register_button( $buttons ) {/ h) n1 b& n" N% _& G
- array_push( $buttons, "|", "recentposts" );5 |" [( B- R- g
- return $buttons;( ]3 U( M9 l i! A+ X. I/ M
- }
銴鋆賭誨蝣 蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅
7 Y7 y# R4 f+ A' X- function add_plugin( $plugin_array ) {
% y# b% y7 `( \( b% j4 w8 P - $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';
" k5 p; u/ ~4 l7 Z, A! g- V - return $plugin_array;* q3 A3 R: {# m7 l; k
- }
銴鋆賭誨蝣 銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁$ g% v, ?5 @ m, y; G' o( m
- function my_recent_posts_button() {
. `/ ?, {/ r9 O - if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {
5 D J& a+ \$ W - return;
8 [" M x4 _0 ?7 W" _ - }
5 P+ O" t' y& m. }. K -
& x% D( m: h M1 C' D4 G - if ( get_user_option('rich_editing') == 'true' ) {4 P* u k/ C' S0 I T, l3 G {% J
- add_filter( 'mce_external_plugins', 'add_plugin' );
: D% f9 T8 B% y# I( A - add_filter( 'mce_buttons', 'register_button' );9 { B6 {! X# g0 I& W
- }
. @1 F2 A& l! K; n' p - }
銴鋆賭誨蝣 嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁
. {6 }7 R( G! R. A敺賣詨曉訖ordpress憪賣- add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣
" W$ f# s c& |2 Z! `& m5 X0 @4.3 冽
8 P2 C3 p" D0 G/ |, ^! ^6 d
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
! y/ W# o; `3 ^6 c t/ U9 _
暺嚗敶箏閰望霈頛詨交蝡賊嚗
+ E: ^% I' a& V6 R! j
頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
" u4 ~$ k0 r; M: f$ O 貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
1 e, K/ ]0 |6 l6 V6 q
唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆 |
|