WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)/ N7 S: z M- z) A
* M! ]5 w* \* K0 H3 m2 M
wordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐
) k/ g; ~0 g4 [' \5 y( P8 D |隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具# A& w4 \6 ^/ A8 u) z( r7 ^
4 J4 P# F% j3 l1 N; T! }" R- w; r凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :
" v" j# ] y0 U# R# {甇文 , 臭誑閮剖銝訾批嗆蝡賊 :4 ~$ p5 |0 M6 @) @' R8 @
甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :
# m: O( n+ b# t- [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣 o M0 N& H Z% h
( `5 t6 R( Y& K% D' F. @5 x; S9 Q1. 蝪∪桃凋誨蝣+ y9 ?, R7 p' _: t, r* b8 b3 S7 r9 z
祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :+ F9 I4 k9 v. |; [( _
閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :2 e( {2 A! p8 j# Z+ }0 c. V3 f- ^
- 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
- 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
- 閮餃賣賊蝯針ordpressaction銝
. J4 r3 q" {' ?# B + G& _4 i! D' K" X* G
祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准) u# b+ {2 s: H% C) [7 R
1.1 撱箇餈賣: _% M+ t. F) [* y
嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡! f; @* ~# Q. u* R) R+ C7 X
- function recent_posts_function() {4 g( M# n, F5 H1 a7 J
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));# z& M% K7 q# x9 K8 O: T* S
- if (have_posts()) :
7 a( t* ~# ~% N. H2 ?2 D1 ?. `+ l& Q - while (have_posts()) : the_post();5 V3 D+ t! ]3 V/ {( M' }5 g
- $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';
e5 E) Y* B t8 C. w: p5 W - endwhile;
7 W' }, P2 P" `7 f. k- D - endif;4 L. J$ q- Q$ G
- wp_reset_query();
6 ~4 v! V1 j9 \- J) F! W& l - return $return_string;3 d& T4 `4 d+ g- |& z$ n( I
- }
銴鋆賭誨蝣 憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯
; L+ m" @! _: }
/ Z+ b/ E' O; l( G, r+ v* x' a% @( y$ h7 Q
1.2 閮餃凋誨蝣
2 \/ C6 {% d/ z曉剁閮幃ordpress賣豢臭凋誨蝣潘- function register_shortcodes(){" I7 D9 @* I: Q1 O
- add_shortcode('recent-posts', 'recent_posts_function');$ \ w( x# T! K: q6 b
- }
銴鋆賭誨蝣 嗅冽蝡批捆銝凋蝙函凋誨蝣
1 ]& N! {- o' I. f撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴( `, q& D/ I4 v9 ~5 H
8 {* ^: u5 y3 I! P* v; |& J4 P# Q: n( C: a, v
1.3 Hook into WordPress' P& A3 S9 i( C6 Y
- g, j/ L& B7 I$ Q箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准9 C! z0 {9 _) c! w a
- add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣
4 S6 U1 w7 `9 x1.4 皜祈岫凋誨蝣0 |0 H4 H# U* @2 T! x9 G) F8 i# @
蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :2. 脤凋誨蝣 2.1 凋誨蝣澆 凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻 閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼 撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 : - function recent_posts_function($atts){& B* a& _; `: y; s: V! Z
- extract(shortcode_atts(array(! N5 g3 \' M! h X D9 S+ Y
- 'posts' => 1,% V1 F8 j9 A* H
- ), $atts));
2 o4 c7 k. j+ j3 R r - 8 L0 J$ t0 \+ D( W- j3 k9 t: B
- $return_string = '<ul>';% Z# z" y( t/ w6 g
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));: y Q9 d5 Y% ]/ \2 w* f# I* N
- if (have_posts()) :
8 [) u- O! f' D) A" ?: a8 W - while (have_posts()) : the_post();/ X+ x/ i9 k+ Y- ]3 }. H- Q
- $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';+ m% z, P9 U/ i3 m U
- endwhile;. o: [8 v! D* p5 k- n f
- endif;
3 @. x# y r0 ]% g) H - $return_string .= '</ul>';+ i- l$ Z, F% S9 T' i
- ) ?/ I. \! I5 i! M% Z4 M
- wp_reset_query();
* E, ]$ \- k( V# x - return $return_string;3 Z+ I7 Y" |; c3 L
- }
銴鋆賭誨蝣 憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :% q& {& s' _" f
2.2 凋誨蝣潔葉憓摰 臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗 - function recent_posts_function($atts, $content = null) {6 o: c- c3 D" N6 @" r5 R% m' F
- extract(shortcode_atts(array(
6 @5 e) m4 A# L/ e+ C5 S/ N+ K - 'posts' => 1, ^/ \0 a3 u3 j% {& q, X
- ), $atts));
7 Q* {: m) n, k3 w5 ]6 x) T8 W2 r -
! y3 Y% E9 j6 c! W - $return_string = '<h3>'.$content.'</h3>';
! N2 a+ z/ x. j, m6 u# K - $return_string .= '<ul>';, S! A! F( |3 Q# m: X
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));" [, ]# X2 L% Q) d! P4 x' e; e
- if (have_posts()) :& F4 O/ s" D+ a# h
- while (have_posts()) : the_post();4 w6 _- J% @) O5 g) A8 Y# H5 Z
- $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';0 t+ D1 T; f5 k. @- i
- endwhile;
* E% ?, \* h. X ]0 t' |8 E2 i% J - endif;# S+ e# i/ Z6 E \( L2 ]+ t' t
- $return_string .= '</ul>';
4 B. {0 N; p5 ~( u& H -
# `+ {1 m9 h. b" L: } - wp_reset_query();# X9 A V1 L& |8 u7 E6 ?+ G( f& J
- return $return_string;
% k5 s9 _3 t: E& f; y' e - }
銴鋆賭誨蝣 蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝- [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣 支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗
) C5 b4 v4 W4 A7 l- k* C. E, b+ O6 o2 R/ |3. 其遙雿唳寥*蝷箇凋誨蝣 3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣 閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛嚗典臭誑撠甇斤撘蝣潭曉典撌亙 ; x0 G$ ]+ Z* u$ c
臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 : - add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣 曉其閬靽格嫣遙雿嗡唳 , 瑟, 湧甈撠甇蝣箏圈*蝷
0 d& f w: A. Z8 D7 W璅嚗刻隢銝剖函凋誨蝣潘 - add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣 冽閬銝剖函凋誨蝣潘- add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣 4. TinyMCE蝺刻摩典凋誨蝣潭
" l8 Z' Q3 k1 P0 _; C( Q! i( L/ b# ], K& o3 ^1 p: H
嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗 - 箸迨撱箇銝 JS 瑼獢
- 閮餃JS隞6 U0 S8 `- w& F( U3 U. x
8 P' m" n) W% ?! y3 A. [4.1 JS隞嗡撱箇JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗 - (function() {* ^; C- ?7 D' b4 u4 z* w' N, b3 A
- tinymce.create('tinymce.plugins.recentposts', {
3 I8 a F' Z3 n- l; c' U - init : function(ed, url) {+ H% a4 D% b* [. E i
- ed.addButton('recentposts', {, O9 j# k6 I! |
- title : 'Recent posts',) V* `7 P1 t) I5 L! e7 d
- image : url+'/recentpostsbutton.png',& G- c: F6 h$ W% K! E& ?; \
- onclick : function() {
/ y4 Z; Y6 h5 s/ ~* `. Y - var posts = prompt("蝡賊", "1");' _) Z4 K, f/ e1 {9 Z( i9 f8 O, L, t
- var text = prompt("隢憛怠亙銵冽憿", "唳蝡");
( C% n/ F) r" R# j, ] U, F$ m4 [ -
+ I' O0 l& f" i- L& v' w - if (text != null && text != ''){6 c( r8 @3 K2 r9 C7 Q
- if (posts != null && posts != '')
3 r5 V2 q7 {# e9 a$ | - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');
* t- m9 Z2 H' J4 j/ g& \ - else" n% i; z; ?3 L" d5 K
- ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]'); e q% Y& Q7 |1 F
- }/ m% G- B# y# ]; x& B6 A# u5 T
- else{
D* s( p; h2 a: X& M% O& i* {) P/ x - if (posts != null && posts != '')2 b, G; F3 D! S
- ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');* V8 z9 Y9 |2 s+ z! f( r
- else
* q) `8 e9 @; Y3 l" D - ed.execCommand('mceInsertContent', false, '[recent-posts]');
1 y$ ^: Q/ x' C: k - }3 U c5 `2 _8 {
- }
( j# B2 }! u3 ~5 m# x - });; @8 b8 @2 B; Z8 [
- },, Z4 _" z ? v$ q3 G# y% @$ c
- createControl : function(n, cm) {
3 V# h O( ~* c! O/ v1 ~, i - return null;
' R5 ^. [+ }7 l+ T6 ^ R+ G2 J+ ?* U - },) s' j7 A7 N' U; v2 I! ]7 f- \
- getInfo : function() {
# }$ w" p. m% Z, W# N' V6 q - return {: u4 b) @; f; P. D# Z8 O% `
- longname : "Recent Posts",
8 N: \* S( ~ W! t - author : 'Specs',
+ N, q& N3 g3 l7 G( B" t ~ - authorurl : 'http://9iphp.com',
* b( u5 a) @6 l7 z( f7 ~ - infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',
1 y% l$ j$ y" H+ G - version : "1.0"
/ ^; S+ q+ c% L+ \4 f - };
0 n) f$ z: u" y. W9 c9 j - } Y- ~4 p, u& M6 [. r9 {$ S: U0 l
- });7 i/ o0 ~8 ` k f; ^) O1 K
- tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);, E& g6 W4 K& j
- })();
銴鋆賭誨蝣 澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨 onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉 敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准 * z9 U; g" A h0 k, k
4.2 閮餃TinyMCE隞
' }% @: b. ~. `# m6 C3 r4 [2 ^9 y0 C8 [& A0 D/ D4 `4 e
嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉 蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩 - function register_button( $buttons ) {
0 t S* R& i8 L - array_push( $buttons, "|", "recentposts" );
7 R* y, D; L7 \( q. M; o% j8 ^ - return $buttons;
, `/ G" ^( X$ `, |9 J) z1 Z5 i - }
銴鋆賭誨蝣 蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅- Y4 w, T& L/ H: m3 k, T6 w. ]
- function add_plugin( $plugin_array ) {4 a Q3 Y- t5 t: @
- $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';
* J; P. b7 {; J: B) e/ B" ? - return $plugin_array;& e6 y# ~% y7 g" ~& w
- }
銴鋆賭誨蝣 銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁9 J( b& [( D0 Z
- function my_recent_posts_button() {
& n3 o# P' r3 L4 `( [ - if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {4 s- P8 O/ ~0 v5 S9 E g4 E/ d9 ~6 \
- return;8 e. o; V: Z# }; a0 W# r* }
- }& Y+ @) s3 v1 ]! B
- $ @6 @: V, s, u3 v: l9 v; U- P
- if ( get_user_option('rich_editing') == 'true' ) {( n$ `9 a7 d# D& K$ C- ^* W* g' H
- add_filter( 'mce_external_plugins', 'add_plugin' );
. @) e# N" I, C6 y2 G - add_filter( 'mce_buttons', 'register_button' ); g0 ]/ W; m$ D/ Q# \4 `: }
- }
7 h% l, a9 u6 z( j4 j. k - }
銴鋆賭誨蝣 嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁
) g2 d9 A) e, T# g敺賣詨曉訖ordpress憪賣- add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣 7 x0 E$ K+ |( T# ?* e! }% j" f5 q" ^
4.3 冽
" k) y3 v% n4 ]; x
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
6 F; f' |5 i( m- |7 o9 }" C 暺嚗敶箏閰望霈頛詨交蝡賊嚗
. a m c- c1 T; |. h6 F 頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
# ^4 ?1 |9 z8 S8 z* @$ c
貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
) R" P$ S; F) Z& |1 |9 m$ ?7 [
唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆 |
|