WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)7 h9 M9 _, t% A& S
" A+ U6 F o9 [ H! d0 f5 hwordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐2 n* ?% A6 N1 \) U6 N. f8 b
隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具3 m( f! ]. e; Z x5 J
, \8 U, k) [' }2 |3 G
凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :
F3 `& [* i' ^* a, ?. ` J甇文 , 臭誑閮剖銝訾批嗆蝡賊 :. d/ X6 J: u7 S4 D! ]( M
甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :& d# \' q3 G1 I1 d
- [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣 ! n/ Z. ?: ~0 V9 z0 x
6 A i( `: N _. Y# g
1. 蝪∪桃凋誨蝣8 w9 S5 [& t3 e
祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :1 z( I2 e* o* W, s1 u
閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :
9 a+ a2 g4 p7 g8 V; o- 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
- 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
- 閮餃賣賊蝯針ordpressaction銝
( G5 l& Q0 a) `( e! [2 m- |3 `
, S# n6 Q$ g. O0 y祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准3 E% u# u/ k; I7 i& c1 ^
1.1 撱箇餈賣
6 v2 V9 f; R! y1 a2 {& W/ ]7 z嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡
& Q3 L4 x! t! X- function recent_posts_function() {5 o6 K) G- s4 `) M5 }
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));
" |1 m; `( D! p! O H2 w( t. r8 F - if (have_posts()) :- r1 W! g7 ^1 S, `) z' J
- while (have_posts()) : the_post();
* E! t8 b' E: k d* R# l. q - $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';
- C* v7 N' x% [ - endwhile;: _( K" C3 {% X4 `, b
- endif;9 \5 p2 z0 @6 ^# e: r
- wp_reset_query();0 W% r, d9 o$ B! E! K' s7 }! X
- return $return_string; o4 P1 G; a7 W: @
- }
銴鋆賭誨蝣 憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯
2 ?3 H: d2 U% a( T3 P
' ] K8 Z4 g" R7 o
9 o( h l4 x4 r: p4 p3 P6 W1.2 閮餃凋誨蝣
& T0 C* g( g! q3 |曉剁閮幃ordpress賣豢臭凋誨蝣潘- function register_shortcodes(){
6 `- c: M& A7 W9 i. I! o - add_shortcode('recent-posts', 'recent_posts_function');; Q/ w4 d2 J( \; `( _1 o
- }
銴鋆賭誨蝣 嗅冽蝡批捆銝凋蝙函凋誨蝣
" P \" L J9 |( W撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴
1 c0 k8 I2 q2 p# `! I# V
6 b5 [* `- K' S+ |) |( b0 N5 p. T# _- N: X; m' l \
1.3 Hook into WordPress
/ X6 W' s! h* h4 L1 R3 L8 A$ U' p
) A8 m) ]. c$ C' a0 J' I箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准
9 }5 Y B/ W% b1 G6 [- add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣 / b1 M: a8 P& b* H. _2 }$ E4 v
1.4 皜祈岫凋誨蝣" X v) F+ c5 @5 ~8 e1 X! h7 p' f
蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :2. 脤凋誨蝣 2.1 凋誨蝣澆 凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻 閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼 撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 : - function recent_posts_function($atts){
) b- ]9 J9 m D! e - extract(shortcode_atts(array( A- |. l! u& M: Z" x
- 'posts' => 1,! p+ P; c3 Q' |6 ]
- ), $atts));
* d8 C7 ^1 s2 W9 m3 F6 G - $ ?4 d* J3 o) c) j5 E# P: e4 K
- $return_string = '<ul>';
9 o) T7 W- l" G( q& y0 u - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));6 b4 K) T E' \# ^, a/ ?! X
- if (have_posts()) :9 V: S: {: A2 ~- {- T
- while (have_posts()) : the_post();
$ d N% }$ T8 g - $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';3 p; D9 H7 Y2 o+ ?/ I! T
- endwhile;
( n% ` ~! j$ Q/ W, q - endif;
8 G2 B3 |. J) d - $return_string .= '</ul>';
0 [, r& E/ [* H4 o, l) Q: e7 q - - Z3 t0 k: o7 g6 P+ E% ]
- wp_reset_query();
6 o7 k0 {# R0 p4 c, N3 T: o - return $return_string;( `6 J8 G4 U( o4 {- x% j6 s; \
- }
銴鋆賭誨蝣 憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :2 D5 Y, z6 T4 i
2.2 凋誨蝣潔葉憓摰 臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗 - function recent_posts_function($atts, $content = null) {/ F- O$ Z3 X. R1 T' e. R! ~
- extract(shortcode_atts(array(
; p$ @- W9 |8 ?) w$ x - 'posts' => 1,
2 D" s) x8 B/ [9 V* L - ), $atts));
5 I. a6 a6 f0 U& ]& ?7 J. a! V, S - & n. l/ V( `$ n' G e- n
- $return_string = '<h3>'.$content.'</h3>';
& a. O! U! D0 C& h, [ - $return_string .= '<ul>';9 G9 N- B" \6 ]/ }/ o, d( e
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));+ u& R% N5 Q+ }4 v) r0 h
- if (have_posts()) :+ \8 m! I: h! C$ l2 j& N
- while (have_posts()) : the_post();
' a, v( ?/ V+ n# d0 x, j$ Z: I! } - $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
; ?% g$ ?, D3 A. X# ~ - endwhile;$ y6 g, U/ |: V6 Q. j
- endif;
( v6 Q& H, i7 B3 Q; _ - $return_string .= '</ul>';
8 q: y# c) L7 p -
0 x6 T7 ?: Y1 s - wp_reset_query();
2 _* n' W; l$ B" O - return $return_string;
- E) M' I, _" @% s - }
銴鋆賭誨蝣 蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝- [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣 支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗
7 }; x4 J1 r- S' ~1 d3. 其遙雿唳寥*蝷箇凋誨蝣 3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣 閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛嚗典臭誑撠甇斤撘蝣潭曉典撌亙 $ {" D+ [; |8 \( s) C& y
臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 : - add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣 曉其閬靽格嫣遙雿嗡唳 , 瑟, 湧甈撠甇蝣箏圈*蝷 5 h& ?2 W+ | ~
璅嚗刻隢銝剖函凋誨蝣潘 - add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣 冽閬銝剖函凋誨蝣潘- add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣 4. TinyMCE蝺刻摩典凋誨蝣潭
4 ]# f! G7 l& C$ n% G* e1 {1 o% C- |! `5 N3 m: _
嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗 - 箸迨撱箇銝 JS 瑼獢
- 閮餃JS隞
9 z* A6 c" e( a% h( F t# [ . D. ~- U" C0 h, r8 T; O
4.1 JS隞嗡撱箇JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗 - (function() {
+ r* X5 l( f3 l; r7 J - tinymce.create('tinymce.plugins.recentposts', {) i- m# {0 u2 h- f
- init : function(ed, url) {/ x+ y# O' ?" @4 |9 f: j+ v
- ed.addButton('recentposts', {
R. m/ Y' d+ N0 C1 Y2 `, U - title : 'Recent posts',. E# [' p9 O; D Z( `0 c5 F
- image : url+'/recentpostsbutton.png',
9 R, a' \* J* L$ j7 [& | - onclick : function() {) j: _6 ` u5 p5 a/ X) K1 }
- var posts = prompt("蝡賊", "1");" ]! I2 V" L$ x; c/ S
- var text = prompt("隢憛怠亙銵冽憿", "唳蝡");+ }$ c' c. r9 b1 I8 ]
-
% q: z3 F5 S' d. X; z6 c - if (text != null && text != ''){
( _0 \ E/ c3 c8 n# w3 v# ~/ {# k8 k - if (posts != null && posts != '')
. r# L4 g# _ e- v - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');. r7 w% z# D# n9 Z; r: p% x, X
- else
7 _- c+ [- m# @1 {% D, _ - ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');
; q9 t+ F- G0 ~$ q - }) h4 w/ R( b3 Z/ E" \" J4 \0 I
- else{
9 {) n+ J4 B& ^; t( E# `- f2 f - if (posts != null && posts != '')
* C! V. |9 G; `0 y- _# P& J - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');
% V Z3 p7 D+ s$ e - else
, h+ ?0 B$ q2 D% V% l( ^5 k# S( } - ed.execCommand('mceInsertContent', false, '[recent-posts]');
% L* ?) g5 B: \' f6 c* X8 s - }
( E* A1 b6 S7 F/ g - }( [; A! I$ s& [
- });
2 ^" z! O8 P8 `" `2 Y - },
' R+ W& c! }* n. `- s0 j) ? - createControl : function(n, cm) {
' y5 g2 U2 a* ^4 s - return null;
' @; _7 j" o9 u" r; L1 e - },) e7 }& `7 ^- G" H* h* B
- getInfo : function() {
) Y3 ~, e6 ?1 {* X( X) M9 D5 g - return {
; k5 T+ I: n% H! ~ - longname : "Recent Posts",5 z0 C7 E( O$ A. X
- author : 'Specs',
- l( m, z' `& a* P$ q' E2 U% A" k - authorurl : 'http://9iphp.com',2 M8 x) ~1 I- @" b! K/ v, h
- infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',$ C. l4 r+ x" F) Y, V
- version : "1.0") o3 W* P5 J" `# z- f, M
- };
+ ?$ e ]$ X9 U$ q - }
6 o d2 y. j' Z. A - });
. P( e# H: o+ z5 b& p - tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);
9 T, {2 B' _% F- p* K - })();
銴鋆賭誨蝣 澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨 onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉 敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准 ! { a6 |; v; i% c$ y$ N* t1 u
4.2 閮餃TinyMCE隞
& b8 o' F* s5 r6 V9 u6 I4 W9 W9 Q5 a
嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉 蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩 - function register_button( $buttons ) {0 a1 H6 u% [& H% o4 g; L w
- array_push( $buttons, "|", "recentposts" );
: Y, D: r1 I! I7 W0 H - return $buttons;; S# ?/ }) Y8 z- I& _
- }
銴鋆賭誨蝣 蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅8 A0 Q" d* H$ x% n
- function add_plugin( $plugin_array ) {8 `, G1 I/ Y9 O' f" M1 u g+ j
- $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';1 e' u" S5 S3 }4 r( Z; ~
- return $plugin_array;' N! ^% J! K, S; A- b
- }
銴鋆賭誨蝣 銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁& [+ f( L% h0 Z+ V& y
- function my_recent_posts_button() { ; i2 G: C, v' R+ c
- if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {
, A# `/ n& ]; ]+ A Z. O. y - return;* f9 p1 T2 x$ [
- }) `; u- i- {4 ~1 a( x2 L
-
& O! _0 K6 J6 u# P( J - if ( get_user_option('rich_editing') == 'true' ) {$ w+ A& g0 V1 z! m0 b
- add_filter( 'mce_external_plugins', 'add_plugin' );. z, t6 j; P1 \0 W' P
- add_filter( 'mce_buttons', 'register_button' );* q6 Q/ U) x' t& z7 c
- } ) ~: N+ ` `! o! Z8 Q- H
- }
銴鋆賭誨蝣 嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁
0 m9 Z" z! o- K1 P1 J+ U敺賣詨曉訖ordpress憪賣- add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣 4 h, H4 T0 i+ R- Q& R+ V+ v
4.3 冽
. t0 ^4 [8 p2 y
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
1 {/ Y, y: L- v7 h. }
暺嚗敶箏閰望霈頛詨交蝡賊嚗
, F/ b h& p6 b0 d$ i
頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
6 b1 m# @* Y% y* a% w
貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
/ e$ E# M* z" N) v# ] 唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆 |
|