WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)
2 b) [* u5 L% F' m" O8 d* A) K$ W1 h+ _: A! K( J$ l
wordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐) Y6 j4 g3 X+ M
隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具
$ d# Q' b. f- l
) {' R" I" d5 R' `7 C7 r+ T; J& p9 S凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :
m, |& e9 I. _ U5 V1 U甇文 , 臭誑閮剖銝訾批嗆蝡賊 :9 Q. H0 x$ u! T. f5 L2 k" ~1 P4 Q
甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :
3 d% d9 p' H" [) \- [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣
# }) r0 N: z ^9 H7 r% l* \& Y3 `
1. 蝪∪桃凋誨蝣$ t; h: r. m' r' t( x* X
祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :
3 b& k1 j5 r& `3 \( P; }; t: g9 i4 R閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :/ z R5 V! \& Q; G, E/ i( o2 ^
- 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
- 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
- 閮餃賣賊蝯針ordpressaction銝4 V1 U2 [7 a; w: [# D
+ L/ e. @7 C9 B$ Y: d- P
祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准8 ?# F- h" d4 ` ?- a/ T/ [
1.1 撱箇餈賣" ?( V% y8 k- x
嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡+ z. Y, T3 w8 q: m I7 M
- function recent_posts_function() {) C/ C; f: D5 E; W1 k
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));
$ m. m" p4 a: P2 g - if (have_posts()) :8 ?& \2 U8 F: k$ J2 O
- while (have_posts()) : the_post(); k4 ?) _- L& e$ b; l4 V
- $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';
9 N) c H. Z8 e+ H2 ~- i - endwhile;0 N' f3 v/ j! d: A" Y- m
- endif;
8 |; V: O$ z' ~$ | - wp_reset_query();3 l, }+ p6 h: K$ c4 I7 q9 W+ l9 R& E
- return $return_string;
& M+ W! [. N, \- g N - }
銴鋆賭誨蝣 憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯( t6 F. W$ ]) \( {2 U
& H, S2 l8 B6 |/ H
- L. i3 u/ }& S, Y+ ~- H" ?& k1.2 閮餃凋誨蝣 x7 i' f; o; S. L# k+ e4 M5 g0 b+ u
曉剁閮幃ordpress賣豢臭凋誨蝣潘- function register_shortcodes(){
2 l$ J% l1 Q7 g6 J z$ M( c) @" b! T - add_shortcode('recent-posts', 'recent_posts_function');
. o5 {/ i- H8 z3 o$ f - }
銴鋆賭誨蝣 嗅冽蝡批捆銝凋蝙函凋誨蝣% Y2 [+ b* r O9 m7 ^" a
撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴
9 H" h+ y# h. I! a) k- L( a5 X
2 _/ O; f" S0 q7 }4 s& C- _. p5 A% \. H+ Z1 w8 G
1.3 Hook into WordPress
3 p$ A: i8 B6 z/ I& y7 K
' v ?& l+ _9 I, H$ N, L箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准
; x4 X5 S, O2 D5 h- add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣
6 J4 {5 C* x% `1.4 皜祈岫凋誨蝣% `6 D7 j- ~8 w0 H
蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :2. 脤凋誨蝣 2.1 凋誨蝣澆 凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻 閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼 撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 : - function recent_posts_function($atts){$ K6 V) @; p! l) x
- extract(shortcode_atts(array(8 Z( ~& G8 B7 g* F( ~' s1 c
- 'posts' => 1,
8 R2 d4 Y. k9 Y1 V5 q% U1 N - ), $atts));
2 Q# t' u/ j3 o/ q) n& x; z - . o1 D7 Y+ {7 M& y
- $return_string = '<ul>';4 }; b$ ~5 U6 d/ a
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
/ n1 e$ `' L& \7 x: H; [ - if (have_posts()) :
, B; q3 p& \. K& k) E- O - while (have_posts()) : the_post();2 T- D/ O1 W2 \% n ?$ i# k: a
- $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';7 n9 k. C$ X/ t
- endwhile;( Y: M9 U: t! O0 z5 O$ W- L# D( W
- endif;
+ `8 g* ?! F; K' {' s1 f' l4 y5 f - $return_string .= '</ul>';) P% ^# F% q: [8 i* a7 E" _- U
- / y3 s0 d6 @* p# v$ a
- wp_reset_query();
4 ?& e5 ]0 }$ A1 E% o0 j - return $return_string;9 Q, o8 g5 _ r* V
- }
銴鋆賭誨蝣 憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :. ? C, K$ T- M0 Z# E; c5 i* {2 r
2.2 凋誨蝣潔葉憓摰 臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗 - function recent_posts_function($atts, $content = null) {2 w2 J' n. w# d0 J. u, I6 _$ h
- extract(shortcode_atts(array(* G3 p8 |6 l1 c- [& X4 v: o
- 'posts' => 1,5 c% K+ P# G6 x ^2 j
- ), $atts));5 U' o+ D; y! J( g) S
-
/ _; r1 {, n1 N" H - $return_string = '<h3>'.$content.'</h3>';+ q: X0 Q2 i) q. @! d' i/ {0 e
- $return_string .= '<ul>';
* b" W/ v9 P9 ?& m; w - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
; `* r! l8 s' g0 F1 a - if (have_posts()) :
8 w) x: |5 E& H0 f7 d8 N& { - while (have_posts()) : the_post();
( i# s/ D! u" N' s - $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';# [4 M4 y6 \/ n! m
- endwhile;% W% C' F, y9 j. }
- endif;( d0 W+ }) v; ]9 l
- $return_string .= '</ul>';
! `5 w, E/ x, ? x; g/ X -
, N6 a& D) p5 A0 i$ R. H7 l - wp_reset_query();3 y7 U$ v) i0 n% x6 c$ u
- return $return_string;; B8 X U( g8 l
- }
銴鋆賭誨蝣 蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝- [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣 支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗
/ o# K4 m3 j/ J O, q3. 其遙雿唳寥*蝷箇凋誨蝣 3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣 閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛嚗典臭誑撠甇斤撘蝣潭曉典撌亙
) F9 D c. D% V# z臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 : - add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣 曉其閬靽格嫣遙雿嗡唳 , 瑟, 湧甈撠甇蝣箏圈*蝷
/ B* y5 f' r% e璅嚗刻隢銝剖函凋誨蝣潘 - add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣 冽閬銝剖函凋誨蝣潘- add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣 4. TinyMCE蝺刻摩典凋誨蝣潭
8 m1 O9 i7 i$ w4 K( g2 {1 W
$ ]3 b! {' n8 ?, i! b5 p嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗 - 箸迨撱箇銝 JS 瑼獢
- 閮餃JS隞, U1 Q- D$ ?8 f6 R5 w+ r4 u, l& v
/ Y9 J" ?4 C& m" r0 V
4.1 JS隞嗡撱箇JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗 - (function() {9 L# [( j" b+ j, ^+ ]% ^* r; M
- tinymce.create('tinymce.plugins.recentposts', {* x: y9 j6 z: k4 a8 G* @
- init : function(ed, url) {
$ f0 E% M4 @/ v5 K4 w: D3 A$ i - ed.addButton('recentposts', {
& Q& ?1 r W( J2 }7 z - title : 'Recent posts',
. M( Z/ \ m- k* N% X9 ~ - image : url+'/recentpostsbutton.png',+ \! ]2 s1 ~ r; q! b8 Q
- onclick : function() {7 b! ?+ G2 T" y5 X. ~% k3 L
- var posts = prompt("蝡賊", "1");8 j* W. m" {- m/ l5 L$ f
- var text = prompt("隢憛怠亙銵冽憿", "唳蝡");
& ~0 R. S& ]7 |9 z5 i% C1 t - / n3 {2 i9 n) X. z7 p* A9 D" ]- ~( h
- if (text != null && text != ''){( t( ]* r$ a* J$ v* W
- if (posts != null && posts != '')
4 o; ]0 s% }" U5 i! G4 E - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');. u4 m4 _+ [- i, R8 L
- else
4 _" E f. y4 _) G6 [6 b - ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');
' Q6 {9 ~- H% j3 E) q3 N% y! Z8 ] - }+ Z* T; w [0 [; f. b
- else{8 T2 W ^. r& f7 W5 O6 d+ K
- if (posts != null && posts != '')
- F; v8 M1 A( S- Q" _" U - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');+ m* d. e9 L9 v% b
- else
* \5 @9 B/ x' |2 W. ?( E- i - ed.execCommand('mceInsertContent', false, '[recent-posts]');$ F# s* E4 y3 x# g! g8 f
- }
. I4 I: i3 @' O& Q) h7 n9 u5 ~$ Z - }% h4 _% \) ~* \6 y/ Y+ W
- });
2 Z ^ ^( b' T, U( i - },
% b8 e0 a; C; \ - createControl : function(n, cm) {
8 Y& M0 \% y" n4 U: Z7 E0 }+ W+ w - return null;$ S ?9 s6 ]* ]; C7 K- Q
- },5 p3 K& z+ a) l
- getInfo : function() {
( m" E6 z0 N: g# o% R. ~/ w - return {9 U: J2 K5 h1 t0 G: w: A* [
- longname : "Recent Posts", L. ^5 e4 c$ R3 j
- author : 'Specs',3 G5 P$ `8 i. t4 I+ C# I
- authorurl : 'http://9iphp.com',
- K, P, J' W! c, b5 W - infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',
+ e! `; O. B+ E* ] - version : "1.0"
9 V* S$ K; E9 F* f. e( [, d5 a - };7 G- v9 ^- N* W. z" j" ?3 r, l- Z
- }8 S1 E6 z$ C, V
- });5 e: m3 N7 P) |$ R9 U9 N
- tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);
3 _+ Y# J p* X8 r3 l" L5 f - })();
銴鋆賭誨蝣 澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨 onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉 敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准
5 l- G8 o4 | k. q' m
4.2 閮餃TinyMCE隞( r+ |( }8 P G7 J3 |2 Q
9 Y& a7 p$ Z! H {1 w! S" { 嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉 蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩 - function register_button( $buttons ) {( {- G& B+ u) ]% C% K0 L. j: b
- array_push( $buttons, "|", "recentposts" );
0 h4 R! ]. a2 u, N5 U! T - return $buttons;! u! j3 o( g) A5 R& w& D: M
- }
銴鋆賭誨蝣 蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅% e$ ?" G* o( L& v, S
- function add_plugin( $plugin_array ) {
7 ?9 o+ F9 X G0 _- Q& J9 c: i - $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';9 X" d# Q% g- E: v
- return $plugin_array;
& A. d0 p( G! Q+ q* F - }
銴鋆賭誨蝣 銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁' H! T. Q' z* ~7 H; t( ?6 t8 g5 ^
- function my_recent_posts_button() {
, a* b& R/ }( [# N/ w - if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {& @1 w A! z' X# e" D
- return;( F; Y$ k; Z* ?; V3 F
- }+ _2 L! e7 q* a9 P; O0 ^5 \
- ; ~0 p1 J( k! y/ S6 N) t9 V
- if ( get_user_option('rich_editing') == 'true' ) {% `0 Y% D5 [( [; N' }, C
- add_filter( 'mce_external_plugins', 'add_plugin' ); q1 _; v a( A D
- add_filter( 'mce_buttons', 'register_button' );
' t3 q# T: N6 y7 V - } 4 _8 Y; G0 }6 \( o& Q, l
- }
銴鋆賭誨蝣 嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁5 x4 y$ `& |/ ~2 C! N0 @' ?, @
敺賣詨曉訖ordpress憪賣- add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣 9 e5 I6 g' D; |# k
4.3 冽
% j; _7 z1 i: Q3 Y. u" S
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
0 R7 L" g2 f# z9 O: U
暺嚗敶箏閰望霈頛詨交蝡賊嚗
) ]5 {5 {! T8 P$ [3 u- y# t7 w 頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
8 t6 \2 A* L# F' y' M
貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
. t8 t# v4 ^5 j7 A) t. R8 J! N
唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆 |
|