WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)' O1 n. D3 D+ `
8 x' e3 }8 @! V% x0 g k
wordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐/ c5 ^1 v- N% H/ U+ ]
隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具
! L; H) s+ ?# W* F: D0 w( Z J8 u4 X" \# X6 r$ o
凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :% Y. X6 V9 u) C7 F0 s) v
甇文 , 臭誑閮剖銝訾批嗆蝡賊 :
. n8 r# Q B5 S甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :
/ ~ g8 p; C* z8 g: H- [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣
- ~8 p/ k4 p2 i8 r' C
. s3 [2 L9 r& _) R1. 蝪∪桃凋誨蝣$ w* S; V9 `. M6 n7 z& o8 ~; [
祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :" l$ b; F1 }8 i& q; {8 S- x
閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :
; f+ {7 E% q5 ?1 a- 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
- 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
- 閮餃賣賊蝯針ordpressaction銝4 o/ i. ?& X$ p8 D. A
* |6 d! T% q5 B2 X+ P% B祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准" `( K8 c" `! C5 U
1.1 撱箇餈賣
1 O$ `) ~" H- b嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡
: |" G+ ^' S4 k" r1 F5 b- function recent_posts_function() {
& E' }/ o6 w* I6 v% t" y - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));" S k1 r+ F( k6 z; x
- if (have_posts()) :! H; A& P5 H1 s. c+ s4 k( u
- while (have_posts()) : the_post();% u: o: d( b4 X0 e* i: t7 w
- $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';: L b! H" [5 |3 ~8 d( V8 [1 v
- endwhile;
+ o- q) M8 U3 _" `+ m1 `" E - endif;% H! Y! i# c3 E5 o7 w/ B' C F9 H
- wp_reset_query();& F7 ~/ w$ A! u
- return $return_string;
" D& \ n! _6 u2 K" p- a# l2 c* n! [# w - }
銴鋆賭誨蝣 憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯
& ~- p) I f- c6 ~. ^5 l/ T% {% O9 \" J W' [, S3 g
7 V ~& C% k X- K3 D! Q1.2 閮餃凋誨蝣9 f; K0 R9 {9 X$ G/ h
曉剁閮幃ordpress賣豢臭凋誨蝣潘- function register_shortcodes(){
4 Q7 _& [# K; C/ s0 C - add_shortcode('recent-posts', 'recent_posts_function');: e9 e8 `, i+ q, J
- }
銴鋆賭誨蝣 嗅冽蝡批捆銝凋蝙函凋誨蝣1 k4 ~! B0 a. n. M) ^3 g
撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴; X& \8 v9 o3 s. v' `
$ v, ~$ e' N0 N1 ^
$ Z& ~5 [, \2 Y4 f
1.3 Hook into WordPress
4 J4 a; K" }% T8 @
5 J: X* A7 W4 Z6 ]5 J! W箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准
* b2 e7 ]0 T/ v: e7 \+ N- add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣 : j' ]$ ]3 R) Q6 S1 X
1.4 皜祈岫凋誨蝣
: `3 S3 f3 k) W4 o5 B蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :2. 脤凋誨蝣 2.1 凋誨蝣澆 凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻 閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼 撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 : - function recent_posts_function($atts){1 e1 H$ ^3 b1 O' p) F. k b
- extract(shortcode_atts(array() P {0 J+ q- D3 }; M/ G
- 'posts' => 1,
* j* x2 Z1 X$ g - ), $atts));% p3 T: o: {; A; J) c. u; H
-
# U6 t( S! s+ ? v1 E - $return_string = '<ul>';
8 b" ]" p! C) J# X' _0 p: u: I - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
* T& x& X( y4 w - if (have_posts()) :- J- B! y8 q$ k
- while (have_posts()) : the_post();
' B* E9 X i) v - $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
5 S& E! s, s* l( e; ] - endwhile;8 g3 W( {- M: J
- endif;
& H2 Q8 R* O7 Y; T" @: M - $return_string .= '</ul>';
, o$ r3 U0 h: m$ g" R# c* n - . A' f0 y9 C B- B- R) y) z+ \
- wp_reset_query();. ~. P- x3 U D( [% s% v
- return $return_string;9 V7 J3 ~) `, j
- }
銴鋆賭誨蝣 憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :9 ]; x- O' X0 l$ [5 t
2.2 凋誨蝣潔葉憓摰 臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗 - function recent_posts_function($atts, $content = null) {
. C$ m! q7 I0 T* w0 Q" B - extract(shortcode_atts(array(
: b/ N8 w' U+ O7 Q5 i# B! j - 'posts' => 1,
9 d J1 z) Y; q6 t3 S6 V! ?5 n - ), $atts));
' w' j$ Q, |0 R5 D$ e1 J8 X -
) j! K. k7 n& z7 D5 U: k - $return_string = '<h3>'.$content.'</h3>';
5 ^! t% W8 n8 O3 g1 h, w - $return_string .= '<ul>';
; b3 u5 K! l3 R) M8 ^ - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
6 v, ]6 u" U8 j& c5 `+ T3 t - if (have_posts()) :" n0 l6 Y* J& f X$ T2 o; D2 _
- while (have_posts()) : the_post();) S: V2 s/ K5 m0 {+ y8 n) v* U8 o
- $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
0 C+ \8 p) Y8 z/ b - endwhile;
. T6 d7 p2 b9 k! e6 g0 Y - endif;
, G- I7 R$ i: o/ b - $return_string .= '</ul>';
/ C5 m. p }3 J. [1 R - ) |; i! ]! v2 r5 v+ a
- wp_reset_query();
, G1 x! E; W- {! Y& z4 F. q - return $return_string;! Z7 M0 J; U% j5 [( c) D. u7 n
- }
銴鋆賭誨蝣 蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝- [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣 支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗7 \% I4 X4 `7 o
3. 其遙雿唳寥*蝷箇凋誨蝣 3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣 閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛嚗典臭誑撠甇斤撘蝣潭曉典撌亙 # v, Y" b1 {! e/ i0 }8 a
臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 : - add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣 曉其閬靽格嫣遙雿嗡唳 , 瑟, 湧甈撠甇蝣箏圈*蝷 3 ` f4 S3 S4 `6 g5 T& k ?
璅嚗刻隢銝剖函凋誨蝣潘 - add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣 冽閬銝剖函凋誨蝣潘- add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣 4. TinyMCE蝺刻摩典凋誨蝣潭
4 ]! Z; x L8 S" T7 ]0 C8 w. P6 `, V2 _
嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗 - 箸迨撱箇銝 JS 瑼獢
- 閮餃JS隞" x& L1 v1 x1 U/ p0 u4 z& B
3 K) E4 f! h: s6 T0 t. d4 a# h! E
4.1 JS隞嗡撱箇JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗 - (function() {
6 M* |" [& e2 R: ~) l) C2 a - tinymce.create('tinymce.plugins.recentposts', {
S7 S4 e5 Q X1 c2 |5 M' D# a9 f - init : function(ed, url) {0 T# K) x$ M* T# C$ R6 P8 S% H, V
- ed.addButton('recentposts', {5 u0 l' p9 v6 Z* Z
- title : 'Recent posts',
( _; m3 j9 G }2 p5 ~ - image : url+'/recentpostsbutton.png',! k( J) g( }% C, y/ w: M
- onclick : function() {; p, m/ u" `) }4 g
- var posts = prompt("蝡賊", "1");
/ A) T1 C6 p% w& a" v7 O: b# K1 t( c - var text = prompt("隢憛怠亙銵冽憿", "唳蝡");
. |' i, G; L' m4 } -
: a( q9 l. L; b - if (text != null && text != ''){
& x- M9 n. U4 X: N0 x) }% E' T - if (posts != null && posts != '')
1 F% n* s, A% L1 g# u: P - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');
/ o$ h) _: x& B% K. p - else
0 j$ U0 W1 D: N7 f7 u - ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');
" e* ], n+ ^2 {* x' | - }
! T2 g2 I4 T5 y( c5 z& y - else{
3 N9 ]" x( r, L$ K9 k - if (posts != null && posts != '')
Z' U2 w, K% `. S$ } - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');
. L: @" g; L, H- E$ c - else
( \% l$ \1 n3 e0 y5 A" Z - ed.execCommand('mceInsertContent', false, '[recent-posts]');+ R' t9 `# I5 F N
- }
5 { n1 Z8 Q" E/ s! s; M - }4 L4 m# I' z/ x8 z1 x9 H
- });# v' f& g2 e. o. F) }& j& h
- },5 J! W6 f$ e9 L3 {0 w: {
- createControl : function(n, cm) {2 X) m$ }! W6 q3 T* L
- return null;; D4 s; `, B3 V- p. S% e* A, o
- },
5 d1 ~8 I( M8 d2 I2 q# C - getInfo : function() {
5 y+ ~/ H; V1 K - return {# F: _7 h- \8 v! x5 j+ ]1 s8 w0 A
- longname : "Recent Posts",; H- E) ~7 ^6 y5 ~3 a1 Q1 _
- author : 'Specs',
$ Q( p0 H& k' H - authorurl : 'http://9iphp.com',' Q8 H8 h) `" Y) M) Y' L
- infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',/ ? u1 U u- ^9 P/ h
- version : "1.0"
$ ^/ V I2 Z8 W9 h W' C; f - };. D8 X" {. ` N9 g( y7 z& y% o
- }
0 ]6 q, }5 V/ D! W - });3 L1 y4 G# g! F
- tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);
$ e. ]( E+ G5 H - })();
銴鋆賭誨蝣 澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨 onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉 敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准 : B, [0 F. v) q
4.2 閮餃TinyMCE隞! Y- \ a4 g" U; s# n
. V. O" x& u: i7 Y/ o3 o 嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉 蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩 - function register_button( $buttons ) {
% D* z! z- y. d& v9 b - array_push( $buttons, "|", "recentposts" );% W% r& X: r {1 ]2 [& ^4 @
- return $buttons;, h9 c% ?6 j( f7 S' z# N% `$ v
- }
銴鋆賭誨蝣 蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅8 W7 E) ]7 n6 C( [/ W
- function add_plugin( $plugin_array ) {
4 p4 J6 h& M+ d+ ?/ p5 U7 x - $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';) I J) d; i4 L; A6 G+ I
- return $plugin_array;
1 s' c6 D- _" X) e1 H. M2 J; |. e - }
銴鋆賭誨蝣 銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁, q% W3 n- F7 b6 {9 j
- function my_recent_posts_button() { + H( d7 O1 H" t+ T. b* F4 s" a8 R
- if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {
5 D6 c; O& ?4 g2 a. S% X8 N - return;
* n8 V6 o6 }9 r! }& S - }
( t9 q% P6 E" M# P* G -
1 q0 g9 [+ P# }4 o# ?7 S - if ( get_user_option('rich_editing') == 'true' ) {/ R9 E) K D: ^8 U: k
- add_filter( 'mce_external_plugins', 'add_plugin' );( G+ g( O) { g! J$ z4 x
- add_filter( 'mce_buttons', 'register_button' );
7 l2 O8 K; p; D0 a( V2 ~- s: o* B - } $ T8 a H( x8 A, t
- }
銴鋆賭誨蝣 嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁" }( |0 B3 h. n+ m; B7 x: ]
敺賣詨曉訖ordpress憪賣- add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣 & R% P) q, O; j1 A- e
4.3 冽
0 F( h6 ^9 T2 Z8 }
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
1 h: L: X3 m+ Z9 K2 F0 [
暺嚗敶箏閰望霈頛詨交蝡賊嚗
9 z4 x0 ~8 d0 g9 S" _0 D 頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
: Y/ x- |# {1 f; q/ y 貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
- N6 ~& n3 B4 _
唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆 |
|