WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)5 E7 S! P0 h$ @& ~& o3 P @4 ?
' w- y5 W5 `; h' c7 P9 J" Z
wordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐% P* G( j3 k. |6 e. G' _
隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具
1 G9 \8 p+ a6 O4 J+ J
0 r. Q; g0 Z* P% n凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :
, l4 J$ L. [3 a- j. P! `0 i甇文 , 臭誑閮剖銝訾批嗆蝡賊 :& m9 U1 [- u, M+ V. S) v' ?
甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :1 X- C- e! D# R+ e
- [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣
" d7 X0 e' m6 E8 @, x
8 q+ j$ l% k: M3 A9 R; ^1. 蝪∪桃凋誨蝣( ^' H# l" r0 d: w7 L, I( C6 _' y
祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :
! i" t d0 Q; Z2 h7 l$ e- l閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :
# U! [9 k, ~* s$ {/ |- 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
- 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
- 閮餃賣賊蝯針ordpressaction銝- H" A4 T8 ?) g& w: U
; N. x3 y/ }+ ?祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准
$ C/ x# g4 Q! N6 L1.1 撱箇餈賣
' B/ S- D' Y7 H$ Y# q+ j嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡
- ]" Y; M% E- M! ^7 o- function recent_posts_function() {: w# ~8 ]$ F; x- J3 {& D: j
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));) V5 A4 R4 r; s4 F. J; R6 i
- if (have_posts()) :
) t4 p$ p6 k0 i2 ?4 c7 X - while (have_posts()) : the_post();
! K5 W1 e& n6 O0 w- g - $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';- b6 w* X+ d5 u6 ^! _ i
- endwhile;
; }8 j2 G+ l4 v- L - endif;
( S7 a7 C- r7 z, g9 A; |# M! s' }+ M: ?2 O - wp_reset_query();1 t% S" W: `2 a# A( O0 t& w) J( J
- return $return_string;# v k9 K! p/ O: _( f
- }
銴鋆賭誨蝣 憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯
- h' C( t C- R$ ^6 A7 W
/ D: @8 R) l0 V! Y
/ j7 ?" t0 h# E& y/ B& o1.2 閮餃凋誨蝣 \6 b( O3 L+ {7 g
曉剁閮幃ordpress賣豢臭凋誨蝣潘- function register_shortcodes(){4 W! Z6 C) m8 r5 _5 R
- add_shortcode('recent-posts', 'recent_posts_function');
6 F: R8 g% K* {* r% Q4 M$ t' P - }
銴鋆賭誨蝣 嗅冽蝡批捆銝凋蝙函凋誨蝣
% v+ Z `: Q. k X- Z: |6 F6 u- U撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴, ?) w& l3 h( A0 |' \* L& C
& n1 P/ ~* c7 f$ W5 B
% U" J2 |6 R- T( S$ v0 C: I
1.3 Hook into WordPress% a4 X& R# B/ d; g% Z* _0 c
6 b4 e2 C- ~7 [6 q箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准
; s+ W( Y7 f( c& T( y- add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣 : D; x% N' A! K8 f, n- v! u- `$ B8 F
1.4 皜祈岫凋誨蝣
% y. l3 f% Y4 K6 N& }! y3 W蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :2. 脤凋誨蝣 2.1 凋誨蝣澆 凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻 閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼 撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 : - function recent_posts_function($atts){
5 }! c* M6 K$ h9 p( Z) [ o* J - extract(shortcode_atts(array(
# m0 x Y7 g- B! U9 X - 'posts' => 1,2 C# U& ^, c) ~1 T2 i2 k) W2 z2 I
- ), $atts));
3 c; H8 g) c: V+ ` Y7 q - , Y6 \) y& q* }2 K
- $return_string = '<ul>';; P9 z2 X( d& @7 q. a% Q4 ~ B& V$ i
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
8 M5 w5 w" Z) j% ~" P - if (have_posts()) : b: b9 l2 d! R0 h% W5 D7 y
- while (have_posts()) : the_post();6 X! E! P1 z! q0 j6 [
- $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
0 s$ u( S0 u3 |) }: b - endwhile;( n8 E! B. t, ?$ _' ]/ T
- endif;# K. B0 b7 D( j( N% G2 Q4 D+ U
- $return_string .= '</ul>';
- D* c$ q) O) c. Y( K - $ A, x4 B: R& o7 i8 F8 K
- wp_reset_query();2 t# B: A% Y& c4 G
- return $return_string;
$ U* w$ I) s5 P- @/ g( n9 p8 h - }
銴鋆賭誨蝣 憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :
/ N5 n/ o* ~. W8 F- t- |7 K2.2 凋誨蝣潔葉憓摰
臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗 - function recent_posts_function($atts, $content = null) {: X) w9 a* `- @8 F1 u
- extract(shortcode_atts(array(0 a7 P/ D' E3 Z* M7 U
- 'posts' => 1,& d# Z% L$ D; Q
- ), $atts));
; l5 b& ?, R# ? - 7 D! G* {9 f- z' J& w/ [
- $return_string = '<h3>'.$content.'</h3>';
% j' J w Z1 O9 p - $return_string .= '<ul>';
- A- G4 G6 b7 L! n - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
8 r- N1 _& _0 H - if (have_posts()) :5 ~4 i) J& j! j
- while (have_posts()) : the_post();
5 m# w5 o5 H& G/ e1 s, |3 d: P. \ - $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
7 z0 x- p/ W; Z - endwhile;
- x3 i# q" y3 X# A6 q* t - endif;
) f } A6 |7 |9 s+ C. ?' S- [ T* ^ - $return_string .= '</ul>';8 X& x; K5 R) [ Y: U- a
-
& t7 a: ^! G# }! M9 X8 v - wp_reset_query();
8 C8 V" W% |& ^; K) V - return $return_string;/ B2 @- y0 [8 t( D5 Q
- }
銴鋆賭誨蝣 蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝- [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣 支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗
5 _% S1 N( k% Q, m4 O3. 其遙雿唳寥*蝷箇凋誨蝣 3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣 閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛嚗典臭誑撠甇斤撘蝣潭曉典撌亙
1 \$ d6 X, ~3 h; }臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 : - add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣 曉其閬靽格嫣遙雿嗡唳 , 瑟, 湧甈撠甇蝣箏圈*蝷 ' T5 I7 U& b) w$ w5 v4 n) ?' C
璅嚗刻隢銝剖函凋誨蝣潘 - add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣 冽閬銝剖函凋誨蝣潘- add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣 4. TinyMCE蝺刻摩典凋誨蝣潭1 r6 C3 a3 M# S) t N3 R. A" X) A
% w. W5 L. x* o4 \2 _嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗 - 箸迨撱箇銝 JS 瑼獢
- 閮餃JS隞' M; X q6 W+ X% h
. d, ~! H( z# f& N B: I7 u/ E4.1 JS隞嗡撱箇JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗 - (function() {+ }) A' `8 b2 U' A
- tinymce.create('tinymce.plugins.recentposts', {( D5 P" c: @4 l8 A; I
- init : function(ed, url) {
% h4 p6 ?' P; u& E+ | - ed.addButton('recentposts', {/ o9 ]' }- @3 }7 w+ P3 i+ E
- title : 'Recent posts',
2 N0 D& L- S: z. s4 h+ A1 s9 B2 s5 B - image : url+'/recentpostsbutton.png',
j) T8 G |, r9 P) k - onclick : function() {8 I5 [2 b' o* O7 W! W4 w V5 C# N
- var posts = prompt("蝡賊", "1");8 u$ W4 S! f* W( Z8 p2 o/ s! |
- var text = prompt("隢憛怠亙銵冽憿", "唳蝡");; x8 b; {& B) G. ]( x1 j( ~
-
+ l4 i8 Z. V/ ~& P - if (text != null && text != ''){8 g4 g; o8 h( C$ C) L( G' v- V
- if (posts != null && posts != '')
# ]8 E4 s% X4 n' D - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');
( O% ~7 t- z# p$ E$ d' Z - else$ a7 ?) `: y7 q
- ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');: C' i! F$ x* c. M' z# T
- } J' V4 g9 d2 c5 @% f! b N" @! M
- else{! e. o" b2 [' c/ I: C1 a9 z& ?
- if (posts != null && posts != '')
* ~, [4 m2 s2 _ - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');
* u6 i9 U; G9 L6 X# Y& u) X5 z3 f - else
: O' S9 K* C. ^* g/ O5 } X - ed.execCommand('mceInsertContent', false, '[recent-posts]');: v7 F8 s8 r% a
- }
: `3 Y" f1 J' i: B% M+ N - }
8 [# ~9 G6 z9 A& ?1 e V) G - });
( a3 F0 i4 J6 O2 l - },) o* I* ]0 u! N% m v
- createControl : function(n, cm) {
4 f: V5 {! D3 i# M - return null;
- {( v( W3 P$ K$ ^" c! [7 u d - },
: p4 v6 s% N, F; z - getInfo : function() {
3 c/ ?4 C% |4 ~7 v' W" W. X0 j - return {" Y9 r% n8 R. M! D
- longname : "Recent Posts",; R! } w5 w! I8 V# v
- author : 'Specs',! k4 ?, B9 Z/ d' C
- authorurl : 'http://9iphp.com',
0 \, f7 d9 S% D( ~8 }7 q$ k - infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',/ o0 S' n# ^/ \- S, Z& i
- version : "1.0"3 I9 C. h( h2 D9 b: x
- };: P5 t0 m) O6 y# i
- }( p* H+ }0 R' u# E5 k# f) I+ B$ [/ i
- });2 @# A% f- B1 y
- tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);7 }) T& W6 V G1 D! u
- })();
銴鋆賭誨蝣 澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨 onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉 敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准
3 j3 w) {3 f# ~# g2 \* x$ w& N
4.2 閮餃TinyMCE隞) d8 T6 b; q$ J8 M- ~, } K9 e& n
: O/ v! d. ]' L% j0 E4 |. H 嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉 蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩 - function register_button( $buttons ) {! A" n& Y2 L. }2 _$ e
- array_push( $buttons, "|", "recentposts" );& n' p$ W9 Z9 A* @' U
- return $buttons;
0 k0 E! ]# M3 h4 Q! o8 T - }
銴鋆賭誨蝣 蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅
e0 u( o' v) `: Y- }- function add_plugin( $plugin_array ) {9 x3 c' ?3 {- Q; d0 c8 }
- $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';1 k! f& J# n# ]6 O
- return $plugin_array;
, N: d* c. } l* ]6 l# N7 e0 C0 [ - }
銴鋆賭誨蝣 銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁
/ ?2 a8 D' l2 ^. }0 t- function my_recent_posts_button() {
* l+ o+ x2 g. [8 Q2 {9 R1 J( d, Z - if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {; g, i0 Z5 O0 x& k$ {" e
- return;, r9 K% N) R' T, W
- }
2 x# J* j3 j+ i& `6 G) w -
: K6 h( G! ?3 ], d, L2 t - if ( get_user_option('rich_editing') == 'true' ) {
* i5 U" Z# o6 C6 `) V& i0 \ - add_filter( 'mce_external_plugins', 'add_plugin' );
: m ^7 Q* c$ N: O( [, K U/ w - add_filter( 'mce_buttons', 'register_button' );$ l' A E. {( c: e
- } 2 l/ \* Z- t3 Y' j
- }
銴鋆賭誨蝣 嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁7 i& o) O8 i3 b. W) N5 c! z
敺賣詨曉訖ordpress憪賣- add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣
" g& u0 e" Y5 d; {/ `2 T4.3 冽
& ?) `1 t) X+ D. n& O
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
3 Z( d* I9 |2 s( J) Q& E 暺嚗敶箏閰望霈頛詨交蝡賊嚗
0 [$ D2 w. i' _& t* B; d5 M! z
頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
^* v- g; k% y* @0 V9 X3 Y6 i. U' U 貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
( K: q# }8 I5 J6 x. S1 P
唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆 |
|