WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)
6 U0 D- [8 i& m4 T
0 y7 g# s2 u1 g3 W' p5 Jwordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐
; j3 b4 p( K1 u5 t隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具
' ?9 x. Z1 `+ z' F: T+ P C3 a) B) }& ]- N7 b
凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :
+ y! U& D6 v& l# I; o" L* [甇文 , 臭誑閮剖銝訾批嗆蝡賊 : Z; S7 [3 R8 ?4 Z. h
甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :
9 _0 g; R4 L1 h8 T! M7 w) p- [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣 6 B# L7 z9 s4 H5 i" ]2 Y
5 I$ r; r( `8 `! W3 Z3 Y
1. 蝪∪桃凋誨蝣' s- t$ L9 a) Q# B; o
祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :
" X4 |/ B0 O2 w: o8 g- e閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :. }: |. v$ ^2 _
- 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
- 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
- 閮餃賣賊蝯針ordpressaction銝& p1 t4 L/ b/ h- A) z
7 ]! w7 [- ]- ]* ^4 x% i
祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准" a. ?2 I; p1 o+ T% C
1.1 撱箇餈賣
" ]) u; `* J; y' P4 P% Q) ~2 P7 D嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡
) z9 d7 J% b5 v/ \- ~- function recent_posts_function() {) f7 y0 G, B0 R! ^
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));
( Q$ j1 Y! h# v/ b6 L4 q; F4 n, g- M - if (have_posts()) :& j$ {4 h$ R, N N) y+ p: `( q
- while (have_posts()) : the_post();8 j4 S) ~" A* {1 `7 w
- $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';
7 i: l3 R: t7 l3 R - endwhile;
0 A( ^# x. Y$ u9 }$ E& C7 R - endif;
7 y+ z7 S M: [3 V( P4 [$ t - wp_reset_query();
+ n( O: X# z4 B, g. x - return $return_string;/ b6 z g7 e% C6 h# O! A
- }
銴鋆賭誨蝣 憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯
P0 B3 F6 r6 i' i7 t5 B
' _& A5 I6 x- m* g4 G f6 t! Z ~7 X- h9 z8 j- N6 J
1.2 閮餃凋誨蝣
) o1 p" I, T! Y曉剁閮幃ordpress賣豢臭凋誨蝣潘- function register_shortcodes(){
: L, G( |5 a j: G1 C' b( @- g1 w - add_shortcode('recent-posts', 'recent_posts_function');
( ]8 G: J+ G) K; E+ _! [ - }
銴鋆賭誨蝣 嗅冽蝡批捆銝凋蝙函凋誨蝣
& p |; H! X# |, y3 t! b+ m撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴
. o) M4 L1 I$ H! ~1 @. M
) _6 h7 [% r6 }& g1 |5 p6 k% Y* J- E/ o; {& e; f, P1 d
1.3 Hook into WordPress
- b; g3 v) w& z' Y- Z4 O8 f4 F
9 p* R! u) E) M' |0 O箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准$ p! j1 X5 o1 m! k8 c J
- add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣 ' |" N" k8 {4 E" M( s- H
1.4 皜祈岫凋誨蝣, e3 P+ b' D" l+ n4 f; @
蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :2. 脤凋誨蝣 2.1 凋誨蝣澆 凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻 閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼 撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 : - function recent_posts_function($atts){% z9 w. B8 W( ^ W# V1 Q) ~
- extract(shortcode_atts(array(
' f K* G4 y7 A; l O - 'posts' => 1,9 l" ]8 N4 J3 X0 p) v" b
- ), $atts));
( L; j- Z9 b- {6 k" [$ s% ~ -
6 H) {" y8 s+ H2 h' e8 d - $return_string = '<ul>';9 a: ], a: V7 j
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));/ V8 V6 K9 _6 w2 s
- if (have_posts()) :, n. m/ ?& H! n# U$ U, O2 |
- while (have_posts()) : the_post();" f% e8 k* {2 O' h0 ^: C5 X
- $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
& a3 u) k8 q1 I! Z# p7 Y - endwhile;
- U% V9 w6 u t* ~/ ]. P9 [* n6 ] - endif;6 f; J$ w! G3 A9 G
- $return_string .= '</ul>';
, S# I, U& B2 h1 f& N) } - 2 D+ O4 Z7 ^+ C3 ?
- wp_reset_query();
7 p0 ]* s( J6 P" C$ H; Z* ? - return $return_string;
% I1 K. z" b6 Q3 p4 K. Q - }
銴鋆賭誨蝣 憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :
7 m$ Y8 O, a* n2.2 凋誨蝣潔葉憓摰
臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗 - function recent_posts_function($atts, $content = null) {
8 k. N) o1 I! K1 m1 r1 \7 t - extract(shortcode_atts(array(4 S; D9 m5 s% r. \
- 'posts' => 1,
5 Y$ B; P% o+ b- S; s; j- F. m - ), $atts));
- ~2 ]& F+ @( n& J" {5 g - . b- ^1 T8 T p; g8 |3 D. L
- $return_string = '<h3>'.$content.'</h3>';
- ~% L$ S/ q/ l3 k, } - $return_string .= '<ul>';+ X1 W6 }9 p6 V2 V( L7 f! V
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));7 y' b; g3 S6 S1 ^! ^2 ]
- if (have_posts()) :
M1 ? P/ O1 r$ t - while (have_posts()) : the_post();6 e3 t6 h- g: Y
- $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
/ |5 Z8 o! I0 H) O9 x$ } - endwhile;
+ M0 Z; r8 g3 L/ H4 { - endif;
' c+ a1 K* ^' Y5 i - $return_string .= '</ul>';
- O1 L' \. n4 j, w; C( {& ] - % U- C- f; d, u1 I( W
- wp_reset_query();
, j) f6 l. |6 d' |: B' W4 y- P; c7 B - return $return_string;3 J7 r- w. h% }/ l ?6 z* w
- }
銴鋆賭誨蝣 蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝- [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣 支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗
2 Y! ~# F" V. v3. 其遙雿唳寥*蝷箇凋誨蝣 3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣 閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛嚗典臭誑撠甇斤撘蝣潭曉典撌亙
: ~" O) p3 Q" z: _9 v0 d( R2 |" S1 ]臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 : - add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣 曉其閬靽格嫣遙雿嗡唳 , 瑟, 湧甈撠甇蝣箏圈*蝷 $ ~* C. S. T/ ~+ D; @& J; q
璅嚗刻隢銝剖函凋誨蝣潘 - add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣 冽閬銝剖函凋誨蝣潘- add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣 4. TinyMCE蝺刻摩典凋誨蝣潭4 h3 f4 d+ p' t) W/ T! C
3 D0 v N4 `4 Q9 E嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗 - 箸迨撱箇銝 JS 瑼獢
- 閮餃JS隞& w6 `+ H( @6 C) D& y A
" C( f& ]& G% w1 [4.1 JS隞嗡撱箇JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗 - (function() {$ U8 b0 G% ?! \; v$ `
- tinymce.create('tinymce.plugins.recentposts', {: |, F9 z- L$ ^
- init : function(ed, url) {
; e& _, W. I* ^( x, x7 U* t% i - ed.addButton('recentposts', {4 b3 X, c0 n1 T4 d& E3 V7 N
- title : 'Recent posts',
$ R6 e# u0 Q% ]2 g - image : url+'/recentpostsbutton.png',
4 E. {7 h3 R$ H - onclick : function() {. ?2 s0 S9 v6 n5 G H" p& z* ^+ q
- var posts = prompt("蝡賊", "1");* j! q7 _$ k/ H/ X: G3 Q3 E
- var text = prompt("隢憛怠亙銵冽憿", "唳蝡");5 g0 }" H) c4 y" J
-
9 i2 ^* E! D. r- m: S: H. K - if (text != null && text != ''){1 \$ ^+ Y' x8 Z% v: o/ v" u h, U% S
- if (posts != null && posts != '')
2 P# J9 n$ Q" R. ^- G) e: W - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');
- H0 I1 T: ]* T - else5 c4 H8 K8 P; t( ^* T
- ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');
$ u. x& V, M: P9 _. L, D! y$ I, } - }* [' H9 ]) v- g3 K
- else{
% B- Y* a. @# j5 z$ y& Y( t - if (posts != null && posts != '')" `8 X N6 J) O3 n$ ]1 G3 H
- ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');( j' u# u1 y# G$ a" ?, v
- else i+ S4 }! E+ v
- ed.execCommand('mceInsertContent', false, '[recent-posts]');, [( { B9 J' X! k" `" U4 U" \4 Z
- }' `7 I& j7 _& M) q4 k* c0 u
- } H. K0 T5 a s. [9 E, t
- });0 x, g- Q9 |$ B& m0 k+ C
- }," x0 ?" ~* B! B T
- createControl : function(n, cm) {
1 D$ b) o4 f: C, S3 C" [# N" c3 Q& ~ - return null;
. \8 r6 w# j; M; a - },
/ c# M/ j+ ^, T$ r) Y- Y - getInfo : function() {0 S) K0 e. t# |1 J8 D* Q6 h5 M
- return {- s# V8 M" g& o6 m( L
- longname : "Recent Posts",
0 e0 B. E. g0 N1 V- X5 A! M9 e - author : 'Specs',
0 O4 Y' Q5 G# r/ J) S3 _% ~4 `' D4 R - authorurl : 'http://9iphp.com',
1 y# q: W' B4 Q4 m) f - infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',
) r4 N" o0 H( S! ]) d7 z# m% _4 C - version : "1.0"
! ?: L' C" ?$ [/ r7 c2 | - };
! [1 W2 g* p; d8 P0 U - }
8 p/ t% v7 s6 k - });, p% F; O: F: S- |
- tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);$ h v' a' \9 v4 C( D1 W e9 P" Y
- })();
銴鋆賭誨蝣 澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨 onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉 敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准
+ g" y5 D& l A0 n1 V3 P
4.2 閮餃TinyMCE隞
7 _( D# e2 _+ }8 K& H' d3 @5 `) u/ v) e* H* _6 Z+ W
嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉 蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩 - function register_button( $buttons ) {
/ [* v% ]3 J, M2 R6 S - array_push( $buttons, "|", "recentposts" );
) }" T0 R4 Z/ c5 G - return $buttons;: ~% T+ @: Y: [, B$ _5 Z+ F
- }
銴鋆賭誨蝣 蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅
$ S, Y2 p( k( l- function add_plugin( $plugin_array ) {
7 ?& Y. G: j& k" p' b5 W% j - $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';/ O8 R5 p9 h9 e
- return $plugin_array; |! |1 I" T% c3 C0 }8 }
- }
銴鋆賭誨蝣 銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁 ^, A9 k' d" K. [. J. X
- function my_recent_posts_button() {
6 ?. T- W6 l9 I* _( @/ W - if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {4 e- H# O" Z) U/ A, Q
- return;( P" ]1 T: d* d- ` ^
- }
# f. I" |: X: W9 | -
9 V9 Q4 T s- G2 p+ a/ } - if ( get_user_option('rich_editing') == 'true' ) {
8 ?2 X6 g4 w, o! _6 [ i - add_filter( 'mce_external_plugins', 'add_plugin' );. _- z. ~2 ^; T$ s5 K* e; o Z
- add_filter( 'mce_buttons', 'register_button' );
0 f: [% K1 A2 r' q s/ U - } * @' S4 J7 o0 }. V
- }
銴鋆賭誨蝣 嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁: M# F/ @( ^* y
敺賣詨曉訖ordpress憪賣- add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣
0 G6 V2 u! n/ Y& ^4.3 冽
- x) A. D7 j' G1 T. n) h
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
7 \8 O: y! r/ f9 i8 r' I
暺嚗敶箏閰望霈頛詨交蝡賊嚗
9 e T" k/ Z# `/ X; S& C' W0 ?- ^
頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
9 X! F Y9 c5 m
貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
8 v) C( W' f5 C* [: r( j3 m 唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆 |
|