WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)
: y+ x y% g {& X3 r/ y. y
( I1 g% F) M8 }& Ewordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐/ o6 w; v% j8 K) j
隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具
7 u6 c" T0 E& Z ]) C, X. V
; `; W0 J% k2 p凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :
: h' d8 f' b5 i甇文 , 臭誑閮剖銝訾批嗆蝡賊 :1 z" i* Y, i- H8 T; l2 R2 Y
甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :/ `/ i8 E4 a' ~" u3 w
- [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣 8 r: n0 g& n7 }8 |4 \
- i" w& U0 e l! H, p% W
1. 蝪∪桃凋誨蝣
3 f! K# a; P V* [# W6 C祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :
$ K) Y) n% ?& a* I; }4 g閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :. W) m5 u! H7 S
- 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
- 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
- 閮餃賣賊蝯針ordpressaction銝
* k$ {3 s9 w0 i, G) [ 9 G [+ I$ I5 y
祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准$ l/ M! L8 l4 |2 M
1.1 撱箇餈賣
+ ?+ K4 k6 c" S$ T' U7 J A嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡
8 z: J" {4 X, a% m7 p: Q) v- function recent_posts_function() {+ [' `/ i& A* j/ q9 P; e2 r
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));' z# M5 m8 c1 L. r/ J
- if (have_posts()) :
0 ~, ]0 [4 W4 A, @ B1 u - while (have_posts()) : the_post();
- u7 Z$ N- H6 m X, @/ @! [8 l - $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';6 y3 K2 T8 ^4 z% c* R8 f
- endwhile;4 T- K% [. a' @2 {$ w: h
- endif;5 l9 w( h& n- R' g7 r, [8 E% j
- wp_reset_query();
0 C5 [9 ]; E2 q% P - return $return_string;; m8 Z/ s) e* x1 }" N
- }
銴鋆賭誨蝣 憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯
( g, F/ V( K( h4 D0 f0 W
$ M+ D$ w. C) E* @$ X! m
* s& x; P: k$ d/ J) J- q+ c1.2 閮餃凋誨蝣
6 Z7 P8 J1 a* J1 O) F9 w( w曉剁閮幃ordpress賣豢臭凋誨蝣潘- function register_shortcodes(){+ F/ G! `6 r- R% K% l
- add_shortcode('recent-posts', 'recent_posts_function');# b6 Z: T4 z2 X- } s. W
- }
銴鋆賭誨蝣 嗅冽蝡批捆銝凋蝙函凋誨蝣5 s8 g/ e: ~7 g
撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴
( y: O4 T4 Z" m
- H( q! x% x8 b; |1 ]: c- p5 i- k
" ?9 y3 w7 ]7 i+ C4 M- w* ]1.3 Hook into WordPress. T( N' o. J" R+ S ^
' F3 a6 K2 y* S1 v" n! A S箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准7 G" P3 Z/ ~: f$ q; _3 }4 u0 O
- add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣 , L& C% Z; s9 |
1.4 皜祈岫凋誨蝣
5 ~4 S% G0 P1 K蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :2. 脤凋誨蝣 2.1 凋誨蝣澆 凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻 閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼 撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 : - function recent_posts_function($atts){
& }$ }7 b* k, ?& x& @ - extract(shortcode_atts(array(
& F: M, j4 v, ^6 P/ r - 'posts' => 1,0 i8 k$ U2 m* v% c8 o
- ), $atts));1 @1 G* @5 ~" r' \$ d
-
9 g2 F% k& I/ L- P" Z! n; B - $return_string = '<ul>';+ A8 g6 C3 K% r; P
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));' M0 q2 f' |" L7 b( ^* @1 `# W
- if (have_posts()) :
5 Q4 ^1 n8 {$ q7 x" L - while (have_posts()) : the_post();" i7 z1 ~+ N& [. U
- $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';) U' t' W! g2 P+ w4 }
- endwhile;7 ^7 Q# m5 n, ~: a g4 o; R4 L
- endif;
3 A4 y$ j( C, u - $return_string .= '</ul>';
8 `2 w7 P& a0 j/ H* z# ^ -
9 Q- H2 \) S0 p D - wp_reset_query();
! e. ?% h; T3 \- g" z. { - return $return_string;: V+ ? O& _$ C5 e) I2 Y
- }
銴鋆賭誨蝣 憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :
$ Y+ Y! }6 a; J2.2 凋誨蝣潔葉憓摰
臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗 - function recent_posts_function($atts, $content = null) {3 v' {$ |& j& m; i, o; d, U4 T2 [
- extract(shortcode_atts(array(
# M t ~$ T& J: ?9 l p6 w - 'posts' => 1,
! k7 ]7 ^1 i4 S* P& E - ), $atts));4 h+ {: R- ?3 y5 c* ?# {6 ^9 o
- , H# w1 N& B3 n
- $return_string = '<h3>'.$content.'</h3>';
) @) o' t1 _! H& e/ x - $return_string .= '<ul>'; V4 u- p% c; A% _* B) s7 w
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
s+ j# C) ~8 D$ L - if (have_posts()) :( b. S, `4 s. g
- while (have_posts()) : the_post();
- U- F: {, e2 u1 C2 i3 g0 S3 p* B Y - $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';$ q+ f x# l- b; `1 t# }( C# V
- endwhile;/ O9 m* H1 z: Z( K2 I! t; `, k
- endif;
, ?* V$ F; z9 k" y7 q) J' @0 @: I" y6 b - $return_string .= '</ul>';
9 t" D# S7 w1 U! N4 R - # Z9 u0 Q% D+ b8 N9 t3 e
- wp_reset_query();
9 E% y) K! ~1 V0 u - return $return_string;1 Y: N: o2 V& q7 s& H
- }
銴鋆賭誨蝣 蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝- [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣 支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗
J2 w1 K7 x+ h/ P3. 其遙雿唳寥*蝷箇凋誨蝣 3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣 閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛嚗典臭誑撠甇斤撘蝣潭曉典撌亙 / b; D7 P7 {+ e7 r
臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 : - add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣 曉其閬靽格嫣遙雿嗡唳 , 瑟, 湧甈撠甇蝣箏圈*蝷 / X% E9 X: X" S s/ ^/ d# i G/ e
璅嚗刻隢銝剖函凋誨蝣潘 - add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣 冽閬銝剖函凋誨蝣潘- add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣 4. TinyMCE蝺刻摩典凋誨蝣潭: C/ D# }' Q- r- C! A, [9 o
4 A2 n: u4 J' C. C; K嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗 - 箸迨撱箇銝 JS 瑼獢
- 閮餃JS隞& W f d# p0 ]
- b0 ]1 E: s% L& x( w
4.1 JS隞嗡撱箇JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗 - (function() {
: G2 A; l3 O3 L1 Q' S - tinymce.create('tinymce.plugins.recentposts', {) l7 U9 y4 v) |, ^
- init : function(ed, url) {
1 x4 C+ Y% `6 V3 r& g( H& q - ed.addButton('recentposts', {, T2 {) [& f/ F. o8 }* B
- title : 'Recent posts',: V& q- S( x* @; K2 v
- image : url+'/recentpostsbutton.png',
7 M% F4 |9 r. {: e8 e, { - onclick : function() {8 c9 T! @5 `9 A @
- var posts = prompt("蝡賊", "1");
^! m9 O: N6 f6 `. c& t - var text = prompt("隢憛怠亙銵冽憿", "唳蝡");( O8 U: S5 D/ z+ o
-
% B3 ~/ s0 y/ G4 D) w3 _5 \- J4 q - if (text != null && text != ''){7 i. @! Z) l/ q2 I7 l, \
- if (posts != null && posts != '')( @( ~; t1 k5 u! ?
- ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');
8 ~# x. ~9 u4 F" q+ g6 m& Q: r - else
5 L+ ]- a6 D6 c7 U a" a3 ] - ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');7 P, i; H/ `! d) d2 o" ]* Y
- }+ p8 J! ^9 A" Z, i1 d
- else{
6 F5 a5 r* i: N/ a5 ^! l9 J - if (posts != null && posts != '')1 Y0 F' o# N. Q! z) c R
- ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');/ O* u* C* S# Y* s9 p6 @
- else- q5 E+ \* X* j# x: c' \
- ed.execCommand('mceInsertContent', false, '[recent-posts]');0 u# a* \( d. I1 B4 q8 X2 a; [
- }+ C: |' J) }7 i0 I4 l% b
- }5 \/ C2 r! y+ `6 t* c
- });
+ a; |1 W6 @) v9 O- f# [0 J - },
+ K9 D" e" T$ V3 d% C9 f6 M - createControl : function(n, cm) {6 d3 y: K9 W/ m: q, Z
- return null;
4 T) U: _/ {& k - },% d/ J/ T$ Q: g+ s5 p& F1 j- C
- getInfo : function() {
8 t1 Z; v% _9 k0 M5 ^7 o - return {
" \3 B( N- o" V: {& Z - longname : "Recent Posts",, B+ X7 L, f, p
- author : 'Specs',! w: z J0 [4 C8 U( d- k# r
- authorurl : 'http://9iphp.com',6 g" L G' d' a3 |7 C; q B
- infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',
2 T1 L* Q+ V6 @- S4 P - version : "1.0"$ E* F D' f( m8 f6 ?
- };
. u2 }1 `6 @- j: G! d& C% U- B - }
% @) k5 k% ]! {# C8 ^! d8 U - });0 X+ H3 E5 H1 T9 r6 ]' H. e1 O7 z
- tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);' o2 q8 g) G0 p& Y( M4 t
- })();
銴鋆賭誨蝣 澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨 onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉 敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准 ) O/ V( P# W+ p6 V$ P8 M
4.2 閮餃TinyMCE隞5 X! v* r3 l4 E7 p% b
, j+ f0 k1 [* [* l6 Q+ X
嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉 蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩 - function register_button( $buttons ) {
+ I: |8 {4 h" _3 `0 \1 F - array_push( $buttons, "|", "recentposts" );. k% @0 Q# V- o( J. H- r: X8 T" P& f
- return $buttons;
# A% a. r' N& p+ w7 j - }
銴鋆賭誨蝣 蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅
9 z" g5 k' K2 @& z7 i- function add_plugin( $plugin_array ) {1 r H6 E& R$ G, v$ o
- $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';
* t7 R5 M( w& T& e8 o - return $plugin_array;) t- J3 O! s9 f4 H
- }
銴鋆賭誨蝣 銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁
) T& y, a) x9 n, ]- function my_recent_posts_button() { 1 U2 ]/ y; ]7 {4 ]- X5 k! M- V
- if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {: C2 C: c; i2 v/ ~ K+ b
- return;
8 e% l/ b+ _4 l3 }# D. @0 X7 V - }
* ~* E" I* I8 {# T - $ v( C; t/ T8 l0 M3 a& e
- if ( get_user_option('rich_editing') == 'true' ) {
. d! T' @5 c. x* B" ?3 [! ^ - add_filter( 'mce_external_plugins', 'add_plugin' );! ]' c6 q- k0 Y
- add_filter( 'mce_buttons', 'register_button' );& p5 L( I6 b R1 [: d1 X! O
- } 4 G( l" V. p6 b! ~' V6 {
- }
銴鋆賭誨蝣 嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁
9 I6 |, u! v& E7 A敺賣詨曉訖ordpress憪賣- add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣 ' X/ Q: v8 o2 J! h( O: Z
4.3 冽( J; ^9 S4 q( M. |- k: h" q
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
& `# t, H# j9 r( z8 p 暺嚗敶箏閰望霈頛詨交蝡賊嚗
' r% n4 [3 U0 L8 R6 H 頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
; h6 F( d" P' h( K5 D: a' W3 ?8 x* l 貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
5 y0 `) l+ l; x0 H* F/ m9 ]8 p& x2 ^ 唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆 |
|