WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button), x/ w3 \& O$ i) Y
# A# z( R) J( U0 c* K
wordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐/ }1 Y" k3 k% u6 x J( D
隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具
4 F r* Q& Y. ~/ `: C, e* M, V8 m
; G+ e1 r/ \) D# |8 Y; P; z, R凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 : S. D& a% I1 y1 }5 }/ q
甇文 , 臭誑閮剖銝訾批嗆蝡賊 :3 v0 [) G1 v4 k- \$ r5 ]
甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :2 S3 b* Z- R2 h+ R- v- o
- [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣
$ u# [8 a3 u( `; N' M, E
" F3 y2 ^0 c9 }7 t: L1. 蝪∪桃凋誨蝣0 p& W* I9 V, H% R2 O& r m
祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :5 X+ H3 m$ q7 C( u( w2 G
閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :( p8 B) E& F* E- w
- 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
- 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
- 閮餃賣賊蝯針ordpressaction銝
: F7 ^# L' h% r2 i
" g" s# A, R; M( l6 H H- m0 w祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准
. c Y) R# T: `" R T2 Q4 i* H1.1 撱箇餈賣8 R" v, N& X- t" M s8 l! u) c
嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡
) P/ \7 Q9 j6 R& d, L1 F+ v- function recent_posts_function() {
. x# W# _, h% y- v9 ?' V; v/ c - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));& N5 r2 j2 L# r0 u. I
- if (have_posts()) :
! w |& D8 F! f0 {! e" ~ - while (have_posts()) : the_post();
' w6 h N5 U1 J4 [. `$ F/ _ - $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';
' y$ g4 {6 C" ^. W* E+ R - endwhile;7 d; t9 f" C: b7 g" k2 R) I
- endif;5 e" q" A2 J9 u; X8 y; T. U4 U
- wp_reset_query();
2 K' x$ S0 ^0 T+ r5 H - return $return_string; F8 c1 V. E: T9 E! a, @+ c# ]6 ?
- }
銴鋆賭誨蝣 憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯" i+ u! }$ a6 M0 m. G6 r8 \; [
8 q z0 B* v: V% z Y7 O7 h2 y
1 m& J! n/ z9 T! H, q( S1.2 閮餃凋誨蝣* ~) t( g' [6 q, F3 P3 E
曉剁閮幃ordpress賣豢臭凋誨蝣潘- function register_shortcodes(){
8 ]2 b: R& A2 L0 d - add_shortcode('recent-posts', 'recent_posts_function');
' k6 @* z$ G0 v8 J, w* X2 Y - }
銴鋆賭誨蝣 嗅冽蝡批捆銝凋蝙函凋誨蝣( j$ k- i. I# G" j; C" M
撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴3 ?& s& Z2 V; w9 G' L: P
3 @4 ^6 k% h& J& {" z! @
, s1 h% D, z4 i1.3 Hook into WordPress
9 O* q: j: ?) G' u# j+ ?4 n" ]1 ]1 c& o/ t3 p
箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准
4 i ?' H6 H" a K6 t' l! u" r- B$ e4 ^- add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣 - u9 i* b& S1 _
1.4 皜祈岫凋誨蝣
! i; [1 O8 b5 u2 L5 [' [# c蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :2. 脤凋誨蝣 2.1 凋誨蝣澆 凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻 閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼 撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 : - function recent_posts_function($atts){( G, e0 @4 ~# ~9 b2 N: F' G
- extract(shortcode_atts(array(+ M( i- J$ R) O" s6 w0 v H' l. `
- 'posts' => 1,3 }# B# [0 z9 x$ T% }& Y" E) @' S Q
- ), $atts));1 }) {9 F) I2 t+ D* k1 C1 y' y1 l
- $ ~0 q9 g0 s* K& Q) j8 R
- $return_string = '<ul>';
3 n) p" l2 @& B) i' f3 K v0 u+ T - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));7 D/ |2 r. q+ z' |
- if (have_posts()) : x. R4 R0 V8 P9 g; [( U. ?
- while (have_posts()) : the_post();! ?# r+ z& j' @- P, F; u0 B
- $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';) f d. e* z- P, S" B& P
- endwhile;( f9 h" D |% d/ h/ f
- endif;
( K! O) L: M# l - $return_string .= '</ul>';
7 b( L; I3 B& X; A) m J( q -
; B9 q' O) a! f- T# X' n0 a2 s" m - wp_reset_query();7 Z; D6 j% P8 Q9 F; s# @
- return $return_string;( M1 ]+ h! I# ?/ n5 i' T U
- }
銴鋆賭誨蝣 憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :
3 F* m7 |9 E' L# m+ }& N) \2.2 凋誨蝣潔葉憓摰
臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗 - function recent_posts_function($atts, $content = null) {' t4 |! h# [; D- R y
- extract(shortcode_atts(array(4 c; ]. @; d' q
- 'posts' => 1,7 j) N' G( ?# M
- ), $atts));, Y! Z7 {$ v8 l5 }+ Q
-
1 G4 w0 B; r# s- V( L& O3 q, n - $return_string = '<h3>'.$content.'</h3>';/ K B/ i/ V" t, Q
- $return_string .= '<ul>';
* C* P- z$ n: O2 u8 Z - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
$ }' c+ ^* g% q - if (have_posts()) :
. o# E2 r/ @% n% U2 d+ w - while (have_posts()) : the_post();9 D s, t8 \# M+ t M. t
- $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';) v p6 ?3 ^* @; t) ~
- endwhile;4 \+ v& ?1 M$ O. ]" J+ M
- endif;% e+ I- C' z8 a# l
- $return_string .= '</ul>';
4 s, k# v/ y! j# r# X. G. Q; U -
) M5 D( d# _4 I# ~ |- f3 s - wp_reset_query();& M3 J" W: Y$ E5 I. g7 i
- return $return_string;
@* ^$ }. A/ v& t - }
銴鋆賭誨蝣 蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝- [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣 支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗
3 i0 ^: o7 U; H3 \+ } z0 S3. 其遙雿唳寥*蝷箇凋誨蝣 3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣 閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛嚗典臭誑撠甇斤撘蝣潭曉典撌亙 9 t8 @# N- h9 _) }" m& @
臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 : - add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣 曉其閬靽格嫣遙雿嗡唳 , 瑟, 湧甈撠甇蝣箏圈*蝷 * ^: ^/ _8 e$ \: P1 [) \
璅嚗刻隢銝剖函凋誨蝣潘 - add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣 冽閬銝剖函凋誨蝣潘- add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣 4. TinyMCE蝺刻摩典凋誨蝣潭4 ]+ `# N0 V+ N4 q
& H L' }% X0 y: ~% \1 G8 F
嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗 - 箸迨撱箇銝 JS 瑼獢
- 閮餃JS隞
1 G* b6 y7 y' y$ w* G% @ 5 ^. T! I# F% ^1 S" X$ e
4.1 JS隞嗡撱箇JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗 - (function() {& Y& t4 F) z# S3 v
- tinymce.create('tinymce.plugins.recentposts', {
% b9 q3 D) l) t; p+ k/ v) q - init : function(ed, url) {& A6 e" B( y0 W
- ed.addButton('recentposts', {1 i) K; }+ `) r
- title : 'Recent posts',
- g# p0 D0 K j, |5 ?% w - image : url+'/recentpostsbutton.png',
c5 _( I5 R$ _% | N- G - onclick : function() {: H% y5 |. L0 I, G
- var posts = prompt("蝡賊", "1");( \& C( b) G; Q8 R/ e
- var text = prompt("隢憛怠亙銵冽憿", "唳蝡");
0 v+ J) a; j/ ^8 c' I1 Y - * k; p+ w! o3 X9 h) [
- if (text != null && text != ''){
5 K4 d+ ]- n% l; g* D2 z1 x5 E - if (posts != null && posts != '')
+ b3 u- P5 A5 ?; L - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');
4 m8 J! }2 `5 q' o7 _) z8 l3 g5 T - else1 p" W9 K, h4 y7 ^5 d
- ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');
& j' `' k8 z$ @1 M/ l - }
) y! h6 w/ ?+ `3 g3 I! Z0 L - else{
7 L$ p; d! L7 i* a2 e - if (posts != null && posts != '')
$ s) G+ A; z2 k0 { - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');7 l. Z3 D2 d8 ?& l, D2 ^/ k# C+ k
- else. M! X; y* v; z
- ed.execCommand('mceInsertContent', false, '[recent-posts]');5 M$ N5 w' n' g% X2 v5 @
- }
L2 A2 `. s: A: c- m' e0 a! X - }' m7 b4 ]# b$ L1 }
- });: m' v. R* }3 C8 m+ @% z
- },
. |) c& x5 j. Y; p$ j& @3 W - createControl : function(n, cm) {5 ?! O" p* i& R2 |" D. K$ c
- return null;6 t% V( ?) A; h( c
- }, X5 V8 C) [4 C
- getInfo : function() {
; z$ x1 e6 W- j* L - return {
" ^- c4 G% s' } - longname : "Recent Posts",
) o9 d# {) x4 G1 g4 U( ^ - author : 'Specs',
?& d# Z, b0 l; ] - authorurl : 'http://9iphp.com',
6 J5 Q( |5 S n0 t3 h5 `% ^& u - infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',
8 f4 O2 @. f+ D; U5 K7 D1 A7 o - version : "1.0"0 @, J* ~9 x" J! Y
- };
0 L3 j5 K" A. |' y - }
6 R0 y3 i4 \. v( P0 @0 M2 k: ]3 E2 A - });
/ D$ H _2 w7 T/ t - tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);
j5 X0 E' `$ Y; u( n - })();
銴鋆賭誨蝣 澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨 onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉 敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准 , K! c4 q, a! L$ j; g) z
4.2 閮餃TinyMCE隞
- r. r8 T2 }- D3 S* {9 |4 ]
, a0 k2 Q& x w* s6 _" M. x5 l 嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉 蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩 - function register_button( $buttons ) {. z) k( |7 \* T/ U) H- b
- array_push( $buttons, "|", "recentposts" );
0 e( Q4 t. ]2 a- t - return $buttons;
4 x* b+ u6 N& ^$ ~ y8 K; C - }
銴鋆賭誨蝣 蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅
4 v8 i: u `; i0 l, z- function add_plugin( $plugin_array ) {2 X& ? O, J+ i: w; E
- $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';- }* i0 B( H$ r$ c
- return $plugin_array;" }! L% x0 g: h1 l' n
- }
銴鋆賭誨蝣 銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁/ A# d) V. `6 Q: ]2 H
- function my_recent_posts_button() {
4 z. a f3 l0 \+ M - if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {3 `+ W5 H: I' h5 Q% X
- return;
H6 a/ s( k8 G$ ? - }
- s6 [5 t9 s; w9 n8 c - ' p2 f+ K; u- k5 B0 @. S' c
- if ( get_user_option('rich_editing') == 'true' ) {
) F1 k2 O6 X8 h$ Y. ?5 f$ | - add_filter( 'mce_external_plugins', 'add_plugin' );
, V c+ L1 A9 G, ]' [0 _: X7 \. m - add_filter( 'mce_buttons', 'register_button' );
, x; Y5 e Z8 R9 X5 S9 [4 H3 v - } # }, x7 k" R/ t1 ], {# e0 Q
- }
銴鋆賭誨蝣 嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁
' S/ P7 F, E8 W0 Z2 M1 j; P敺賣詨曉訖ordpress憪賣- add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣
; ]% i" z2 X; z6 z4.3 冽
6 v( @% o& R; ^! p6 P) ~- f
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
6 Q" l' s) a. K0 j. C+ I4 G
暺嚗敶箏閰望霈頛詨交蝡賊嚗
& }; {6 X8 H& r! k9 q2 D" `' j/ [
頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
3 q `) ^: n* `3 O2 @& G0 e3 t. J 貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
' n) [1 v' S7 O: g
唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆 |
|