WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)
6 |; @% C3 y+ b2 r: w0 A
5 f* V8 f0 d/ U% |1 H" E6 B$ lwordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐/ D% }0 n. D- z2 U) o) H
隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具' \0 }' O1 O O) b8 e s
4 r8 z% N/ ~0 H: b; P6 J
凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :5 w4 l+ \, L+ ^/ w1 \
甇文 , 臭誑閮剖銝訾批嗆蝡賊 :" K$ q2 I2 l. Y1 h' f
甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :7 L5 _, V1 J% C. N
- [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣 $ d9 p3 r& u) |! ?4 o
. x2 X$ t/ e3 h4 h- G% X) |: E8 X
1. 蝪∪桃凋誨蝣; g2 a6 M! B) e) Y
祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :9 _: M1 Y) P% p2 R; R
閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :$ |$ k( X/ D1 K& Q
- 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
- 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
- 閮餃賣賊蝯針ordpressaction銝
5 {3 _) g- ^, z1 Q, c 5 O1 t- c i3 D t0 t$ k
祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准
2 w; F, F# J- n( s1.1 撱箇餈賣
% U9 u- @8 Y6 H* N' b嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡2 t, I1 e. A( }7 X1 S! V
- function recent_posts_function() {8 Y! T8 r8 ^6 W
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));
$ B5 b9 x- h4 H - if (have_posts()) :6 E' J+ E5 I% ]$ x6 p
- while (have_posts()) : the_post();$ t- H) G1 l" P" t
- $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';6 L& V4 J. G4 W5 E0 r- O
- endwhile;
( i. n6 J; v+ O. D# U' J" a - endif;
0 |! o& w, R7 N( c- |9 T, D - wp_reset_query();
$ I+ r8 I- q! o. m+ m7 e - return $return_string;
4 ^8 G* D+ \5 {$ b! C0 C8 J0 {; Q/ W - }
銴鋆賭誨蝣 憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯' s7 X/ X% Y8 y1 C( x
+ H* ^* O' B6 a$ S
/ Q# F( \$ V" I: `, j1.2 閮餃凋誨蝣
9 g, t6 ^% Q# C; E" J' t曉剁閮幃ordpress賣豢臭凋誨蝣潘- function register_shortcodes(){) j( b- [, b' K
- add_shortcode('recent-posts', 'recent_posts_function');
9 O2 p! q7 y/ w( ]; D0 n2 ^7 O - }
銴鋆賭誨蝣 嗅冽蝡批捆銝凋蝙函凋誨蝣
, \5 r" u" Y$ P* e! j/ S' M: z撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴
+ H% I6 @- S8 @- A4 Z* h& V
- G, j: r& {% N2 Z
5 [0 N, Q0 ?+ d) I1.3 Hook into WordPress- E1 X# b% m8 w# ?+ S
' W3 i6 n) w/ B; M8 w
箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准' \4 g4 m6 l7 `
- add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣 x) e0 n! r* N6 w
1.4 皜祈岫凋誨蝣1 Y5 x- X: U. p& y, m; ]6 [
蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :2. 脤凋誨蝣 2.1 凋誨蝣澆 凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻 閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼 撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 : - function recent_posts_function($atts){
* \8 T* l( M0 r# A$ @& y - extract(shortcode_atts(array(
7 [& o! L2 }+ K$ u7 x8 o( ? - 'posts' => 1,% k- @" m4 `# L+ }
- ), $atts));
; h0 ?7 h' r- s5 W5 {/ T" z - % O5 c, p2 }! C% y
- $return_string = '<ul>';
* o! k% i8 s" w9 S: F - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
0 `1 q4 f: |! Y2 w/ y a' A - if (have_posts()) :$ }& u+ L4 e H, P _( o
- while (have_posts()) : the_post();$ C1 c5 i6 K" v- w
- $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';) D+ Y! w% S+ y1 f# J
- endwhile;
6 F* A W9 T: U+ {6 E- ? - endif;
' p4 E H. g$ s% s" A3 j, J6 ~ - $return_string .= '</ul>';3 y% V' t9 Y0 q( k# i0 k% v6 S
- , w0 t1 J( f' {, y
- wp_reset_query();# u8 g" y6 q; i% D# q# B
- return $return_string;
, P& g: I6 Q% U' A - }
銴鋆賭誨蝣 憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 : _2 {- ?" Q. H" q5 w/ V
2.2 凋誨蝣潔葉憓摰 臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗 - function recent_posts_function($atts, $content = null) {+ j0 w+ Q* Z/ y
- extract(shortcode_atts(array(3 \2 R9 z8 R9 u2 D
- 'posts' => 1,/ J' X1 B9 I/ |+ \. _
- ), $atts));
, r1 u- ~# V9 k -
( H% \: `; ~8 _# e1 A$ K6 K - $return_string = '<h3>'.$content.'</h3>';& Z- ?' Y+ C1 o6 p$ }( g
- $return_string .= '<ul>';
: f1 z, N8 [7 f5 r4 T6 ] - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));# h# M2 u9 ~9 O/ O' b2 k) D
- if (have_posts()) :8 O7 m2 h; H4 y
- while (have_posts()) : the_post();
( c1 n5 s( b* ^1 B6 W. N - $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
5 j' ~$ o4 D1 l" l6 ~/ o - endwhile;2 D+ W! y% e+ m: t
- endif;
: ?* a5 j) f( f' v+ s0 D) ~: Z - $return_string .= '</ul>';
. p( c/ f; Z0 d+ l9 ~ - $ d' K2 e) j: F! A
- wp_reset_query();; e+ N2 x& ~4 w0 H) @: D, i1 g) a
- return $return_string;% y* ]9 c! r5 K% i0 t# ^
- }
銴鋆賭誨蝣 蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝- [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣 支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗
& y0 t' q! A( ]$ o- n( @3. 其遙雿唳寥*蝷箇凋誨蝣 3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣 閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛嚗典臭誑撠甇斤撘蝣潭曉典撌亙
5 `1 g B J# B, x' T( p) p+ y臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 : - add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣 曉其閬靽格嫣遙雿嗡唳 , 瑟, 湧甈撠甇蝣箏圈*蝷 1 w+ x1 ~7 Z* }
璅嚗刻隢銝剖函凋誨蝣潘 - add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣 冽閬銝剖函凋誨蝣潘- add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣 4. TinyMCE蝺刻摩典凋誨蝣潭
9 y% k1 j! }% L
4 W! S4 N( Y) W5 `嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗 - 箸迨撱箇銝 JS 瑼獢
- 閮餃JS隞# w' e3 E8 }3 k" D# Q; F* L
; p7 m! V/ [ n
4.1 JS隞嗡撱箇JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗 - (function() {
% g/ p5 `! l1 Y d4 c - tinymce.create('tinymce.plugins.recentposts', {* o/ x; _* C g# S
- init : function(ed, url) {5 ^6 E3 z* T3 t* h4 B. W
- ed.addButton('recentposts', {
. ?+ I- u& X9 p' |0 [* |: @! T. g - title : 'Recent posts',
% r+ E$ X6 v* q( W% Z. [ - image : url+'/recentpostsbutton.png',9 H, \, A/ R; j# [( T1 z; B9 X
- onclick : function() {
! e2 k" j5 \% s* o - var posts = prompt("蝡賊", "1");8 W' g# m$ ]" `' o& W+ G
- var text = prompt("隢憛怠亙銵冽憿", "唳蝡");
, P8 O3 Y& f V; h -
) t6 J" l& l ^. n$ B* X3 K - if (text != null && text != ''){, M9 d4 E3 A: a0 R9 b2 V
- if (posts != null && posts != '')
4 o, D1 P/ ^# p S3 J - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');+ ^6 M2 ^, L0 {$ X) A
- else
5 v1 l; A0 P; V - ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');
( ^9 O0 K1 C/ N U( z5 U/ } - }* S& v$ X }9 y3 \% O
- else{* X6 `8 c% A3 `
- if (posts != null && posts != '')
4 i/ P! ]3 q0 ^6 b8 W - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');8 L' d% b6 `+ u1 R. z$ U' u
- else V/ u" w3 Z, K! C
- ed.execCommand('mceInsertContent', false, '[recent-posts]');
: a5 l3 d# J. \3 _$ ` - }
' C0 H' H! f( h% L0 Y `6 ^ - }
$ K5 z+ Q& \" J: ?& Y$ e - });3 l% {. ^6 `# P- r
- },
4 {6 P3 M7 j* G - createControl : function(n, cm) {
, \! \+ |5 X( @1 B - return null;1 @! l+ Q4 x3 L' F) j
- },
2 p4 r+ n; _; j+ W- ~6 \1 N# _9 d - getInfo : function() {
# ~$ n9 c" j- F; k6 W1 \ - return {' C& I+ p% C3 N' R( P0 y; Z
- longname : "Recent Posts",1 s o4 M+ [; @# |9 f+ Q
- author : 'Specs',+ \) W8 ` W, T* E! v) x
- authorurl : 'http://9iphp.com',9 H. d- S2 r- T3 A# A$ R
- infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',
# R! J$ C' c* l) U5 i' H, G - version : "1.0". R! c" f2 Y; }9 k
- };; Y) j2 X7 E6 A
- }
/ D0 A& w! ~# G# J - });+ Q: S. W9 h0 `
- tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);* a: D* [5 y! w3 N+ b
- })();
銴鋆賭誨蝣 澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨 onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉 敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准
[% y+ _5 M# r( {9 i
4.2 閮餃TinyMCE隞
+ C% [% I ^) l9 N Z# r, Q! V& o" |$ b$ }1 F
嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉 蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩 - function register_button( $buttons ) {
! d. ~/ L- w; K" g& \- a - array_push( $buttons, "|", "recentposts" );, q8 ]5 @& L$ k0 Y9 a
- return $buttons;* F# D, w/ i! F
- }
銴鋆賭誨蝣 蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅5 s9 j/ F$ @: |, o. g5 U
- function add_plugin( $plugin_array ) {
M1 F D" t( J" I: \) C& G0 [3 t - $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';
9 ?& e! O; i8 V C - return $plugin_array;
z: }2 o/ s1 g: x - }
銴鋆賭誨蝣 銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁
6 @( @9 C1 V9 y0 H, F- function my_recent_posts_button() {
- W0 S, G3 f+ [, p1 I - if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {5 ]+ L6 N2 u# @6 |$ ]- n- k
- return;$ w P: ~5 a1 ?! G, G
- }* `" O. F1 z* t) x, y% H
-
) J8 A7 f2 k( O - if ( get_user_option('rich_editing') == 'true' ) {) `" D& f% p0 ?" a; h% u
- add_filter( 'mce_external_plugins', 'add_plugin' );
) v* P# h, |* r+ k3 \5 ~7 m, M - add_filter( 'mce_buttons', 'register_button' );
6 g W* [* }' }3 j9 |% j2 K, w - } 3 } a$ M% H1 [. {. W
- }
銴鋆賭誨蝣 嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁
3 b! _. X4 [8 I敺賣詨曉訖ordpress憪賣- add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣 " H8 t! u; r5 A+ q
4.3 冽7 r+ t4 T1 l% P4 o
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
5 L% U) K% q- q6 C8 e 暺嚗敶箏閰望霈頛詨交蝡賊嚗
2 }( h2 @6 A( X6 t: X: Q. P 頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
9 E" o( ]( i* _' p# q 貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
& k1 ?2 D6 R1 B0 O7 x% c) t6 P
唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆 |
|