WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)) J% ~% i/ O# t4 h7 ~% ]; C2 c. c
# G7 Q, q( p# C. a
wordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐' }6 R$ I' F1 z+ R
隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具
1 m$ q! ?. _! y' E
' g6 J! k9 `# E" q凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :) w8 v7 a8 z: i) `$ H# E
甇文 , 臭誑閮剖銝訾批嗆蝡賊 :
; i/ }, I$ K6 \/ v# P+ X! g6 Y0 o甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :
% \' w2 Y) m, {' k* Z0 T- [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣
& {/ X" ?5 k: c6 E
& T) N$ A7 p4 G" H! N# C1. 蝪∪桃凋誨蝣5 u9 F# E- P/ r3 n; M1 H( I5 s
祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :! J( b: R) ~" v) W
閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :
1 @5 _0 G4 n5 Q7 e7 C/ d. n q- 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
- 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
- 閮餃賣賊蝯針ordpressaction銝
, V) _ r( [4 O9 ]! Z4 H; ~- A8 |1 Z . ^5 h4 p7 e& V7 b& y. ]
祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准
+ t1 n3 [+ v* o9 h1.1 撱箇餈賣* v1 t- {8 |6 U8 R
嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡. c+ ^; y5 s' I1 L# ~: O* ]' B* q
- function recent_posts_function() {: N! I1 _4 e$ R- @; a, @& g( q5 O0 G5 x
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));
8 E0 h& J4 E' x% n3 Z R, v - if (have_posts()) :1 Q& S- l1 G1 i q: R
- while (have_posts()) : the_post();$ e! B0 @4 N! o' Q
- $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';: V( j! Y6 R2 n
- endwhile;, w# ~6 ~9 C4 Z7 i$ Y
- endif;
y0 z* s- \ H' {; ] - wp_reset_query();
6 v/ x$ U9 X9 Y9 P& h - return $return_string;' R3 J* k, Z* u3 v1 H( M- h+ H/ |
- }
銴鋆賭誨蝣 憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯2 Q# X& g R) h* @; B' M) A8 p9 `) v# v
8 C) r& ]7 I% \* J# ]/ `0 ^- [5 U: n) |; U( S5 p8 a- R) e
1.2 閮餃凋誨蝣
) N) N3 L; @8 _) b曉剁閮幃ordpress賣豢臭凋誨蝣潘- function register_shortcodes(){
9 n1 e% V: W7 U* ? - add_shortcode('recent-posts', 'recent_posts_function');# h4 X$ N5 u" I8 g2 i
- }
銴鋆賭誨蝣 嗅冽蝡批捆銝凋蝙函凋誨蝣9 k8 p9 ?0 {% H' h& _0 U( L
撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴
2 c# Y& B# n4 V; `" ]) D0 _. Z5 G" ?2 H, c o& x/ |+ @8 }
' [5 }* G" P4 i/ B
1.3 Hook into WordPress0 A/ T0 w) `& e1 Y7 R. E W2 Y
) p3 \1 s3 r* `; |# c: H% p* E箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准
# d4 h5 B/ K( G2 n- add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣 - C3 e/ u% | R. _" l. L
1.4 皜祈岫凋誨蝣
) l& H+ B" j0 Q5 R1 s2 y4 t蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :2. 脤凋誨蝣 2.1 凋誨蝣澆 凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻 閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼 撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 : - function recent_posts_function($atts){3 M7 Z8 K% S9 x z% r
- extract(shortcode_atts(array(! a6 z0 `3 H3 S4 {; y4 U: H0 D# y
- 'posts' => 1,
4 D5 F5 X' v2 c% Q- h( j( I - ), $atts));3 M* ~1 X) b. m, N/ W' X
- \1 Y6 H3 T. h! D0 F
- $return_string = '<ul>';
5 [0 t5 F2 i* i4 E; a) V3 S0 N - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));0 C5 G9 P+ m6 u3 Z1 r$ O
- if (have_posts()) :* l" g m; E% _% F6 ^( ]9 ~4 J( O
- while (have_posts()) : the_post();
% D5 A) d6 \+ T( Q+ G7 q - $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
v& j! s5 T( R" \* L% @) f( y - endwhile;
: m. f& @6 b* g - endif;$ |( a+ m4 [/ `+ H/ z9 V/ F+ N- `% i
- $return_string .= '</ul>';
$ T7 s# z6 ?, r) v5 l7 J - ! F I" c+ O4 P* J4 V3 P
- wp_reset_query();$ s; F# c% V$ V2 w1 }+ n. V" D8 U
- return $return_string;- j1 `1 s0 m2 Q! E; W& [
- }
銴鋆賭誨蝣 憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :
9 o& q3 i" L& w. A2.2 凋誨蝣潔葉憓摰
臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗 - function recent_posts_function($atts, $content = null) {# V$ {$ I6 d I; P
- extract(shortcode_atts(array(
2 J$ n; b( D( ]0 n( K/ d9 ^( | - 'posts' => 1,# [! @7 ]* Y5 ~7 }% R
- ), $atts));
' v: Y4 W! ?( d2 E# D0 R -
0 B0 c! d$ U5 V7 G7 }6 F - $return_string = '<h3>'.$content.'</h3>';
0 S/ W, ]7 W' ^ - $return_string .= '<ul>';3 D8 _2 y9 p2 L; o( |% \! l: D) Z5 a
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));+ q0 L7 y7 G+ H& s! t
- if (have_posts()) :( u# l( @% e8 x& Y) L. Z
- while (have_posts()) : the_post();" }$ h9 I: V* m% B5 E" H/ _! E
- $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
! {; P0 \6 ?4 F. i) Z$ i5 ], o0 C - endwhile;
* a1 m1 n$ {% i6 D+ ], y* _( k0 { - endif;
: P( k1 F3 b6 {( N; j4 S; a6 }: o - $return_string .= '</ul>';7 G+ {% @' M% g
- : K6 ^+ `8 @% u; x, _' x9 S
- wp_reset_query();
7 E* `+ w6 T# H/ B' ~ - return $return_string;
6 j+ r5 c6 n' d1 f8 d# n - }
銴鋆賭誨蝣 蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝- [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣 支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗
0 z* X# z4 i* r: P6 c1 W3. 其遙雿唳寥*蝷箇凋誨蝣 3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣 閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛嚗典臭誑撠甇斤撘蝣潭曉典撌亙
, K) ?0 r9 u! b$ [" c7 j臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 : - add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣 曉其閬靽格嫣遙雿嗡唳 , 瑟, 湧甈撠甇蝣箏圈*蝷 , Q3 o q7 C7 m
璅嚗刻隢銝剖函凋誨蝣潘 - add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣 冽閬銝剖函凋誨蝣潘- add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣 4. TinyMCE蝺刻摩典凋誨蝣潭
" l- T1 S- a0 }/ p/ O- j7 R
( x% a& e) y9 W/ `嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗 - 箸迨撱箇銝 JS 瑼獢
- 閮餃JS隞- b; \3 ~+ y" e& ~; E
8 e+ Z* I' { S+ r7 l' J4.1 JS隞嗡撱箇JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗 - (function() {3 A k& y4 S- S7 G0 \
- tinymce.create('tinymce.plugins.recentposts', {
) ?. t7 v8 k6 Z/ T$ a8 n7 R - init : function(ed, url) {
" @* _+ a4 ^: e1 g* k, } - ed.addButton('recentposts', {( I5 L5 I. Q- O9 b$ w# l
- title : 'Recent posts',0 k5 _* }: `) x# U
- image : url+'/recentpostsbutton.png',7 O: P) X# R- J' [
- onclick : function() {
- m2 O4 |+ E6 W3 N - var posts = prompt("蝡賊", "1");: Y& i. \3 F& T* y
- var text = prompt("隢憛怠亙銵冽憿", "唳蝡");
9 o$ v8 j% H6 U( p5 K - 7 ]- |5 M+ J$ a$ O. Y! s8 e
- if (text != null && text != ''){* F( `+ H% ]0 Y
- if (posts != null && posts != '')3 Z7 f) ~) F- `! I& H: ~ T# L) F
- ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');/ F3 y2 k$ U" }
- else
6 G3 q" m+ l2 x! h8 [, Z4 k) h- k - ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');4 [! _ [3 q( m `% E
- }6 I' ~9 e# s5 r
- else{* W! x, L! K, H! @- ^- U, P/ @
- if (posts != null && posts != '')
9 a' ?- F* S) S1 ?$ q: l8 F - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');7 n( b" l+ m0 t8 S1 V9 ^
- else
0 I) r" H* K7 d/ F) u# n" ] - ed.execCommand('mceInsertContent', false, '[recent-posts]');
2 ]7 X. `) V. E) S& v4 `# ~. _/ x* w - }
! p# P; @( N) P2 v - }/ f, u; k; v+ j# H
- });
4 k% ~" E9 Q2 k e9 o& a, U - },
$ G. `( H. G# v6 k z+ Z! [ - createControl : function(n, cm) {
2 a' \# k- b2 N# X - return null;
: B1 \. ~+ U; J. O$ ? - },3 {$ _) X8 B$ H0 h
- getInfo : function() {
8 G8 ~7 Q* N9 x& \ - return {0 g- O8 X3 M4 a* `! ^1 Q0 y
- longname : "Recent Posts",
" `2 K. a/ R) { - author : 'Specs',2 M" Y7 R7 d# `$ x9 L
- authorurl : 'http://9iphp.com',
: _! `$ L& d" A- B3 \ - infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',
' @% X% H8 e9 u$ a' C4 s - version : "1.0"! |" p2 M" O8 o" P
- };
+ B9 I/ @0 @* r0 ~) i2 R - }1 D; k- N7 K5 q) d
- });
9 _( C- @) R2 l2 ]1 `1 ` - tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);
6 N! p' L) I1 i2 I8 _ - })();
銴鋆賭誨蝣 澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨 onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉 敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准
+ c9 p# G1 N2 q: Q l' {
4.2 閮餃TinyMCE隞
# U0 S3 [ T, v# b- H! }
5 ?' S1 l7 \6 y$ P2 s 嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉 蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩 - function register_button( $buttons ) {. i' S/ h7 v" V' L5 c4 [
- array_push( $buttons, "|", "recentposts" );
% q' O; v- d# v- r* J- k - return $buttons;
O' F% ]/ n; u6 f4 A, R* C4 | - }
銴鋆賭誨蝣 蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅* u+ r! l D& K& N( G O
- function add_plugin( $plugin_array ) {
! x4 b. N5 h' g: A. } - $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';* C' I2 _/ ]0 s J
- return $plugin_array;2 g, S: z; C6 n/ p9 ?' k( T* N; q j
- }
銴鋆賭誨蝣 銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁
' K$ I# ^+ m9 A- function my_recent_posts_button() {
/ |9 v) A7 d q9 J - if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {
/ I4 U" t0 K# m0 Y9 q$ e6 t - return;
- f8 F% _( H' I8 Q) f6 |, @) C2 u v - }
9 z2 d O9 D+ D' S [ -
( o; z' C( P P/ X8 j* u0 ]) i9 G. } - if ( get_user_option('rich_editing') == 'true' ) {
, v( J- K* \# {+ h - add_filter( 'mce_external_plugins', 'add_plugin' );9 D" P' H& C$ g/ I+ l* J, d
- add_filter( 'mce_buttons', 'register_button' );
6 l8 S' a; H# L( K5 s - }
$ z" F, N& f* B) w - }
銴鋆賭誨蝣 嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁
7 S0 o0 A# y) r( a# d敺賣詨曉訖ordpress憪賣- add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣 [7 g8 u9 B' q9 y- _% C4 Z. v. {
4.3 冽
' d# i/ t! v- ]2 u
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
5 S% o0 W) b- ~/ l' \ 暺嚗敶箏閰望霈頛詨交蝡賊嚗
$ y3 u' ^+ v8 y* A$ s1 p$ Y
頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
/ d2 N" J* {: ^5 F 貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
i4 X' C% }6 w 唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆 |
|