WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)8 _" a; O6 T5 W7 J* m
6 F' c' {( l" l. s* gwordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐6 [; y g8 B- c; ^! @
隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具
. c# _7 X+ y$ y! Y8 I
: x t" r+ a' o& e凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :
+ Q) q5 z6 }9 _甇文 , 臭誑閮剖銝訾批嗆蝡賊 :
! ~% l8 g, h7 P- `$ A- L1 ?4 Y甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :2 M7 X/ Y! c9 z
- [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣
/ E f# h. r; E2 \" n% H# | F, z7 M% o8 Q+ @2 Q! C
1. 蝪∪桃凋誨蝣
5 l; c3 ]% ^3 a2 v祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :
, U% ?& E3 _3 I3 X+ [2 B閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :, O V' |! h3 Q: R; \/ |9 Q+ c3 `
- 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
- 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
- 閮餃賣賊蝯針ordpressaction銝! L# Q) K8 k9 B# W+ u
, w q4 j$ R* I* ?% R8 ?2 F# Z. g祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准* Y/ H9 V* w2 b
1.1 撱箇餈賣& l# A/ j, y, l3 d3 h; U
嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡
; @; Z- ^9 C( ^5 D G6 o- function recent_posts_function() {+ l" o9 O- r" L4 F- B
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));) z, x5 i8 }. S- z( O3 D! Q
- if (have_posts()) :- K: ^# B+ A) [2 [8 k# B0 N' M# p1 }
- while (have_posts()) : the_post();
# b( A& c( @ b% T1 Y - $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';/ j- Y$ x2 B, T
- endwhile;
K' z! c" g6 e5 u! | - endif;; u6 ~' V8 o2 f+ \# L) {* b0 I
- wp_reset_query();
" ~9 X2 x7 O L0 _ L+ X+ y8 _ - return $return_string;9 F0 m0 E! F# H3 b! t# J
- }
銴鋆賭誨蝣 憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯
2 ~& O# n/ R6 X: w% S: j9 H' ]9 h
& U8 N, w, l2 J. `, z& f/ {0 U% t+ _1.2 閮餃凋誨蝣
' h7 A7 I V+ s6 j' k曉剁閮幃ordpress賣豢臭凋誨蝣潘- function register_shortcodes(){
P/ _/ t& @1 Y$ m- A3 @9 | d8 ~/ Y - add_shortcode('recent-posts', 'recent_posts_function');
' r8 B' O1 R5 v/ s - }
銴鋆賭誨蝣 嗅冽蝡批捆銝凋蝙函凋誨蝣
8 F) Q8 H3 X8 t撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴) X" f4 _- u" v8 Z: p6 |* ]
, M! E6 |# m% n9 [/ m& t& [" c- ~2 o1 N) A" N+ J8 a5 S
1.3 Hook into WordPress
$ ~$ i n. b7 i8 u3 a- c% r5 O) F$ o- p: \
箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准
6 F- u& \! {, f0 i2 n0 b- add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣 + L# t1 X( b8 P( S
1.4 皜祈岫凋誨蝣" ?# a% ~9 T1 J9 S. s
蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :2. 脤凋誨蝣 2.1 凋誨蝣澆 凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻 閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼 撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 : - function recent_posts_function($atts){
3 ^- I h6 l! ]% \; c - extract(shortcode_atts(array(
6 A: ]( F- V0 y$ J7 I( [. h - 'posts' => 1,
+ q( E9 |! [2 c; e1 i - ), $atts));5 X% M. E# E$ e8 ~0 X
- " ]3 I' j3 Q( y; d _0 K0 b6 M
- $return_string = '<ul>';
6 B& D: G+ | J( a' F - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));1 E( Y: l0 M: t, m" ]7 x
- if (have_posts()) :8 v0 b. @! t! Q4 c/ [5 R
- while (have_posts()) : the_post();; ]1 M3 n! a' E$ ]
- $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';7 g% N1 m6 v5 X( G u
- endwhile;# A$ P+ L2 q( b. @- _
- endif;
' L" o2 ?6 Y% u) }: P. s& { - $return_string .= '</ul>';
# a- s+ ?" O" i( ]% [ - " w7 u) e5 F8 N" a; G4 t- x
- wp_reset_query();
1 {! b! `7 \3 N8 x$ y; h* O# a - return $return_string;$ I' U O( x' j$ w) l
- }
銴鋆賭誨蝣 憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 : V4 _) A3 ?* M4 G4 W7 n6 a
2.2 凋誨蝣潔葉憓摰 臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗 - function recent_posts_function($atts, $content = null) {4 q* z5 D; A; o( \' H
- extract(shortcode_atts(array(& s9 p9 K6 [+ t& b4 ^
- 'posts' => 1,
9 D9 G6 P/ U: z( ~# q9 ` - ), $atts));, m5 Z' ~" N; d- d" @
- * {" ?2 T2 J! c! J5 w* }) J
- $return_string = '<h3>'.$content.'</h3>';
9 t' ]- d+ X/ b* g: u0 m - $return_string .= '<ul>';) d! ]4 ]+ |" l6 c/ K, S5 ~
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));' t& S. a+ ^% z+ Q* Y* g
- if (have_posts()) :
1 v- M0 U. x0 X' w E, e6 M - while (have_posts()) : the_post();/ W3 ]- N* D9 F4 X: b
- $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
6 i* q# [ p/ f - endwhile;
0 w; ?( F/ l" m) G - endif;$ l) @) {. F" f
- $return_string .= '</ul>'; y% t* j3 Y- I2 ^
- Z/ y5 q6 }$ m1 o& t
- wp_reset_query();8 R6 ]* {/ u4 q
- return $return_string;- _3 k& ^8 e. s z
- }
銴鋆賭誨蝣 蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝- [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣 支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗
1 ^9 h- v$ {% S3 h- P/ a' A% h3. 其遙雿唳寥*蝷箇凋誨蝣 3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣 閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛嚗典臭誑撠甇斤撘蝣潭曉典撌亙
a7 d# r0 N6 \臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 : - add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣 曉其閬靽格嫣遙雿嗡唳 , 瑟, 湧甈撠甇蝣箏圈*蝷 4 }$ U7 M/ S& \7 y
璅嚗刻隢銝剖函凋誨蝣潘 - add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣 冽閬銝剖函凋誨蝣潘- add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣 4. TinyMCE蝺刻摩典凋誨蝣潭! o( b' w# |2 [6 i. K3 m
- D( s4 F( P4 q3 P0 P% I嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗 - 箸迨撱箇銝 JS 瑼獢
- 閮餃JS隞$ Q8 n* t/ c g. F7 T/ M7 v
$ | u3 b, [) | X* B. X# }4.1 JS隞嗡撱箇JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗 - (function() { G {2 h; e' m
- tinymce.create('tinymce.plugins.recentposts', {6 \6 m* b, i# @* t2 N
- init : function(ed, url) {! h& K4 ^! B# ^+ ~/ a% R" L
- ed.addButton('recentposts', {+ Y( g* R. ~% s6 p) W: Q
- title : 'Recent posts',
* V: f8 T& n5 ^" v8 r% @ - image : url+'/recentpostsbutton.png',
& Q6 T, F) n* K4 @ R: A3 z3 P - onclick : function() {
8 N! Y9 Z' H; ]5 r0 U' ?( |- | - var posts = prompt("蝡賊", "1");$ v9 }: h% ~ V# |1 b
- var text = prompt("隢憛怠亙銵冽憿", "唳蝡");2 q7 O0 Y$ g2 k7 ~; P
-
* m. S% s# H3 |6 ?- d - if (text != null && text != ''){
* @* q: a& ~$ V) i- w - if (posts != null && posts != '')
2 Z: u# ?$ S# Y3 `/ H' f" }7 V& u - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');
: F9 ]2 _5 D, y - else7 p: Q# ?. [ N2 Z6 k: Q1 m: ~
- ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');
. ~( s& g2 U, c# l1 J, {1 i* | - }. i; p% G1 X h( `( k# J, E$ j
- else{# [& h& y$ y3 n' W, |# h" Q4 v
- if (posts != null && posts != '')
0 O$ t4 i p8 u. s, n - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'); {8 F2 h( E) K6 u0 M
- else4 I% o* f( i6 I1 s$ O T% t
- ed.execCommand('mceInsertContent', false, '[recent-posts]');9 J4 W* I* z* v' J4 J+ U& t# z" c
- }: |; d' q* W; x) U( W7 B/ Q" `4 X
- }$ h; a" _& @5 T9 g9 |
- });
" j; }" Y9 V. W$ f9 E - },- b/ `8 Y: a% [" g
- createControl : function(n, cm) {
. ]4 R7 r6 Q' H - return null;' r6 X1 X: O: I4 u
- },
; ]1 q |% L! a7 m& H* ~ @8 v - getInfo : function() {
4 x8 y2 j ]$ a8 @ - return {
+ i/ R0 B0 Q+ G1 W4 A - longname : "Recent Posts",
: Q6 X( C$ X- N4 q - author : 'Specs',
9 L# m i$ @0 K) M, S - authorurl : 'http://9iphp.com',
. Z. C: u) t) U+ P - infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',$ V; B2 \# |+ s2 r0 l8 l' k
- version : "1.0"% L1 J! }- x- I/ ~1 k& v3 {: Z
- };
4 V8 A, v1 S: V! c. r; Z0 @5 A7 D - }0 E: e' A0 q& z. v4 \7 Z7 B
- });
1 b* @; \0 X8 M) Z% F. `1 D' M6 u- r - tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);& \, w) m |8 g- N. U5 G7 N
- })();
銴鋆賭誨蝣 澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨 onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉 敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准
' [5 |8 f8 j1 x& M! b' ^8 I
4.2 閮餃TinyMCE隞. J1 w5 x( {; ~9 B" L# t- ~
; C$ k1 H6 S8 f
嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉 蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩 - function register_button( $buttons ) {/ D0 U5 m( }; N" N) i I# i0 }4 q
- array_push( $buttons, "|", "recentposts" );$ `' n# M# p) M0 Y
- return $buttons;
. F; t- H+ V" N2 d$ V/ { - }
銴鋆賭誨蝣 蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅
* {; m5 A( P8 x0 n, K' m6 p- function add_plugin( $plugin_array ) {1 f2 A! M+ a7 {, P$ y5 `) r: V; d5 J
- $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js'; z! }8 K B0 D" v+ ~/ J
- return $plugin_array;! }5 ^+ G9 s9 K5 y
- }
銴鋆賭誨蝣 銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁( Q2 h, {# `9 A1 {' F: l! c
- function my_recent_posts_button() { . |; k" Z3 m0 g( B" P1 i, f/ n
- if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {
3 j, a# |+ \. k - return;
( b! ]5 x: ~8 w7 A) S* @" f - }
# f5 T; R9 Y- ^1 q9 S' k. b -
- O! n- h4 J! U$ g6 n - if ( get_user_option('rich_editing') == 'true' ) {
+ b8 W2 y0 ]& @6 s) T2 i - add_filter( 'mce_external_plugins', 'add_plugin' ); n6 @! g0 R! ^* I0 \9 t
- add_filter( 'mce_buttons', 'register_button' );' s- U% R; ~1 J+ J/ D
- } 8 J A. i) B4 H: r9 V
- }
銴鋆賭誨蝣 嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁+ r% t3 [8 e* X6 @$ O3 p; P1 I
敺賣詨曉訖ordpress憪賣- add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣
$ I5 H1 b9 W' V# i9 b5 T1 ?4.3 冽7 k+ @1 q; X7 m9 |# i1 t
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
; J( A$ }: I) x$ N6 G6 b 暺嚗敶箏閰望霈頛詨交蝡賊嚗
" B( m8 G% N1 `. A5 h 頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
C: w+ }# ?) X
貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
$ }6 L, T" ^* ~0 _3 T
唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆 |
|