WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)4 j5 l+ I" ~$ y m
( X( o$ |8 b! p I
wordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐% G% ]! |( I5 @0 |7 @
隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具
* z) x$ v% d' }8 @/ U. \ X. g, ^# o$ ?: p. ?
凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :
% ^) v$ s# u* [+ L1 J; g! B甇文 , 臭誑閮剖銝訾批嗆蝡賊 :; j$ \4 Q4 y; U" a5 }: G
甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :
9 Y9 W, R. i1 H- [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣 q. k6 i1 V3 S3 o# y4 I
% F/ g- C" y$ U. l
1. 蝪∪桃凋誨蝣
1 M8 ]# _) v/ ^祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :' n6 V" K' J# B- @8 W, d0 e
閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :, Z" p5 F/ i7 q. f0 ^$ m
- 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
- 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
- 閮餃賣賊蝯針ordpressaction銝" [0 b) a4 M7 S& Q0 [
+ U: y8 ~7 f5 F& u& @. {8 k
祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准
* \- n* _8 m, m3 {, U; y. v' r, Q1.1 撱箇餈賣
( f$ B, A8 y; U嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡
/ H8 R# z5 \! I" o m5 C3 J- function recent_posts_function() {' t5 [* s" n Q6 v0 D
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));
4 \) _7 A2 q/ E8 C - if (have_posts()) :
- @" f" D& x a) \ - while (have_posts()) : the_post();5 I' c5 H' V" ^8 ~
- $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';
4 `) W) M* o% V4 Y* [ - endwhile;
# o) W; \& E& L! o5 l; x/ g7 A4 G - endif; b' u0 P& B& [$ f- `9 |' v
- wp_reset_query();
" N& o2 }9 q9 H+ _* i: ~' V - return $return_string;
/ ^" Q( u# j" z, j* d; }( j - }
銴鋆賭誨蝣 憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯
5 G' n: v: f: }" X
. V$ [$ n$ Q" f/ n" R( _1 ~' I
/ D) K' C/ z9 a7 |, k# U1.2 閮餃凋誨蝣/ J+ J$ C8 ]4 r" J& ?8 Q8 t+ C
曉剁閮幃ordpress賣豢臭凋誨蝣潘- function register_shortcodes(){- d( u1 N' m( ^! ~' M
- add_shortcode('recent-posts', 'recent_posts_function');1 F5 _- W& R* |/ M3 M, X, K
- }
銴鋆賭誨蝣 嗅冽蝡批捆銝凋蝙函凋誨蝣
b" o% j. [5 d& y2 q撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴
, y+ X! e1 f$ a" l3 j$ S/ w8 d, q2 ?! g
% a/ X4 Y: p* Y% J1.3 Hook into WordPress' C) b* x4 s @
1 _5 K6 a) {* F6 V2 P7 A% `箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准3 h/ e# J, A0 q
- add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣
: ~" }5 ]$ `" a2 b% i1.4 皜祈岫凋誨蝣" n- e: g# ]) ?: n+ K# Z
蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :2. 脤凋誨蝣 2.1 凋誨蝣澆 凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻 閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼 撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 : - function recent_posts_function($atts){
6 u# y. c0 J* v0 s e, } - extract(shortcode_atts(array(. D2 E, ]) V/ u: Q7 W7 w8 e; n+ _6 U
- 'posts' => 1,0 g. C2 } j% W- o' J# @
- ), $atts));
8 t/ V' k! V0 e4 | -
# E2 `- j/ U7 } - $return_string = '<ul>';" o6 c# n2 T2 j# Z0 l; w4 w
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));: L% I9 [. q4 z7 b
- if (have_posts()) :
9 t' i% f- P$ o7 V! f - while (have_posts()) : the_post();" Z7 c: z* n: }+ Z9 U
- $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
# `) g9 ^+ \# F, E8 l' \! {* n% `: e - endwhile;) @8 ?* M+ j% T. E" a y
- endif;
7 ^2 }& P- F! O: j& d - $return_string .= '</ul>';
* |8 I& S# Y6 r -
; H( `' ^# Z2 N - wp_reset_query();7 Q& b! G7 o7 w# S# |. u
- return $return_string;
3 Z/ Q. I8 Z6 \7 k: ?% ^ - }
銴鋆賭誨蝣 憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :
; `4 K: ], ]6 B3 j' N0 X2.2 凋誨蝣潔葉憓摰
臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗 - function recent_posts_function($atts, $content = null) {, I, ` T; k: R% s, \0 |8 U' O
- extract(shortcode_atts(array( ~* \2 {2 G( ~0 D- @) x# G7 s
- 'posts' => 1,
4 a& n8 C: U2 J- Q& ]/ y M - ), $atts));# p& T/ ^9 t3 X5 P1 ]( i. t# Y
- * S" G8 J$ k% I- y
- $return_string = '<h3>'.$content.'</h3>';
' f7 ]* _- o8 T$ [1 l& G - $return_string .= '<ul>';
+ u5 c3 Z7 B* W7 R# O" M: ]9 D - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
& y, A. U: X9 h# k9 Y - if (have_posts()) :$ r/ V9 p. h) C3 P. B: U" D2 y
- while (have_posts()) : the_post();
/ Y; q+ N( \) V, M" ]2 s5 j6 c - $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';+ M$ H2 o/ u* E0 q) K& c
- endwhile;$ K" [# h Y+ e0 Q# s* Q
- endif;
4 ?: P, v0 A$ M% K5 A) Q8 m - $return_string .= '</ul>';
7 @" e, O% |: j+ v" ] -
3 ]0 ?5 J( o0 U: u6 n: G/ k - wp_reset_query();
9 I+ h9 t, R( D - return $return_string;& D0 ] a; y9 q. a( k( s- t( x
- }
銴鋆賭誨蝣 蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝- [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣 支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗) N* z/ h4 b# b6 R! L
3. 其遙雿唳寥*蝷箇凋誨蝣 3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣 閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛嚗典臭誑撠甇斤撘蝣潭曉典撌亙
, h+ B/ D0 }8 N5 ~臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 : - add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣 曉其閬靽格嫣遙雿嗡唳 , 瑟, 湧甈撠甇蝣箏圈*蝷
2 h' ?% Y& S, J$ g璅嚗刻隢銝剖函凋誨蝣潘 - add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣 冽閬銝剖函凋誨蝣潘- add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣 4. TinyMCE蝺刻摩典凋誨蝣潭
' U1 z# j' d6 |5 Y, y4 G4 q; I" O/ x, K
8 G7 f/ Y! @3 j& s0 F嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗 - 箸迨撱箇銝 JS 瑼獢
- 閮餃JS隞
+ D3 f V- ?% ^" e* }
' R# [/ K; Y: N; D4.1 JS隞嗡撱箇JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗 - (function() {6 k, O4 P2 F. {- z3 z$ c6 Z
- tinymce.create('tinymce.plugins.recentposts', {
* O8 L! ]! E3 W, X0 q! u4 N, n - init : function(ed, url) {( A o3 l3 t. B' k
- ed.addButton('recentposts', {! d ]2 v) G6 G6 n
- title : 'Recent posts',8 [) A" |, ~* P% |7 E8 T# l$ I
- image : url+'/recentpostsbutton.png',) u0 ]) Z* Q& c
- onclick : function() {
( w, O* X. v! [0 F6 C4 E: W: ] - var posts = prompt("蝡賊", "1");
( O! C1 `/ O0 C+ O8 r0 ^ - var text = prompt("隢憛怠亙銵冽憿", "唳蝡");( Z# a, j: _' Z t
-
5 j5 H# c8 `9 k2 u - if (text != null && text != ''){2 H) z, |) ~1 Z
- if (posts != null && posts != '')
- F& D" X( q: F - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');
9 C7 b9 U" E& {7 J6 i - else% R( }8 `9 P- `7 c9 }5 }
- ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');
$ J. ~0 J4 V1 D8 x! d) J8 j# x - }
0 b! ~$ B2 Z" A" M3 t7 b7 K! r - else{
2 O+ N0 g. g% m& q - if (posts != null && posts != '')" }& k4 _8 f# M. y8 \/ S# k6 W I
- ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');
$ N7 {; |$ ^. D7 C% E& j4 e - else0 Y% F$ ^- n4 E" b, P, t
- ed.execCommand('mceInsertContent', false, '[recent-posts]');
, J. A" K0 h4 P, P - }
/ i% Z D4 r: G6 I0 z4 ^ - }+ t& a! [* C, a# v. }3 |
- });
5 y; j6 J9 d8 D( G6 x' o - },
1 Y1 q6 x2 s- v; O7 f w0 t# a! ] - createControl : function(n, cm) {
2 p4 K7 R4 R8 C3 H( v8 D* T - return null;1 c1 P3 g# Z' X$ T/ u2 i2 D6 R
- },+ k: P4 d7 o4 |' r8 C2 O: X' A h
- getInfo : function() {
& U# j9 V( }5 q+ h8 E! t* M, d - return {$ W4 c. U4 X' L
- longname : "Recent Posts",3 c' @6 s3 G. q2 h( G: c9 S" I
- author : 'Specs',
* B) W3 ?5 e- E8 A P - authorurl : 'http://9iphp.com',) G0 L6 d9 C* l
- infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',% Z, f1 }+ ?0 W6 r7 `& L) p4 D
- version : "1.0"9 {0 e& X( i- y) G$ ^. Y- L
- };. @8 T* z9 s8 f# y5 U# v* S
- }
8 y% i( E2 Z! X3 q. d/ b5 ?- s - });1 T/ f2 B" o M
- tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);# q5 @; U. `5 L" g1 v
- })();
銴鋆賭誨蝣 澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨 onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉 敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准 : g/ k# R/ P% ]
4.2 閮餃TinyMCE隞" J& o% p! U* J" o
( P4 Z: R( s, T# o 嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉 蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩 - function register_button( $buttons ) {2 g3 V J, }5 q' k
- array_push( $buttons, "|", "recentposts" );7 Z/ o( o& p* E; C+ k0 t- L
- return $buttons;% x) S6 q1 F# r8 t
- }
銴鋆賭誨蝣 蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅. ^: m* d% J1 h
- function add_plugin( $plugin_array ) {' g# u8 S1 H5 m# G7 n+ ^
- $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js'; \4 A2 }- a6 u' k5 C. V4 H. Y
- return $plugin_array;
0 L: o) M" |3 v% C; S7 ?- i - }
銴鋆賭誨蝣 銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁
; J% z% q) K* M+ K$ O2 ]- \8 ^- function my_recent_posts_button() { : p% \& `4 v& t- n
- if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {5 J5 z) @& [; I8 e
- return;
7 ]+ [& B$ h' k$ @ - }
% a( U3 x/ {# ^7 ]2 N -
, P1 b9 O9 z# b' | - if ( get_user_option('rich_editing') == 'true' ) { G; z2 \- s, U% l9 q7 Q
- add_filter( 'mce_external_plugins', 'add_plugin' );6 e! N/ F' c, v9 c; H# k% r
- add_filter( 'mce_buttons', 'register_button' );
# `" V/ R. _/ D9 y6 w1 D - } ; T {4 s. T, p6 Z( f! b3 n, L
- }
銴鋆賭誨蝣 嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁
5 I' _$ x, X; B+ U" Z& i敺賣詨曉訖ordpress憪賣- add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣 6 T# I# B' U b3 F. U- t* ]
4.3 冽9 E( V( w) H; y2 N' A* _5 ~! _; F
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
2 `% Q5 v. R) e- T$ m. Z$ G 暺嚗敶箏閰望霈頛詨交蝡賊嚗
. P ? C9 a; U+ i% q 頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
8 w3 L4 B" M* E& A8 i; ]
貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
& S. S% q, i% ^$ a% H 唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆 |
|