WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)
L3 _2 f0 `3 h! Q( c: U2 T1 T0 P4 P' x3 J
wordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐
2 v. I3 H- T# `9 c隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具
N% ~, e# o' e5 T* a
9 f9 j8 M' X% |0 g8 {凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :
) l+ a6 j/ v9 b5 h+ a0 N% }* }# o甇文 , 臭誑閮剖銝訾批嗆蝡賊 :, P0 m4 }- [" U8 n4 s
甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :5 h5 D6 c, Y% n0 n- }& L. ?4 w# O% [, {
- [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣
. ~& o/ U' n7 r. a4 c4 T. P) o0 C4 O( q
1. 蝪∪桃凋誨蝣* n. t6 M8 p% W# o
祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :
* i2 c, A4 w$ }. ~+ N* x閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :+ G; C2 r% k7 ?! Y! [' r
- 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
- 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
- 閮餃賣賊蝯針ordpressaction銝3 C3 ` l/ E: p7 u$ h! j; M# O
$ l$ p0 R- W7 b3 l祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准* j- W' m) D0 H3 Y4 V# h
1.1 撱箇餈賣9 ?# [# }( ^. @+ O
嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡
3 ?9 h/ K" d# W) J- e* X; R6 l- function recent_posts_function() {( O' f8 t+ h+ ]! P5 w
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));# @: Y8 ]4 l/ Z; W, t6 _0 K6 ^* }
- if (have_posts()) :
G4 ]' |1 G8 j' N - while (have_posts()) : the_post();' Z' u$ }5 t V' K( V, ~, T, t6 J
- $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';. o: U+ Y* Q% G
- endwhile;
; a/ Z. J c* I! B- C9 h - endif;! N7 Z/ n% U4 i. r
- wp_reset_query();8 V h3 \$ X/ J9 M
- return $return_string;
9 |' j5 o Z7 z/ g - }
銴鋆賭誨蝣 憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯
4 ^2 D, j, `$ p; j7 g* R$ Q& S7 Z4 f0 v& f2 l9 Y/ o/ v. K& w
" U' d" D- X4 S& s+ j3 s1.2 閮餃凋誨蝣
6 [. Y8 B7 m) ^+ _) X曉剁閮幃ordpress賣豢臭凋誨蝣潘- function register_shortcodes(){
* W* W& P! Q9 _* T' g, c. [ - add_shortcode('recent-posts', 'recent_posts_function');
9 ~: j- j- J0 @ - }
銴鋆賭誨蝣 嗅冽蝡批捆銝凋蝙函凋誨蝣2 V& e1 G y: K/ e
撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴
b6 S8 @, d1 o/ j0 ?7 C
f+ ^" x# E' h# ^& d% @6 H+ i( w9 [4 u9 P/ b1 g& H
1.3 Hook into WordPress
! O$ g6 B& h/ D# J
. W' C0 |$ Y- J0 ^箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准
( g* w+ z* b3 I, {- add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣 % k8 N. L6 Q8 p
1.4 皜祈岫凋誨蝣
, P5 e6 f% K- N& z蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :2. 脤凋誨蝣 2.1 凋誨蝣澆 凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻 閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼 撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 : - function recent_posts_function($atts){2 q0 S, t! f1 c& S1 K! A
- extract(shortcode_atts(array(
) ~$ N3 T) [- j, {; y8 V - 'posts' => 1,
! y. R: [# @# z7 e x3 f U - ), $atts));; t; l! x) z7 \! G. s4 `* b
-
( G2 w6 j4 _6 Z - $return_string = '<ul>';
! E) i7 w& K# E8 a& }7 ^) p - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));8 g/ g1 D$ z6 J# V
- if (have_posts()) :' y4 C' R4 o9 Q! p8 L8 D: B' z
- while (have_posts()) : the_post();
) J% v/ Z. V" V* Y3 M* G7 D f - $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
7 v" R! z* H. I2 C7 c4 ^ j$ G - endwhile;& E" t! P6 x' @, m' S
- endif;4 z) l# z0 L& O/ M
- $return_string .= '</ul>';
' Q2 p9 X) U' w; Q9 G) t5 S% d& S -
& d# }( a/ y# c. m0 j V& F - wp_reset_query();& o+ D% P4 k `
- return $return_string;
4 t9 o" V! S2 c, P; y( c4 v - }
銴鋆賭誨蝣 憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :
9 Z8 }2 G5 L3 j2.2 凋誨蝣潔葉憓摰
臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗 - function recent_posts_function($atts, $content = null) {
8 f, E9 N0 ?/ @ - extract(shortcode_atts(array(
" u9 ]% F- ]; h N0 q0 I# U - 'posts' => 1,
3 I t' J/ ~' z: e' i, p - ), $atts)); v# U( `- |! J4 r
-
% B3 t# F! Z9 ?5 q1 p; T - $return_string = '<h3>'.$content.'</h3>';
4 E& Z4 ~3 A4 Q" q, O+ X; f - $return_string .= '<ul>';" B; ?, j( w5 D0 t' A7 b& E
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
* R& ?, g( p/ |5 D* _8 m/ a& a - if (have_posts()) :
9 B8 E# F: d+ l: i' c - while (have_posts()) : the_post();
8 P1 N3 f( x0 \6 W- { - $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
2 o) u: {5 W. f C$ Y- s. m - endwhile;! y- U+ c0 A* @
- endif;
0 C5 v& a9 q; Y1 q/ W" @0 \$ E - $return_string .= '</ul>';
, M# K- S. I3 U7 [" @ -
5 E+ W" h& _ Y& Q- o" l5 m4 G - wp_reset_query();
7 A7 f) P0 U0 o3 ] - return $return_string;
$ `1 E7 O. C4 Z; o - }
銴鋆賭誨蝣 蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝- [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣 支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗 W) f% @6 z" E1 R/ T; k/ X% w" D
3. 其遙雿唳寥*蝷箇凋誨蝣 3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣 閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛嚗典臭誑撠甇斤撘蝣潭曉典撌亙 / x5 Y* e* ` S. t
臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 : - add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣 曉其閬靽格嫣遙雿嗡唳 , 瑟, 湧甈撠甇蝣箏圈*蝷
& }9 R$ M5 h# U' H/ ~璅嚗刻隢銝剖函凋誨蝣潘 - add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣 冽閬銝剖函凋誨蝣潘- add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣 4. TinyMCE蝺刻摩典凋誨蝣潭2 y. c( o' J! D6 A+ N
4 Y2 F+ @8 R2 Q! X: O5 X
嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗 - 箸迨撱箇銝 JS 瑼獢
- 閮餃JS隞& t- o: v' W2 } ~! j2 q
5 v$ P- W8 e( e
4.1 JS隞嗡撱箇JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗 - (function() {
4 ?; \ y# y- Z o - tinymce.create('tinymce.plugins.recentposts', {( K/ K/ p! v7 c( h+ e% c# [
- init : function(ed, url) {: R5 B2 K( q2 t# k6 ?
- ed.addButton('recentposts', {2 `% E7 L$ J& h7 u$ j4 P6 N5 _
- title : 'Recent posts',+ Q# A3 F$ h1 g( p
- image : url+'/recentpostsbutton.png',( {9 V) ^1 r) j9 V) ^* O/ f( j
- onclick : function() {0 P4 i3 d3 e8 c0 Y* F, l) l o! ^
- var posts = prompt("蝡賊", "1");
! j. p. U* Z6 ]/ m: W, b - var text = prompt("隢憛怠亙銵冽憿", "唳蝡");
! K+ Z. N& I) ^& [. { - ) E# Z# m" A* ^) Z9 h! k5 K
- if (text != null && text != ''){
3 z d- d) v0 l5 Z - if (posts != null && posts != '')" k8 s/ F. C0 N& `
- ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');
6 j/ X8 y, m$ L0 m7 C U+ M, L - else5 y! }- q6 T {/ U0 N
- ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');0 J) I7 a8 H+ Y6 S8 G
- }
, R! q. B/ p0 w1 @% A7 x* L - else{
4 }- @ A7 ?" G8 ]. m - if (posts != null && posts != '')4 P# B% q8 J! K
- ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');
, a9 |1 U; @% J2 j$ y& M - else
0 g& D. y& G9 s - ed.execCommand('mceInsertContent', false, '[recent-posts]');
1 C/ G7 V% W8 M# K- M& b' j - }
! a; s/ r% [6 D* V* A - }
- m8 @) \ \8 @" \! i - });& n4 R: d2 E, [2 u1 F3 c t- ~
- },; g6 [7 K( H: a) c4 z. I
- createControl : function(n, cm) { ?! y: [& @# f. M
- return null;4 @. {1 S, o2 g9 Z" I
- },
) ]6 p1 \, r# U7 E9 \ - getInfo : function() {
- l3 l8 H( u7 r$ u - return {+ s" _& M, r8 h. ^0 E
- longname : "Recent Posts",
/ t# @ K7 a4 L. p' \3 a- b* J - author : 'Specs',# U! }/ Y& B1 I# b
- authorurl : 'http://9iphp.com',) G1 n0 y4 u2 o
- infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',
5 _) S6 t u8 T2 F6 J+ N - version : "1.0"( O. w& h x, [
- };
6 k, k* @3 n: q+ F7 [ - }
4 [9 y9 y6 d% |; ], c) J, t - });- \& q9 T# e/ u. J9 K+ r7 r
- tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);
( D: w6 o+ o. n6 o: H# z+ W( Z - })();
銴鋆賭誨蝣 澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨 onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉 敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准
' N, K5 O8 n. {/ a3 ]; _! ~1 H3 C6 I
4.2 閮餃TinyMCE隞
; A' z. @8 b5 L
; B% [) ~, K ?3 G8 i# o$ a 嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉 蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩 - function register_button( $buttons ) {
2 r& L2 g+ D& p) k* [ - array_push( $buttons, "|", "recentposts" );' |% N+ B/ J% N+ y& r
- return $buttons;
/ r7 s- s$ ?! E& [) t - }
銴鋆賭誨蝣 蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅7 S; @% s8 I0 u/ O
- function add_plugin( $plugin_array ) {
" b# m6 C7 T# p ]- X- I - $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';
3 M2 S, b D; m - return $plugin_array;5 C, ? c i* |3 h2 `. `7 k% O
- }
銴鋆賭誨蝣 銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁
+ a$ ~0 T/ Z6 n- function my_recent_posts_button() { " i* @6 i# [4 Z2 L% Q# V/ m
- if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {, s. J! t4 r( U4 q* Q6 O. }
- return;4 C1 g1 T+ s3 L% x& d2 s1 e4 D
- }/ ~! b+ @/ p" [8 S
-
6 R/ @+ F, }% I - if ( get_user_option('rich_editing') == 'true' ) {! d) i$ A4 H' K& O3 p
- add_filter( 'mce_external_plugins', 'add_plugin' );
* L( ?/ n @9 m" S5 c - add_filter( 'mce_buttons', 'register_button' );
* J9 E# l) c1 W+ {/ D - }
( f; P! t4 b. m, p+ \( e% h0 v, D - }
銴鋆賭誨蝣 嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁3 V6 q. m0 E# C' E
敺賣詨曉訖ordpress憪賣- add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣 # D$ g% o+ O) T- J% A7 w- [4 P
4.3 冽6 b) Q" s: u; o. E7 n. O
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
- ]% N& o- V) @$ I2 w 暺嚗敶箏閰望霈頛詨交蝡賊嚗
* Z7 v; N. t& i7 H3 u 頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
7 C1 d/ }5 f8 I2 u1 M 貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
: ?- k2 Z, i5 w5 h8 P( A# j
唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆 |
|