WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)
$ \- v) }# v/ G" n+ k0 H4 C2 R1 r0 C& ]/ Y; M O) ^
wordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐
0 d% M0 F, z3 D隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具9 ?: p8 h& @/ F0 g
) j" A. i' w- j$ ]
凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :$ }% l, ~4 c$ M3 i+ e! f4 Q
甇文 , 臭誑閮剖銝訾批嗆蝡賊 :
3 [1 D# L' J) m; x+ L# [, [ v Z甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :0 h6 v1 g. ^3 N. n v8 h# B( D
- [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣
& A6 d1 L( K+ u
! ~% U$ V( s& i" R6 E' |% [1. 蝪∪桃凋誨蝣! _) e# ^3 w0 R: j- v: m4 z
祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :$ @; g1 Z1 d1 Y- v& @6 ~
閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :
. F) l" ^! X ?& D7 G- 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
- 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
- 閮餃賣賊蝯針ordpressaction銝% i5 R: C N, U* M# \
7 ?4 b' R; E9 Z; W3 y* O9 b祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准! c8 e& `( p, w& y
1.1 撱箇餈賣1 m; z/ O# i4 p$ g1 U
嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡4 m, i% K( t. `8 C& `
- function recent_posts_function() {# S1 |( G* i i9 k
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));
) T6 T8 h. ~, y* x7 l3 L - if (have_posts()) : @* V6 l4 [8 [, I
- while (have_posts()) : the_post();% j8 ~& _) m4 m; O2 g- V$ O" l
- $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';2 ]" s) a, G; m' X) `* c8 t
- endwhile;; g% x) R# U0 m# O
- endif;6 Y) x s/ z" D& I1 t( B/ t! [( _
- wp_reset_query();0 W* f/ {) d. _! }. k3 a8 P
- return $return_string;% N2 _% H) ^# q( s/ h; i. d; D
- }
銴鋆賭誨蝣 憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯; `5 u. {6 g. a) X* g8 N" @- ^9 q
# Z" F) W& f0 M/ Q% P& l
. @. {5 r3 m5 \& k8 O% k& t
1.2 閮餃凋誨蝣/ o0 n3 z$ s8 Q) @$ i5 T0 j
曉剁閮幃ordpress賣豢臭凋誨蝣潘- function register_shortcodes(){
8 X! v, i; W& j% U - add_shortcode('recent-posts', 'recent_posts_function');
8 ], L9 {& z6 L2 f& o0 z - }
銴鋆賭誨蝣 嗅冽蝡批捆銝凋蝙函凋誨蝣( D" a" Q7 @; x' A9 h
撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴
9 d. G0 p9 w. U1 N$ w7 j) F( F+ x( B
! V" \5 y$ _7 ~! G F/ e. x2 Y1.3 Hook into WordPress) g1 ^2 n1 u/ v- T. n
; n* c+ r, g' r. O, V5 W9 m
箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准6 x# n7 }! w1 `& L0 W
- add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣
2 k" l Z+ \+ b" ]* _; Z1.4 皜祈岫凋誨蝣! S. Z5 N. c& c, i/ s7 {
蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :2. 脤凋誨蝣 2.1 凋誨蝣澆 凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻 閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼 撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 : - function recent_posts_function($atts){
3 r9 z* Y1 \* O+ S8 {. s - extract(shortcode_atts(array(
. V) F: @3 t) e* B2 S - 'posts' => 1,# n( z& |2 } L7 ~! W
- ), $atts));7 I+ \8 D3 L) p P; E# g2 a
- 8 {. x4 l5 m* T5 p6 A/ w
- $return_string = '<ul>';" B# V" d5 |: w$ m+ q4 P+ j
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));! [7 R, m& ?5 o! f. Q- A& \& Q
- if (have_posts()) :
) @# @" z! Q4 E% X0 h4 F6 [ - while (have_posts()) : the_post();
6 N( C2 P& X6 k8 a A( Q4 I - $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
- y- f. Y/ P- C - endwhile;' o1 ~5 x Z. d" [' p/ D9 ?
- endif;8 D1 J+ j; ^# A: m# K+ V5 S
- $return_string .= '</ul>';
0 j% j! C& J7 ] - 3 T: M" `9 x( J. V' A4 K) s; Q0 ]* B
- wp_reset_query();
2 I+ ]# N& x0 ^ - return $return_string;
1 Z/ m: n8 d- T$ v - }
銴鋆賭誨蝣 憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :
$ x4 A# @- t: s _7 v" [2 b2.2 凋誨蝣潔葉憓摰
臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗 - function recent_posts_function($atts, $content = null) {2 H: z3 d; H- u+ A) o+ l9 v
- extract(shortcode_atts(array(
4 s- v2 t3 X0 y# V- Y2 f - 'posts' => 1,5 W% A8 K: K( a- i/ f2 v+ t
- ), $atts));
- W) v" j( ?: B( y) u -
1 t& i# r( H* |+ K/ n - $return_string = '<h3>'.$content.'</h3>';4 Z" R" ^: V, C: @$ q
- $return_string .= '<ul>';8 y0 k( j3 u* |2 E6 C0 f$ K$ T
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
* M( l2 j5 e9 U9 \4 u. T: Y - if (have_posts()) :
9 D/ `# B, F& M/ [ - while (have_posts()) : the_post();
f: A/ M9 I! F3 i/ U - $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
* C% ]" O" w0 {# t3 r( k V - endwhile;+ ^9 a# O, J+ {7 k2 h# |0 }
- endif;4 e# T) N6 L- T/ U
- $return_string .= '</ul>';
* Y2 h' l+ D& {: A -
, C& K4 x) n. X& } - wp_reset_query();, M- r/ v2 _- ?" `% X- f, W1 S
- return $return_string;
2 u$ J# G- e0 h# v: v$ H - }
銴鋆賭誨蝣 蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝- [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣 支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗
% Q+ _) n5 R- w3. 其遙雿唳寥*蝷箇凋誨蝣 3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣 閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛嚗典臭誑撠甇斤撘蝣潭曉典撌亙 6 G; [2 ]+ g7 H0 @( ]6 l' z, p
臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 : - add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣 曉其閬靽格嫣遙雿嗡唳 , 瑟, 湧甈撠甇蝣箏圈*蝷 9 v# ]( C1 y8 `+ q
璅嚗刻隢銝剖函凋誨蝣潘 - add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣 冽閬銝剖函凋誨蝣潘- add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣 4. TinyMCE蝺刻摩典凋誨蝣潭
* C3 T$ M- v- ~
* W5 j9 Z( |: O6 ?嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗 - 箸迨撱箇銝 JS 瑼獢
- 閮餃JS隞
1 U3 n8 _# q! u/ _: E7 ?
* q" e$ W9 h# F8 a- w' X. F8 I, h4.1 JS隞嗡撱箇JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗 - (function() {( @6 u; W! N- F6 ]0 d. e
- tinymce.create('tinymce.plugins.recentposts', {
. `# O8 c8 A: I, i - init : function(ed, url) {9 ^$ t2 ^# R2 Z. [; d
- ed.addButton('recentposts', {
# z5 ]- x" ]! k8 e - title : 'Recent posts',
9 P& k# j+ M' l4 ?! j( ~5 ^ - image : url+'/recentpostsbutton.png',& A' U) r6 B. H
- onclick : function() {$ J; n2 A5 h2 P1 o9 b4 v, b
- var posts = prompt("蝡賊", "1");) s* {! T8 Y9 c
- var text = prompt("隢憛怠亙銵冽憿", "唳蝡");. x2 N, s! c% |' @; X7 U
-
! W8 _& U( V9 l7 i, U! I - if (text != null && text != ''){
( N. [2 R& j) n9 c1 l. v% f3 z - if (posts != null && posts != '')
& D. v! e7 g% T - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');
, H" b+ ?. R6 @" D - else: o6 T3 ^8 t0 R) Z# }5 Q
- ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');9 {1 D6 {' u+ p K W. o2 s
- }! V* T% {! Y' V0 z, ?- I
- else{" k$ s( h/ g" c' U( G4 v9 i: f# N% k
- if (posts != null && posts != '')
) O; U3 a, o5 R! ] O - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');
! E# _3 c8 E" ~7 t5 v- [+ n: X, q4 \ - else! i- C: c# v5 S
- ed.execCommand('mceInsertContent', false, '[recent-posts]');
* T, d* q" `6 E% z1 S - }' J2 E6 ^6 d& ?5 Y- T3 E) q4 m: j
- }6 C5 Q# {+ e( S0 S* D
- });
! A4 n4 t) Z6 E) ? - },& A3 P: y1 I- r$ j- G8 Q6 k5 U
- createControl : function(n, cm) {
0 K* R" g2 y# G: E1 y6 `0 m - return null;
' |! c4 a# Y5 y$ I" v8 J6 @& q - },
" P0 H. M( c( A$ U; C: K1 J - getInfo : function() {
" \0 \% r# D6 V3 ~4 k - return {1 |' ]7 f& Z4 n' g
- longname : "Recent Posts",- Z* I) Q8 e7 l: A# p5 p7 d
- author : 'Specs',0 E3 K, l2 p7 W# j) s
- authorurl : 'http://9iphp.com',5 m) Q% R9 t$ j2 H
- infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',
! q+ r3 ?+ \. S0 {5 }1 ~0 u [; E8 c. _ - version : "1.0"2 d* w0 n" \( Q0 b1 v
- };. [& c: T( V( v# I
- }
' P3 c5 h W3 X - });
1 @( V% @5 m' F7 p% s- B0 { - tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);$ r2 H& q# u( c ~! f. }, E
- })();
銴鋆賭誨蝣 澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨 onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉 敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准
( Z w. x$ i, G2 a6 Y, W
4.2 閮餃TinyMCE隞
$ [4 r6 o* N1 ^1 S3 W2 Y1 v* b Q
0 Z8 W' L9 K9 [: ^2 I" `+ Y 嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉 蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩 - function register_button( $buttons ) {* I$ }* J5 N" b0 }
- array_push( $buttons, "|", "recentposts" );
! _& g9 J# A; k/ q - return $buttons;
4 _; y, c& v9 J5 }- M3 b- @1 }, d1 @ - }
銴鋆賭誨蝣 蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅
8 n/ l7 V8 Q6 d- function add_plugin( $plugin_array ) {
% i0 F; ]% Z2 L9 l6 C$ N; q - $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';; a( w& B$ `5 L/ D# P* g$ g
- return $plugin_array;
$ X4 @/ v% ^5 V' q0 j! I( f3 }2 P - }
銴鋆賭誨蝣 銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁! o4 w1 d; Z% |, m
- function my_recent_posts_button() { 5 [" f4 l5 k+ _6 ?. x W" [! c) G
- if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {1 E& d+ u: c) i& ^8 H! T
- return;
# @' }0 `; Y/ Y' z - }
+ b7 f9 i& l9 ?% Q& Q B -
. a2 o- d# J& z+ t; x - if ( get_user_option('rich_editing') == 'true' ) {
# r0 T' M& R( v+ S) t! ] - add_filter( 'mce_external_plugins', 'add_plugin' );1 j& U; @! o: G7 \; }; ^
- add_filter( 'mce_buttons', 'register_button' );' ^+ m+ ?8 Y8 v" ]6 [7 s3 j2 k$ r
- }
; ]: n) v; l- a; G$ p- D - }
銴鋆賭誨蝣 嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁9 ]9 h+ x& I) J1 z. e
敺賣詨曉訖ordpress憪賣- add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣 & p/ Y& B. @) h; }
4.3 冽
3 N+ B% Y/ w) U2 \' ~
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
" p- }3 h7 o7 E! k8 E 暺嚗敶箏閰望霈頛詨交蝡賊嚗
2 ^/ Z7 J# {) i7 p
頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
. K0 K. G* @4 P" U 貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
4 y z2 Z9 l; }8 H1 [ 唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆 |
|