WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button): F/ g: f" x' B& L) o5 M& y
. T0 K: Q- Q" p/ }
wordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐
2 c' B0 j9 s% B8 W }# @隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具$ ~/ G8 T6 a$ C/ W/ c
i4 e9 z: K. M3 [ s
凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :0 L }" D; ?0 r+ e6 C
甇文 , 臭誑閮剖銝訾批嗆蝡賊 :
/ o4 j4 l& c( L; y5 U2 B甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :8 c& V6 ^: `, C/ P! x4 w
- [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣 7 e1 v4 w. ]1 j/ n1 A ?& i% M
1 T, L9 ?; @0 R- N, m' t5 t
1. 蝪∪桃凋誨蝣6 o2 y7 ^- K9 Q
祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :8 x( A0 {$ I4 |( }* G0 ]' N( c2 N
閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :' j9 A' v$ ^3 B7 ?. Y
- 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
- 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
- 閮餃賣賊蝯針ordpressaction銝
+ B- I4 V/ q/ x& d ' I0 J9 w( d4 Q$ \
祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准, }" G0 r% |) W: l: \5 ~+ e
1.1 撱箇餈賣" H& Y J7 i* m' g. n! S) d! z5 ^
嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡& K2 q3 R7 I. V) l* z
- function recent_posts_function() {
3 T2 }. m5 M! S% m - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));
( r# I0 }) R! r" Z# V, V y - if (have_posts()) :, c% p# M! b: [. Y
- while (have_posts()) : the_post();
# d% {' ^3 j: T0 @' J - $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';
8 Z! V+ \" `' w2 a; A. k+ `6 f' ] - endwhile;# e9 c, J+ H ?; K! S8 ^
- endif;( G2 `* j% p% X3 \& ^
- wp_reset_query();
) k+ F( S) i) J* h% X* k9 e, { - return $return_string;
/ c+ b* y8 b, u7 `7 _ - }
銴鋆賭誨蝣 憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯
( t; g" K% z% }8 w* M
; Q8 J6 |7 s8 \1 `- e9 C A' P
( w0 y8 p8 v/ B1.2 閮餃凋誨蝣
( P, S" O w4 c/ U* w曉剁閮幃ordpress賣豢臭凋誨蝣潘- function register_shortcodes(){
" U' H& }9 u, f4 m- y) ? - add_shortcode('recent-posts', 'recent_posts_function');! ]- f/ J. Q3 d
- }
銴鋆賭誨蝣 嗅冽蝡批捆銝凋蝙函凋誨蝣" N7 E6 g+ j$ Z, a- ]" Y5 X
撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴4 P# p& s- N& @+ o. I
0 s1 ]. H- [3 ]' N* W3 ?0 b
7 V( ^; P x8 P( V
1.3 Hook into WordPress+ B$ x( r. a9 M$ R
9 Q; c; [+ ~7 r' K1 p: l& V) K! G箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准
3 l; b! t" o3 h+ {- add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣 5 m8 l: A+ m( _1 q/ [
1.4 皜祈岫凋誨蝣
4 w7 T" a& b8 m. h/ [0 L蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :2. 脤凋誨蝣 2.1 凋誨蝣澆 凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻 閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼 撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 : - function recent_posts_function($atts){: j* {+ F/ S# S$ z) H/ o+ o
- extract(shortcode_atts(array(
# t$ D& s& i0 w/ R- t9 d - 'posts' => 1,# S6 u% F; R0 G t) W4 Y
- ), $atts));
( k" x& ?' ?. V5 S -
9 A; N; p1 t# Q: @4 `; c$ m u1 c - $return_string = '<ul>';
. `: V/ j( W( ?/ W8 F" Q- [* G - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));5 M# R2 O! X/ K8 \
- if (have_posts()) :" V U- Y- i- P8 k
- while (have_posts()) : the_post();
8 }1 Q5 K) t1 n' m" p - $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
( k6 N2 J! t9 G1 v - endwhile;6 v) F% w6 X; c- r4 P% r
- endif;
. O1 `5 e Q: U7 O9 } - $return_string .= '</ul>';
# m7 z/ C1 y* p" s: P -
6 l; N2 ^/ j" e' W' y - wp_reset_query();
% S. M4 _7 A3 i/ {2 N% R$ l% | - return $return_string;
! r$ b1 V/ F+ Q j" @* F - }
銴鋆賭誨蝣 憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :2 I9 C6 B, P& K/ c4 w* F2 w' p" ]
2.2 凋誨蝣潔葉憓摰 臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗 - function recent_posts_function($atts, $content = null) {
) E* f7 m6 K5 g) ? - extract(shortcode_atts(array(0 j6 H- O. P1 E5 Y; I
- 'posts' => 1,
9 s8 |! r/ [. c0 M3 P5 V, ` - ), $atts));
7 P* V! @% H5 B8 M5 S5 H -
% x! ~; v: H" u8 \% `8 l; g - $return_string = '<h3>'.$content.'</h3>';6 ~# ]. m! V0 W6 t
- $return_string .= '<ul>';. `* P$ ~5 q# J
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));) n5 z; T% Q0 [5 M" b) r" X
- if (have_posts()) :
& i2 Y% e( T6 @% i% R% H7 U - while (have_posts()) : the_post();
2 _; u7 C1 P% y, z! \$ ] - $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';' K! K' `* A: D- q' R
- endwhile;
$ Q% f8 l+ ^$ {6 N3 C - endif;& _' N V) Z) m6 ]" x- |
- $return_string .= '</ul>';6 ?8 m: t0 b7 G, F# q' D
- ; d- M1 y2 ?/ O4 p# G& X
- wp_reset_query();
9 Z0 w2 W) \5 s2 S: } - return $return_string;! J0 U& u# w! w$ ~& M: R: r
- }
銴鋆賭誨蝣 蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝- [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣 支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗% I/ ~/ i+ @7 x( ?& }' \) B! R8 V
3. 其遙雿唳寥*蝷箇凋誨蝣 3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣 閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛嚗典臭誑撠甇斤撘蝣潭曉典撌亙
4 m* C W9 f1 G臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 : - add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣 曉其閬靽格嫣遙雿嗡唳 , 瑟, 湧甈撠甇蝣箏圈*蝷 5 w) M/ m9 f% C8 r
璅嚗刻隢銝剖函凋誨蝣潘 - add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣 冽閬銝剖函凋誨蝣潘- add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣 4. TinyMCE蝺刻摩典凋誨蝣潭1 U, I2 }) p0 D, p$ E
3 r3 V5 L3 }- v; l& z嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗 - 箸迨撱箇銝 JS 瑼獢
- 閮餃JS隞8 @+ x$ e- I( a4 f2 c
' n2 E3 F6 F+ _( M
4.1 JS隞嗡撱箇JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗 - (function() {- U$ T; [* ?1 i
- tinymce.create('tinymce.plugins.recentposts', {
1 P# z8 F( A1 S* \8 m - init : function(ed, url) {& s+ _) Z* n4 V' C y5 J) W2 ~0 `% o9 w6 U
- ed.addButton('recentposts', {
- t. C3 J9 \( x0 w+ W: J/ D# z9 ] - title : 'Recent posts',' @6 k, H; _$ {
- image : url+'/recentpostsbutton.png',
" x' ?, L; Y& o - onclick : function() {+ [, v# s- }) ^0 ^& z u8 H
- var posts = prompt("蝡賊", "1");
4 L/ G0 K; g; k7 ^! Q) W - var text = prompt("隢憛怠亙銵冽憿", "唳蝡");
5 t! V4 y/ M/ e* O ] - ) B U- F; p( [
- if (text != null && text != ''){3 l. t' C: C: s7 V9 _$ `
- if (posts != null && posts != '')
. s* S/ M' b8 g1 c$ _7 K/ z - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');, D9 B; _, _- B5 d) q* Y# s# b7 X+ H8 k
- else
3 _/ {! Q+ V& | - ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');
# V4 y; m, n6 E0 c v$ t( o3 u" M - }( r6 u% D+ t$ j5 q: g& C
- else{7 P8 Z& j% x9 N' F/ }5 M
- if (posts != null && posts != '')2 f! C. |" f, j3 {/ q9 D9 d% @8 t
- ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');
4 N* d/ U$ E) L4 K - else: q+ {5 B5 f, U* S- u5 Q3 ?7 \7 Y0 n
- ed.execCommand('mceInsertContent', false, '[recent-posts]');$ F; R; Q% ~0 Z' P0 F0 {
- }
% w( K `( {; F2 u* v! i - }$ z$ b8 L' @. q% l( u! _0 T' F
- });& k g; H, x8 l9 |
- },
( e5 P M% Y! M& } C1 G - createControl : function(n, cm) {: C3 a9 a) o0 y3 V
- return null;8 U2 X. R# ]0 U& q( s% G6 H
- },
: ?% H) c3 o0 m- J0 m8 S. V8 w - getInfo : function() {% v. Z( B, X, a% `" `& f
- return {: M! ^: b0 r1 G4 s' l; v# K
- longname : "Recent Posts"," h) U# o, B" [3 e7 {1 `% K+ V
- author : 'Specs',4 F4 \( M9 F0 U; y k) X; B
- authorurl : 'http://9iphp.com',# m4 }7 N( v" k: l9 M
- infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',. \6 X+ ?3 |. i
- version : "1.0"" l0 Z! Z, B. z) G
- };
2 t m7 G( x; _& V" V - }; P. @3 y! }! t8 n2 f0 D
- });
0 H Z0 V' o: G. P5 F$ K - tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);6 m) g. @ v( c+ a4 z3 B
- })();
銴鋆賭誨蝣 澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨 onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉 敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准 % K: _* a; ?8 s3 F$ ~( Q3 T
4.2 閮餃TinyMCE隞7 d) r2 z, O' ^0 i/ k4 Y
: |+ n/ ?3 z. N& e4 d* V1 f
嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉 蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩 - function register_button( $buttons ) {
+ O& S. C3 c, t# w' S" a - array_push( $buttons, "|", "recentposts" );" u, I; m. c) f, ~7 i7 \
- return $buttons;; p4 C' D: e! }$ U6 N
- }
銴鋆賭誨蝣 蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅* x! W1 e3 M5 m! y2 v) x
- function add_plugin( $plugin_array ) {
~2 B/ D9 I/ ^. I - $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';! `" ~5 m0 i6 |6 n8 z. ^8 D- ^
- return $plugin_array;0 E5 W/ h5 W$ R! y: n5 p
- }
銴鋆賭誨蝣 銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁
@* s7 x' K3 F3 {$ J) T$ ]- function my_recent_posts_button() { 4 j7 B. O* f' E7 \) r) Y$ x2 `8 z
- if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {: e! ]( @# g+ c/ C! a
- return;
/ h/ Y' e- [4 `0 P. P - }
5 z. H# c3 a6 k9 L5 g2 d -
$ h% U5 c/ g4 U6 h - if ( get_user_option('rich_editing') == 'true' ) {; `6 ~# s8 ]+ l, Z! R
- add_filter( 'mce_external_plugins', 'add_plugin' );
& e1 l% L+ k6 B4 | - add_filter( 'mce_buttons', 'register_button' );
( u/ f! E7 {$ n2 b - } ( l8 E" |$ X4 ]2 Q: r/ M
- }
銴鋆賭誨蝣 嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁
* ~! J @1 ^7 {* C+ Q敺賣詨曉訖ordpress憪賣- add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣 7 _# Z2 W/ q) p! I
4.3 冽
1 {& k. g, D1 b- Z" h
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
5 c7 R- f- S5 G( f+ I% r
暺嚗敶箏閰望霈頛詨交蝡賊嚗
5 q$ X8 C9 U- a3 b- E, D/ V; T# u) C 頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
2 r$ X' z1 v6 T& U/ ?
貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
* i9 p( ]# X o& V' ]
唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆 |
|