WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)
$ u. r, s# R( B6 y' l
) H2 W2 I+ n/ i( g, fwordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐3 ~$ Y) B$ T. F% a
隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具6 _' G/ M/ @; j$ W5 z1 }- u6 u
- A# i8 j6 s1 h( Y凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :
4 V3 [/ @8 `. h' _) c' Y3 J甇文 , 臭誑閮剖銝訾批嗆蝡賊 :5 J# j; E b0 x1 m
甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :
% i' C' {. z" b# x& C- [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣 9 L% V0 }9 n- E# F+ H5 o; Y9 M" l
$ u& T1 U( D) U& L
1. 蝪∪桃凋誨蝣2 Z3 d. n* W" ^+ ^( @! _
祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :
6 l5 K3 Q/ _+ U( W" N$ S: L+ ]+ e9 y! o閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :
g2 I8 G- q, Y- 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
- 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
- 閮餃賣賊蝯針ordpressaction銝
% w) s/ |& u' ], \" K, e # i6 ^$ { Q0 m" f) g
祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准
2 T0 Y1 x% ]: L2 i' H% l1.1 撱箇餈賣
. m9 D. f; s+ g9 d F" X: d( U" ^嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡& G: f% f- i' u
- function recent_posts_function() {
- k! m% S/ T C; }6 b7 ?( p - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));
& H% ^) u) K' ~* b3 P4 a' k - if (have_posts()) :
1 A- F9 t/ B1 e; q w$ ~, J9 }+ b - while (have_posts()) : the_post();) k9 y4 a. R8 B/ W7 Z4 v
- $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';
& g' e: p) P7 S8 H5 L6 B - endwhile;
+ ~& A. d' t6 ^4 j. }, { - endif;
- N& o# Y' N9 S7 ^: i9 V% ^" m: a - wp_reset_query();* T0 ^. U$ ~# m1 Z; ~ _- i% ~/ @
- return $return_string;% q; Q: r& o7 A5 i4 D; {
- }
銴鋆賭誨蝣 憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯
; R2 k9 ]7 T2 J
6 j5 p% ~6 P: h/ o+ W- W! W4 o; K
1.2 閮餃凋誨蝣2 _9 i; M* J: \& h
曉剁閮幃ordpress賣豢臭凋誨蝣潘- function register_shortcodes(){
2 G5 t9 y' S# g2 s' I - add_shortcode('recent-posts', 'recent_posts_function');
' l2 ~+ }! L: u - }
銴鋆賭誨蝣 嗅冽蝡批捆銝凋蝙函凋誨蝣# t1 @1 k+ X( G8 |, P. w
撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴
; D+ p. b: S! P o, }4 ?6 [6 c, i! \- U5 ]; P* b) I2 q
* x4 L7 s" P- S7 a" f: A
1.3 Hook into WordPress) i- ?! b4 s0 {9 D
6 F4 l. o D; V' S$ n4 }
箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准
& i# v' ^0 [! \$ @5 E5 f& g: I- add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣 / q+ {$ B" B; Z
1.4 皜祈岫凋誨蝣
' m/ P/ P/ R+ w' {蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :2. 脤凋誨蝣 2.1 凋誨蝣澆 凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻 閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼 撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 : - function recent_posts_function($atts){
. \% F/ X" b3 R4 M9 \% Y0 w - extract(shortcode_atts(array(6 Z4 J# O; f& H
- 'posts' => 1,
; |9 K/ x& D0 E. V4 T/ \0 I: q& r - ), $atts));# s: w1 g3 c7 W& x) `2 M
-
9 k0 t1 x! p- m! W7 h" x1 T$ U - $return_string = '<ul>';
! Q9 u/ T9 {# @: g$ {$ O - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));) ~$ Q1 h& e y) y+ J
- if (have_posts()) :
0 w$ M2 @- t Q2 w* f6 c$ p9 f9 e - while (have_posts()) : the_post();* I, f. z3 q* U o% `; ]. z3 g
- $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
$ K% ?) F( g0 X3 ^' B3 M - endwhile;2 s3 L, s* [5 A' H9 ~/ O# B/ E" ]
- endif;
( v6 c4 M) y5 M- d; y, ` - $return_string .= '</ul>';
: H3 x7 i+ ]; a3 s# M/ q - ! W2 ~7 B& R. |& C" e$ n+ Q' |
- wp_reset_query();
9 m- v# k: S& J& f - return $return_string;. G' u+ r* j! ]) S& C+ V2 N4 b# w
- }
銴鋆賭誨蝣 憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :
9 v) M: L) O0 X3 F$ V8 U- p2.2 凋誨蝣潔葉憓摰
臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗 - function recent_posts_function($atts, $content = null) {
7 P' Z0 Z7 V+ y2 K# t - extract(shortcode_atts(array(
- }; s3 }( S- E8 l. V; F# o - 'posts' => 1,9 U+ A& l0 r0 g2 @: o% W5 q. l1 O
- ), $atts));
) w) H7 f) R, B& j -
- s$ i5 W6 f/ i - $return_string = '<h3>'.$content.'</h3>';
" G+ U) X8 @5 L' i" @ - $return_string .= '<ul>';$ K" j" q2 @( H
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));) j) S6 G8 s6 J \
- if (have_posts()) :* T. c; w6 T+ _% ^
- while (have_posts()) : the_post();% @) i: e9 ]* L: l# @# i
- $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>'; U& u ]) {, C* X& [; p. z X
- endwhile;
/ N' |$ R: u* ^" n - endif;
9 \( d9 t N# l% s- o - $return_string .= '</ul>';
) x& Y( {3 o) G2 i* `" G7 ~ - 1 _2 r# Y2 q! _5 L7 f" `" t2 n7 p+ K
- wp_reset_query();
% X' ?' R& Z# h3 t - return $return_string;
8 t1 E$ w3 l# h# g/ I: B. @, @ - }
銴鋆賭誨蝣 蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝- [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣 支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗) l" X6 ]+ V- Y: c0 l1 y( {: q, B* Z
3. 其遙雿唳寥*蝷箇凋誨蝣 3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣 閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛嚗典臭誑撠甇斤撘蝣潭曉典撌亙
2 U4 g' e' ~! ~, ?4 @3 N臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 : - add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣 曉其閬靽格嫣遙雿嗡唳 , 瑟, 湧甈撠甇蝣箏圈*蝷 0 i! o! @" U% e. T3 b. n; Z. M
璅嚗刻隢銝剖函凋誨蝣潘 - add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣 冽閬銝剖函凋誨蝣潘- add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣 4. TinyMCE蝺刻摩典凋誨蝣潭0 G9 B# X% M" ~5 [2 T3 ]
: |# C: M6 G( A. W( ?4 o
嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗 - 箸迨撱箇銝 JS 瑼獢
- 閮餃JS隞
5 ^6 F ]& C8 q& z; _
4 |: o6 z8 s8 x7 J* n! E9 [' o4.1 JS隞嗡撱箇JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗 - (function() {" s/ K% Z- d/ g) F# a
- tinymce.create('tinymce.plugins.recentposts', { d4 f! @6 i: K @1 m) Z }
- init : function(ed, url) {) ^, L: v; ? \" v
- ed.addButton('recentposts', {
1 L: A5 l1 o( {! d. I' C7 P - title : 'Recent posts',1 ?! h7 h1 O4 A2 c
- image : url+'/recentpostsbutton.png',
( u3 e9 S/ U: d$ t - onclick : function() {
/ T/ l& j/ d; e' N( |6 z) R6 g - var posts = prompt("蝡賊", "1");
3 h8 k1 M* @; W6 |+ T0 q2 T - var text = prompt("隢憛怠亙銵冽憿", "唳蝡"); n b W8 v' ]7 _( W7 D# ]
-
( z7 w+ ^) z R3 w$ ~3 D* C+ J+ w - if (text != null && text != ''){: t$ K( | g. @
- if (posts != null && posts != '')# j/ j! M; F$ V0 Y+ p
- ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');
. \1 ^. [, h$ M) o - else
6 r( a5 ^3 [3 \& s( o% V$ \6 A! P - ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');( v: w0 H( f1 l. H$ f
- }
; G" S, u' [' [ - else{
1 [6 b; o5 h( c - if (posts != null && posts != '') L! C+ {. h& p, f
- ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');
; x7 j: c6 A5 H4 y4 V6 m - else
4 T0 w! z* A, G5 E8 r9 n3 T - ed.execCommand('mceInsertContent', false, '[recent-posts]');! N. e9 w' a9 R
- }5 k9 h5 F% ]8 Z( M
- }# l @1 ~' K* n) @# M+ _) c, `
- });
5 o p7 }! G0 y+ x0 S! }7 h - },
8 f" `, x7 v7 {& ^+ a6 G' M7 h - createControl : function(n, cm) {2 y" E3 N, r! h1 U/ n0 `9 {
- return null;. T$ ^% F( A: n! s% i7 @5 V" }
- },6 o# x" J% d K% ^$ j6 V6 N
- getInfo : function() {
, a' w" G& i3 h9 V - return {
5 L- R: `( F+ Z/ |+ a - longname : "Recent Posts",
: y# h' |0 x; S! b - author : 'Specs',+ ?& V5 p$ l) u3 g7 l
- authorurl : 'http://9iphp.com',4 w# p( d& J& T5 |' I
- infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',
) ?! ^7 W4 h5 L' t. O* U, S - version : "1.0"( K/ |4 P$ n" P5 `$ P! f
- };* x7 d3 M! e" m
- }! S1 M8 V: N U. L, T
- });
4 a% @6 M0 D3 I& T$ I- L @& U7 p" { - tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);
0 f3 b5 {; S$ V1 ?6 M - })();
銴鋆賭誨蝣 澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨 onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉 敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准
7 W+ k5 n% W3 E& H" d. m9 b% C
4.2 閮餃TinyMCE隞
4 e5 R1 e& H2 V! W2 ?6 ]" [) n8 G/ q3 ?- }3 g" v
嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉 蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩 - function register_button( $buttons ) {
5 N+ y2 @3 b d* ^! d/ l - array_push( $buttons, "|", "recentposts" );
4 L" o4 q6 o8 d* b+ O7 \% r - return $buttons;
" E: c: o; s. J' t! N% r - }
銴鋆賭誨蝣 蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅
! g) L. ]5 o9 m- l: f- function add_plugin( $plugin_array ) {
, N% E, {7 G- Q9 s2 w' w8 _ - $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';6 T t$ _0 Y- I) B6 Q `
- return $plugin_array;) o' ]4 K8 q, Z! M! I+ M; Q$ U0 t
- }
銴鋆賭誨蝣 銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁
6 i5 c/ i! r5 `6 ]) g1 b! X9 ]3 l- function my_recent_posts_button() {
' K9 r* E* u- Q! Q. o9 G - if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {' Z+ `* E4 e8 L
- return;
! t/ d' K) F5 A, u5 b5 l4 [ - }7 O: n. m0 h' t
- 9 B' E- P% s& a Q' S
- if ( get_user_option('rich_editing') == 'true' ) {+ G) [+ x$ y( k! h4 Y+ ]7 f
- add_filter( 'mce_external_plugins', 'add_plugin' );
$ Y- Z' L- e5 ?& C+ W - add_filter( 'mce_buttons', 'register_button' );
3 L% u7 J! q+ |5 x% a6 i - }
5 O$ m% F, T. H8 O8 s - }
銴鋆賭誨蝣 嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁
% q& V# z( E Z2 w: f6 n敺賣詨曉訖ordpress憪賣- add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣 + O# D. l4 C5 x7 `9 y
4.3 冽
- `; P7 v( ~1 [+ o. @
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
% R+ U2 N1 ^! `- \* c4 Z
暺嚗敶箏閰望霈頛詨交蝡賊嚗
9 K: O! f6 I6 @/ e) g* S# c 頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
8 q! ~# V1 X8 o# V. I" h2 B( g& Z
貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
8 k% L7 m. j6 ?* u0 Q! ? 唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆 |
|