WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)3 |3 ]& Y. c$ u# C+ c" `& V! U
u4 l# k7 c# `
wordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐
, p7 N1 N# T% |( I隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具0 B1 C0 I6 C$ D! ~7 ]
5 T' g% X2 F3 _: Q3 @
凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :
M: @6 H ]2 _: g+ G4 [0 P7 Z% L甇文 , 臭誑閮剖銝訾批嗆蝡賊 :, S- {6 | I2 c5 o* Y# x/ o% o
甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :
6 E$ I' X( [0 f- [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣
! R) X4 u# f- ^) D/ I# f. R \4 e: x0 I5 q
1. 蝪∪桃凋誨蝣; t& n, ~+ D. }0 M
祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :
8 J% w; n) [! {) O8 I5 r! j3 z5 l閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :
4 r- M1 ]; v, R1 z2 p- 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
- 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
- 閮餃賣賊蝯針ordpressaction銝3 p0 ^; P, `, \
+ a* Z0 ^9 N8 q' X0 r+ b
祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准5 _& R+ U; F* ?1 H3 ]& ? ^3 D; v
1.1 撱箇餈賣
* i2 y. R% [ I# a' b5 k- @嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡
% K6 P& d' ^0 ~0 n# o0 I$ N' L- function recent_posts_function() {2 a6 u9 J4 ~# M$ [. q! Y( U
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));
% P3 j) }3 ?. L" ^! u- f5 q - if (have_posts()) :
: P$ W) N. T. |! x/ C/ Q# B - while (have_posts()) : the_post();3 a0 d, B# R X G* ?: z: _" M
- $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';5 E6 g* Y( W0 J" ^ j
- endwhile;
. ?, P6 @9 g2 E: u1 x& ~ - endif;
) ` s5 K8 @ K9 y8 a' V6 t - wp_reset_query();' H* R2 f8 j1 v z) e0 p' E" Y6 A( G
- return $return_string;
3 L. c. L# P$ r2 V3 [ - }
銴鋆賭誨蝣 憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯) Y0 e$ \6 d+ ~# t* ?7 Y5 e
& L, D2 Y9 I% b, B4 O" d# r7 r7 n' e; b
1.2 閮餃凋誨蝣
1 _. v* L0 R {9 X7 L" N6 ]7 C2 o; F$ p曉剁閮幃ordpress賣豢臭凋誨蝣潘- function register_shortcodes(){
) X+ s4 Y% z$ A; X" F+ p - add_shortcode('recent-posts', 'recent_posts_function');$ Y o9 P, `) e
- }
銴鋆賭誨蝣 嗅冽蝡批捆銝凋蝙函凋誨蝣
& G0 x6 o! h: Q m# K撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴# V. _5 B" k: f; H
7 j* E9 `1 W: Z a; O$ L" }/ ?, h$ M8 Y; [; U$ [/ t$ j
1.3 Hook into WordPress7 V0 p- D# ^+ t2 @
/ o" `5 d' v0 \& ?& ]+ G箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准, o5 p; i$ W+ v) T& h2 w9 ~
- add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣 9 _- x# ^1 U; x, U$ {
1.4 皜祈岫凋誨蝣) X! a+ P) K' k3 Y
蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :2. 脤凋誨蝣 2.1 凋誨蝣澆 凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻 閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼 撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 : - function recent_posts_function($atts){
* V( L. L+ J8 z' m0 r: s - extract(shortcode_atts(array(
# l( H& ]. L4 u% I; d& w - 'posts' => 1,4 q) v* r3 [, y/ K2 b& ]: K) V6 J
- ), $atts));
) t* s) L- H- ] - " _0 A" Y% G" T( a
- $return_string = '<ul>';1 S# |/ |" y3 X* K1 n0 a/ X
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
" t+ h0 ~3 B8 _6 b2 R2 u - if (have_posts()) :6 I+ g& t6 _1 c A; P- N
- while (have_posts()) : the_post();: Q* Y% M5 a) u8 u3 _+ G
- $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';" A/ G/ K$ T5 ~9 u) V& b* [
- endwhile;; G3 s) @9 c0 Q6 u5 ?; y( X
- endif;
4 v+ g0 D2 g0 G$ g5 C - $return_string .= '</ul>';8 B+ ~/ ?% c4 O3 |. @
-
5 T3 A" k; N& W. b - wp_reset_query();
, u( M: q9 U& ^( U8 e- R - return $return_string;1 O, d9 E6 Z; u2 f+ v: K
- }
銴鋆賭誨蝣 憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 : @1 f) l! b- A8 j3 Q1 o' N3 C
2.2 凋誨蝣潔葉憓摰 臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗 - function recent_posts_function($atts, $content = null) {
! x* n9 l( i; a. r - extract(shortcode_atts(array(9 J- Y5 n6 p# y. i4 i V
- 'posts' => 1,! [! P8 u) U/ E; M0 v- |% H+ L( ~
- ), $atts));
. K- ~% D j% M$ u- G - # T5 G ~# y: e5 {: p
- $return_string = '<h3>'.$content.'</h3>';# k9 o7 K7 R. L: ?
- $return_string .= '<ul>';
8 U+ E/ v+ M; G( \; |8 {1 V - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
0 h: y6 f' C& v - if (have_posts()) :, ` Q Z* N) q0 z B0 a
- while (have_posts()) : the_post();; x% U! I; {2 A& X/ y9 C
- $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
! }. L% k* s. u! O3 U - endwhile;
) ^' ^3 r) G4 k3 _7 |5 W& ]9 S" d - endif;
4 r( c6 }' [" T& G4 Q" M) A: @/ `3 e - $return_string .= '</ul>';
1 v+ E5 O5 O4 a p+ M - ( F L2 ?# m% }/ A1 ]
- wp_reset_query();
# \, u. Y3 Q' E9 j - return $return_string;
/ [, r! H& ~3 t$ g3 u' T - }
銴鋆賭誨蝣 蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝- [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣 支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗
" _& a8 P) E( |3. 其遙雿唳寥*蝷箇凋誨蝣 3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣 閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛嚗典臭誑撠甇斤撘蝣潭曉典撌亙
6 k ]: K# Z6 t" C* C: B臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 : - add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣 曉其閬靽格嫣遙雿嗡唳 , 瑟, 湧甈撠甇蝣箏圈*蝷
: m' O4 | i' \; t' L: ^$ h璅嚗刻隢銝剖函凋誨蝣潘 - add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣 冽閬銝剖函凋誨蝣潘- add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣 4. TinyMCE蝺刻摩典凋誨蝣潭! y; o3 x: P( R. B0 w6 Q
& b8 t" N/ a( Y! C. d0 v" A
嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗 - 箸迨撱箇銝 JS 瑼獢
- 閮餃JS隞$ |% M, |( g1 |
" ^4 Z; Q- I+ p1 S' ]/ f4.1 JS隞嗡撱箇JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗 - (function() {! n' w, A' y9 B9 C- O9 r2 y
- tinymce.create('tinymce.plugins.recentposts', {6 x2 C2 V* M: G* X" z# C
- init : function(ed, url) {3 P8 z- d8 s+ o8 d- G
- ed.addButton('recentposts', {+ x: D7 d% a* P' }0 z: z/ L( E+ Q: x
- title : 'Recent posts',; ^0 I+ s$ M0 e2 n7 d+ C
- image : url+'/recentpostsbutton.png',3 c. n- C5 ]# i7 |# ~& J1 l H0 j& z
- onclick : function() {
# M+ j5 V0 R& r - var posts = prompt("蝡賊", "1");
( j8 R$ v( l" ^ - var text = prompt("隢憛怠亙銵冽憿", "唳蝡");: \2 N9 d& G$ w/ p$ {4 d
- 9 H3 q+ ^$ Q( _! U3 Y/ N L8 p; z
- if (text != null && text != ''){3 A, \& p9 }; G9 ?: T A; p
- if (posts != null && posts != '')
Q" r0 S$ m9 P. Q2 J" w - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');; u2 }* ?$ q4 ^6 i9 |+ ?1 w
- else& b5 u) v4 ^7 t* _9 T9 F
- ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');( q7 X$ F3 ]6 R% y3 G
- }/ u2 Y8 O \, a6 `$ ?/ r" `
- else{
* d# z9 n( H. g9 g9 |) o: p9 S8 R - if (posts != null && posts != '')) y8 v; r4 b2 n, N, H" X
- ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');" k5 X* K1 D( S$ S3 L# V
- else
8 @8 k8 }" h* U# }# x- R - ed.execCommand('mceInsertContent', false, '[recent-posts]');
- C. S4 M9 n$ C7 n: C' k - }
+ N$ k7 }) Z {: J* { ^2 M' q - }
7 T8 F9 `: s6 [8 x- ?7 i, l& u+ u - });
1 D" l) N" U/ `3 l6 G+ y - },( S* w5 X" H% D% Y
- createControl : function(n, cm) {
. j! B( F8 A2 o- m - return null;& x, z8 a c; n/ \
- },
1 x$ S! G7 t: ~8 ~/ [9 M7 F - getInfo : function() {2 P! O5 l. A+ M! A5 O, q
- return { t6 ]3 b# z6 f2 O% m3 J
- longname : "Recent Posts",, W$ u3 L+ p9 o1 U
- author : 'Specs',
% D9 A u* m2 e - authorurl : 'http://9iphp.com',/ Q. X7 Z; _. _$ P
- infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',' {+ W6 B V0 S0 m4 r- |
- version : "1.0"; L5 n) P# d$ D8 ]5 p% l
- };
' W! P: T3 J3 [. N - }) }8 v9 ]; m$ D5 t% l
- });, r5 U) S) ?8 d, U. m
- tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);
: g. m/ {1 k" t9 b - })();
銴鋆賭誨蝣 澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨 onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉 敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准 8 @( _! o1 s$ A# r3 B, w
4.2 閮餃TinyMCE隞* D+ i3 A4 l# |2 j
4 _8 e, Q3 `8 h; Y 嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉 蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩 - function register_button( $buttons ) {
$ c0 N8 a$ E R3 K" q# A - array_push( $buttons, "|", "recentposts" );
+ l' @ A( C! F8 Y* a4 c& K - return $buttons;6 a' ^4 W$ D5 E- S R! n
- }
銴鋆賭誨蝣 蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅
# O$ Q8 W, x# H2 V* w- function add_plugin( $plugin_array ) {
# A( ]# z9 Z G. L' e) I S - $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';( s7 N" y2 @0 q4 }' n
- return $plugin_array;5 v4 ~' f1 d8 i8 V8 |3 S
- }
銴鋆賭誨蝣 銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁# m4 J/ a- m' w/ }2 F
- function my_recent_posts_button() {
. u6 J0 w5 P% T' f. z - if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {
/ o1 F; Z }0 @* r4 u - return;
# s' D$ `/ f A - }
3 F# r5 v& h& g; P% t8 t% } -
: d8 v3 h/ b, g* p) n, M5 [ - if ( get_user_option('rich_editing') == 'true' ) {/ T9 _4 z. s v2 ]8 p
- add_filter( 'mce_external_plugins', 'add_plugin' );
) Y8 t4 e# U' d% c - add_filter( 'mce_buttons', 'register_button' );- ~$ Y: o! E4 @
- }
- U+ \5 p' M) C2 }1 t+ e& e" D - }
銴鋆賭誨蝣 嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁3 t: Z- X- Q" V4 I1 D6 ^
敺賣詨曉訖ordpress憪賣- add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣
% F- a* `* E3 b2 R* b4.3 冽
# N7 H2 ^0 }2 L4 J; t, ~
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
& G- C! u( a( D; o
暺嚗敶箏閰望霈頛詨交蝡賊嚗
+ N$ K$ F) l: B) G* o1 t 頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
& v1 J0 |% {& P: u9 T
貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
& N9 @! g& w5 C6 Z q& p
唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆 |
|