WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)
0 M' D; F9 B3 `. g
, ]3 M& t l2 X- c6 o" B/ Hwordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐
( {( M! q; W! l! w9 Z$ Z隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具
+ q4 O" f' W; Z" {+ m( X) E2 V y+ F9 @, y6 \; R8 O
凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :* j" a4 d: k$ ^; w, ^" b9 E
甇文 , 臭誑閮剖銝訾批嗆蝡賊 :
/ D) |: Z# M% B甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :, l: }) `1 T' i0 k
- [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣 4 |5 U/ _6 o/ |: w$ v
( f" r4 o$ w. \1. 蝪∪桃凋誨蝣
! T# W2 J; t' [' `4 F/ ?祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :! B9 ]4 {. l h G; r/ [& `& x8 L
閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :
5 `; s1 Q7 ^: I& M: b/ ~6 V- 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
- 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
- 閮餃賣賊蝯針ordpressaction銝7 ^* }0 \* M: P) L1 |
3 ?) X2 Z5 {) S: r, u# v/ B3 s6 p2 s祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准
M) t) y7 r6 [' c6 a9 B/ b, U2 i) ^1.1 撱箇餈賣
0 G( \3 v. |# o( A& o/ i1 U$ a嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡8 y1 }% S2 k# m, W& V/ O
- function recent_posts_function() {
2 w& o& b; i/ f3 x6 z! i% k - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));: i- u+ m; s) _6 a' h
- if (have_posts()) :
' Q7 l% U+ X& E7 B! [; @: p. ~ - while (have_posts()) : the_post();
! P# A! a' u: g - $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';" ]: V; _7 u8 p5 J3 I% o; r
- endwhile;
* y2 k& K& r. n6 z - endif;) R" T. k. F5 R' f9 A% C2 ^8 C
- wp_reset_query();
# `' {; z" o, G0 w - return $return_string;% b# X, H' A# [
- }
銴鋆賭誨蝣 憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯
: S3 C, `' {0 W( l- s$ l2 |. Y3 e4 F
/ {. x, v- i: G# q4 F$ Y7 M1 B, X; ]1.2 閮餃凋誨蝣
6 H" q0 S! R. N1 L曉剁閮幃ordpress賣豢臭凋誨蝣潘- function register_shortcodes(){
: u& ?7 l3 o: e% k' x6 R - add_shortcode('recent-posts', 'recent_posts_function');. I0 r+ E& U$ j! K* k- L1 x' O
- }
銴鋆賭誨蝣 嗅冽蝡批捆銝凋蝙函凋誨蝣
# i4 [$ F# e d% z" h9 c* C* f撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴
& B/ V; `4 l5 k/ m( i! K( G- b \& c" ^, D
) B, Y% s6 ]1 c6 \" a1.3 Hook into WordPress
+ k8 u1 u) b* k0 C: M% S- q
* m2 m& l" W. n箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准0 v. B- s# F: ?
- add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣 1 a- N5 D1 |7 a) X- b
1.4 皜祈岫凋誨蝣" x7 K$ r5 `$ G# s" n
蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :2. 脤凋誨蝣 2.1 凋誨蝣澆 凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻 閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼 撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 : - function recent_posts_function($atts){
4 h5 |3 e, y, O7 Y1 r7 { - extract(shortcode_atts(array(
) P- c4 \% R7 y+ ^ - 'posts' => 1,
, o8 }& B, u& o3 b( K- N3 N" X4 m - ), $atts));
) s! c2 u. a" |! `$ _9 S6 ?9 K -
1 n3 a" r1 g0 t/ w - $return_string = '<ul>';
. S/ }$ L6 R+ v* O0 Q1 F M+ c' V# A - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
4 a. S2 H9 ^ O - if (have_posts()) :1 ]; c$ h8 Y, m \/ R+ u3 I
- while (have_posts()) : the_post();7 I: o8 F3 B" Y3 m, M
- $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';/ r; ?/ d) x3 D4 {1 @ L$ J
- endwhile;
8 i8 {6 c, U( D: x& U - endif;1 v- o% H6 B/ S
- $return_string .= '</ul>';
& u- y; p8 x5 l! n3 v9 G - ) Q# w# d1 ^# x2 t1 \" J. q5 Y4 X
- wp_reset_query();
+ s% o! ~+ f4 S* D" O0 h) Y( Y1 U - return $return_string;+ Y' g# E* t# R8 R
- }
銴鋆賭誨蝣 憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :$ @- |1 u R! J* k$ U; ?
2.2 凋誨蝣潔葉憓摰 臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗 - function recent_posts_function($atts, $content = null) {
( ^% F& Z& q/ i& O" j+ a5 a& N" F - extract(shortcode_atts(array(
8 G, c+ d0 f, \- E/ n - 'posts' => 1,+ r4 ?( C& p d
- ), $atts));
) r6 Y# X$ z; E5 F( P -
) E+ O2 m: Q' w, W7 V - $return_string = '<h3>'.$content.'</h3>';! M* y6 p- f2 p6 P- y3 _' A9 b
- $return_string .= '<ul>';
( C+ L1 Z4 R }! D* g& F4 G7 ?! h - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
$ X8 m! b+ |6 H1 L - if (have_posts()) :- ?# K" ]* `& B# K" {
- while (have_posts()) : the_post();
0 k) B6 P' m' I0 Y9 q7 w: E - $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
; |0 `! R* f$ n/ c. k - endwhile;
. Y0 z1 I/ h' l+ K, V - endif;1 s1 r- i1 O$ h9 F
- $return_string .= '</ul>';7 b& c# e3 R" G8 a
- 5 D6 h. E" _6 g0 F
- wp_reset_query();
) v& y6 M( f8 n y - return $return_string;& O" b1 E. k- ?
- }
銴鋆賭誨蝣 蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝- [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣 支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗1 o1 p* x# @0 L- G8 H A$ X7 O3 P% z
3. 其遙雿唳寥*蝷箇凋誨蝣 3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣 閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛嚗典臭誑撠甇斤撘蝣潭曉典撌亙 6 T, ~( d, c2 r1 |) D
臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 : - add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣 曉其閬靽格嫣遙雿嗡唳 , 瑟, 湧甈撠甇蝣箏圈*蝷
# s; C4 Z2 _" q1 O) V璅嚗刻隢銝剖函凋誨蝣潘 - add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣 冽閬銝剖函凋誨蝣潘- add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣 4. TinyMCE蝺刻摩典凋誨蝣潭" R6 f2 T% Y( N2 ~. _
4 K& e" ^# V# u! u嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗 - 箸迨撱箇銝 JS 瑼獢
- 閮餃JS隞; B$ A* E3 Z0 Y$ I6 V
0 n9 W H8 ?" ~+ O( O, x" Y
4.1 JS隞嗡撱箇JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗 - (function() {" b# J/ G8 W' y, B- u
- tinymce.create('tinymce.plugins.recentposts', {- x7 M1 Q U: X4 C
- init : function(ed, url) {: {* E; x1 ?& M6 M8 s
- ed.addButton('recentposts', {* j7 \$ R# p7 j" p& f
- title : 'Recent posts',2 W* r7 r; j8 n, T1 D3 s( m
- image : url+'/recentpostsbutton.png',
- ~: h+ D5 `7 {0 T% M- e - onclick : function() {3 S# m* l3 t8 D( n3 D
- var posts = prompt("蝡賊", "1");) n+ x9 ~$ F9 B* p9 O5 I0 G4 g
- var text = prompt("隢憛怠亙銵冽憿", "唳蝡");0 \ h2 P( @; B1 P9 r3 U3 ?
-
! F& j* q, a0 M8 M0 R8 F - if (text != null && text != ''){+ Z# _& c$ R7 m/ p3 e5 d* n
- if (posts != null && posts != '')9 v+ [% ^/ f. u; g" b& t" Q3 z
- ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');5 F! I% H% A% W5 @6 A# {
- else3 ]% i& k4 F1 W
- ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');$ v1 D& }, l, B0 ^/ i
- }9 f1 F* Z2 \8 s. L! A& s3 T
- else{
' z2 ~/ g0 O! _ - if (posts != null && posts != '')
2 b _. X* P' K9 E6 T6 A* n; H6 { - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');
8 y8 K# |" T2 V* B7 X3 e - else
9 u3 {/ S8 x' Z5 B - ed.execCommand('mceInsertContent', false, '[recent-posts]');
+ ~2 D* T4 g# A - }
5 i% x+ |- V. v0 L7 A - }6 `( c _ U+ k
- });
/ b4 Z! k& ?$ ]! R# v/ N - },
) `8 M7 y" B$ N# z5 I y/ @9 \ - createControl : function(n, cm) {
0 h {3 p. t: ^0 G9 c( A - return null;3 b; F4 Z& |# h/ \6 C
- },, S K6 o" V/ M/ Z5 _
- getInfo : function() {
D: ~3 I B( l% C8 L c - return {9 y1 G g/ Y6 K
- longname : "Recent Posts",
- C: ^- D9 G8 l - author : 'Specs',
6 j+ }# X& }' T1 _. g - authorurl : 'http://9iphp.com',
$ F# l! @6 D2 u7 x - infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',* i/ q# b P8 n1 p
- version : "1.0"4 F' \( c5 b, L' Q. j
- };
& e& u( R2 r8 T3 c/ z+ }/ S9 \ - }
4 ?' }6 l/ J8 O, ^8 O9 W; ^: t - });) K& A E7 p! N. b `/ f. m
- tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);
9 q- `$ ~$ A$ D/ Q9 l9 H2 G - })();
銴鋆賭誨蝣 澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨 onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉 敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准
# l4 s7 `& [5 G. D s
4.2 閮餃TinyMCE隞
; f5 k; D: I: G8 X
% g8 c( [# w( h4 Q y z 嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉 蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩 - function register_button( $buttons ) {
* {) y. {5 Y- I- D% t8 I! f, O - array_push( $buttons, "|", "recentposts" );* b. @; F2 c0 I3 q4 c% e
- return $buttons;& T( B V2 s/ q5 L8 X# S/ `9 O2 T' H
- }
銴鋆賭誨蝣 蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅
, S4 _$ \( M3 |' Y& t6 n- function add_plugin( $plugin_array ) {: T+ z* L' { V: h
- $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';3 {3 b$ A7 K+ r9 F! `6 X G: F |4 |
- return $plugin_array;
5 x3 L; N5 s ^0 P - }
銴鋆賭誨蝣 銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁/ E* c! g, ~9 c; a' g
- function my_recent_posts_button() { 1 Z1 u1 B! ^- s6 q1 ^ \
- if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {& R: f" L9 r) c+ C
- return;
7 U) P! Q! f/ N - }
6 W$ `( D1 d$ t7 g( M0 p - 0 W; [6 t# h8 Q$ e0 O
- if ( get_user_option('rich_editing') == 'true' ) {, M5 V: @3 k- Y6 h7 ^% h
- add_filter( 'mce_external_plugins', 'add_plugin' );
' K. {7 i! J- b: m$ b# w - add_filter( 'mce_buttons', 'register_button' );
! W9 N5 I! L4 ^ j( I - } & i" Q$ a$ ? r' }# |
- }
銴鋆賭誨蝣 嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁3 w0 [/ p0 {+ o3 B/ |* L1 e
敺賣詨曉訖ordpress憪賣- add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣 7 h) _% Z& l+ Z4 |
4.3 冽
K- b6 w& x) d- l& u! j6 y
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
% }6 P! m" p* ]. W# q4 c7 ? 暺嚗敶箏閰望霈頛詨交蝡賊嚗
% Y1 }' b; H; E1 `! l: w
頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
1 K) J. W' ^2 F& B, o
貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
& m; ]' C$ ?2 k0 F4 |3 o/ f
唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆 |
|