WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)
4 q: Q5 }4 E8 C, u8 |' v+ W
& b6 _, p8 Z' h' R3 Uwordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐
3 n+ m! J1 f! X5 l, g% F隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具. \- L5 n+ Z: X3 q) B: B
$ H- |9 r. S- O! y- t' p0 _( [
凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :
: z( O v# w* ~, Q- U, \甇文 , 臭誑閮剖銝訾批嗆蝡賊 :2 ]# C( J& c! F) n( r5 N
甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :
0 J) L- w% A+ {5 g$ ?2 Q- [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣 3 R# o( \# ?& e+ D* S
" {# W! z9 Z1 J: Z( t1. 蝪∪桃凋誨蝣
' M" E' F+ S! f: a! l: q; P. j- M' b祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :
' Z( S5 Z; D/ @! u$ J) L1 ~/ w閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :
# N3 p5 u. Z. U& q2 D( {+ j. j; o- 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
- 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
- 閮餃賣賊蝯針ordpressaction銝) H; \: k0 e- t$ `- z% T' l
6 i r* ~. S! {$ G5 D, q0 P
祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准
+ x+ A; ?" Q% _1.1 撱箇餈賣
& M/ Z! t* N1 r' H% X8 v. |8 f/ f嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡
6 `3 I2 g! Q+ x7 E# t- function recent_posts_function() {
+ j) ~ C5 y* f! f - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));
* {" g E& S( {9 W" C" c - if (have_posts()) :
" m8 g, g& { A5 D1 g* e; l - while (have_posts()) : the_post();. S7 ]6 p) |- p8 i' s2 n
- $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';
& h* B4 z! D$ S' l3 Y+ |. d - endwhile;
4 h6 v+ `. r. G. T( @; I0 t - endif;
1 j0 V/ t5 l( v& m! H/ x( _& ? - wp_reset_query();
4 C! B" H" J8 @8 F - return $return_string;
+ y) @( K; o1 F. E - }
銴鋆賭誨蝣 憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯6 P: F$ [0 j; x5 ]; Q* K$ P6 X6 E
: U2 L5 C' {5 y3 b* m
6 V% O. u& c3 ]& n, b6 o3 G D$ ]1.2 閮餃凋誨蝣
' W b# O0 ~! \4 }曉剁閮幃ordpress賣豢臭凋誨蝣潘- function register_shortcodes(){
5 {5 x- t9 W3 a8 T - add_shortcode('recent-posts', 'recent_posts_function');
7 u+ y X6 D- ~; h" }% M' x - }
銴鋆賭誨蝣 嗅冽蝡批捆銝凋蝙函凋誨蝣
' u `' K( n8 T# @撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴$ \: K) b* s* l' A; H
1 L( X2 p: j' ^
) E: N1 p5 O3 s8 O4 t5 p( x1.3 Hook into WordPress6 Y# ^+ M! O- I
, F3 q: A% }; s2 m/ m7 O5 Y" Z
箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准( {: Q. L" k" s2 a C! J
- add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣 7 g# ]) M n+ z& I6 g
1.4 皜祈岫凋誨蝣6 {" c* ~ n6 C. U9 O
蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :2. 脤凋誨蝣 2.1 凋誨蝣澆 凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻 閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼 撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 : - function recent_posts_function($atts){
: @/ @, v2 ^, E* U& C: O$ n - extract(shortcode_atts(array(2 ?2 U( G* E! Z* B: J- Y
- 'posts' => 1,' a5 ?" A+ \, ]4 m
- ), $atts));
0 S) r( |1 k5 |5 ?0 l& M1 M3 b - 9 J0 _" { H/ `. x$ o; h
- $return_string = '<ul>';9 g+ B5 I P6 [5 {! [( x/ g
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
& Z! J( `- P7 X* t. Q2 w j K - if (have_posts()) :- T2 U5 q$ G( w2 G' G
- while (have_posts()) : the_post();6 H6 ^( s: G4 e, P
- $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
X( [7 g$ X/ g, o& ]9 F/ V - endwhile;
- A& I) ~- o" V- v - endif;
3 _9 i9 y6 _6 L2 L - $return_string .= '</ul>';8 c1 o+ f; w {4 U' ^2 c
-
. g! v* e! f: v/ a - wp_reset_query();
# j9 Y5 E0 S) ?! v& s8 e; q9 N - return $return_string;8 A f- H! K* u8 v! o" X7 ]* w
- }
銴鋆賭誨蝣 憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :0 @$ D6 Z( O$ q1 G, f. s
2.2 凋誨蝣潔葉憓摰 臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗 - function recent_posts_function($atts, $content = null) {; S' f& A- K3 s: v) L) e
- extract(shortcode_atts(array(
* n: C0 Z$ `' u5 ? - 'posts' => 1,( p' u: _$ A9 j; v) [
- ), $atts));
0 T; R' p. F% {5 O0 X. t0 i- k -
% ]/ Y6 X0 M4 i; }6 ? - $return_string = '<h3>'.$content.'</h3>';
, m0 y7 V: B4 I# s - $return_string .= '<ul>';
1 o6 j6 P/ Q% F7 p6 e - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
: K, s' o4 g/ H, L5 |, o5 s - if (have_posts()) :
; {4 B7 O) ^( i9 ? - while (have_posts()) : the_post();0 d" ]- u& q1 p
- $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';! A9 ~6 R& }& U- [* ^! R
- endwhile;. d+ j' l, V0 j1 z9 S/ Q4 M: x
- endif;0 u' o4 l1 Z6 D; L
- $return_string .= '</ul>';$ S3 ~3 N, B% \7 }7 Q, I2 I! T) U+ h
- ' V2 d+ |- y" u% G! C
- wp_reset_query();
9 m! b+ O% |3 e: d5 c, q+ _1 B+ x/ _, y - return $return_string;* @2 ]9 E0 j2 J/ E5 |- Q
- }
銴鋆賭誨蝣 蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝- [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣 支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗
/ [# J2 {$ n' S3 I4 H8 x$ d3. 其遙雿唳寥*蝷箇凋誨蝣 3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣 閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛嚗典臭誑撠甇斤撘蝣潭曉典撌亙 ' u9 s3 e5 P$ f0 k% X! F& F4 ?
臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 : - add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣 曉其閬靽格嫣遙雿嗡唳 , 瑟, 湧甈撠甇蝣箏圈*蝷
4 n/ m6 W/ P6 m' P5 ?璅嚗刻隢銝剖函凋誨蝣潘 - add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣 冽閬銝剖函凋誨蝣潘- add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣 4. TinyMCE蝺刻摩典凋誨蝣潭
7 ?0 J4 U# X0 v- E! S y& x; \7 I+ j, H0 x
嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗 - 箸迨撱箇銝 JS 瑼獢
- 閮餃JS隞
) k: W) H& u7 g, e' S! {
0 f+ x- T: ]3 n( b" D5 c5 u2 M4.1 JS隞嗡撱箇JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗 - (function() {
6 M% \1 S/ O4 T - tinymce.create('tinymce.plugins.recentposts', {) `5 h: |) k8 I! U8 X# e
- init : function(ed, url) {( q5 [$ o0 a# _0 l" Y+ G: z7 J
- ed.addButton('recentposts', {- m$ c6 k. @/ W9 T w
- title : 'Recent posts',# `/ T. _- } ^% K; V; o- p
- image : url+'/recentpostsbutton.png',
8 F$ t) _4 p+ T1 c( {" D - onclick : function() {7 F% G* \5 m& z; i6 W: I
- var posts = prompt("蝡賊", "1");
4 N! p$ }- P( { t) P+ ? - var text = prompt("隢憛怠亙銵冽憿", "唳蝡");
. ^ T! {6 d- \! j) m - 6 d, X: c* f$ U0 t5 h# q' ]
- if (text != null && text != ''){
' I. ?4 _. d3 t% S5 @5 }% E$ P7 V, G - if (posts != null && posts != '')
# s* d: b; G$ F7 @6 @7 X" ?1 K - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');8 T+ o) l4 i. e4 D% B
- else
" W' Z! L. {8 {* T. U - ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');
) ~4 o# @: K! i9 B U- b b9 _ e - }
- U6 m2 r) T$ Y7 N& G: i: b* \ - else{
: b0 _4 }. D; f6 B* ?- E - if (posts != null && posts != '')
0 P# _" a9 r! Z" J" G' V - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');3 V9 i0 T- o, ^) @6 ]' u& l
- else
. j2 |) E; f) z! u6 J - ed.execCommand('mceInsertContent', false, '[recent-posts]');) ^; q- ^6 E- B: `6 g
- }
4 `0 Y. | X/ u% r/ u5 }# a - }2 p' D }- M: L. U3 r3 T0 z' F( C1 n
- });2 N/ k# b" W6 W- x" ]! T
- },9 L2 z- N3 t- q
- createControl : function(n, cm) {4 T+ |+ H4 }! S- {, @ ~; Y9 E6 e
- return null;& y5 t1 c5 A; C- y* p7 [& @
- },
/ T5 K5 b7 _, W0 `' K, X4 U1 ^2 @ - getInfo : function() {
. g/ i, \, N! a1 h" C0 X, r - return {
- x4 I& D" S1 J0 p3 d9 V! o1 }5 V - longname : "Recent Posts",, @( D( b! Q8 q( B
- author : 'Specs',$ m& s5 A6 p2 r1 q; C
- authorurl : 'http://9iphp.com',
/ g8 B. [* B2 k1 o - infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',# Y. g( Z+ `' d0 b1 _0 z6 j
- version : "1.0"
8 g! @- d- I2 w3 h - };
6 R3 q: H% s3 y, Q - }3 ?2 `* a) l9 c6 H! O
- });
, ^. K% F9 b ^' g4 s- | - tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);
1 ]( q; N2 b& F8 j! J - })();
銴鋆賭誨蝣 澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨 onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉 敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准 5 h; |1 B6 H( N
4.2 閮餃TinyMCE隞1 Y1 {; ?8 U+ b& y8 \1 U" H6 i
0 E7 e R) I5 {9 w) s: b
嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉 蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩 - function register_button( $buttons ) {
9 S0 i# H1 B/ n2 s" q6 [8 e) [ - array_push( $buttons, "|", "recentposts" );4 b" j- r7 A% B( K8 q, q
- return $buttons;0 o6 C5 M0 ?5 c7 Z* r
- }
銴鋆賭誨蝣 蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅, v8 E! S; v' l& j# X9 w' j! a
- function add_plugin( $plugin_array ) {
& S9 t! B6 C% W) Q. W - $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';
; c `& H3 E' A4 p Q - return $plugin_array;
5 K( x A9 |/ r& L, b6 y. q0 s+ O1 s - }
銴鋆賭誨蝣 銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁3 m0 h" ?$ _9 e9 P" b8 A6 |
- function my_recent_posts_button() {
6 j4 D# a! ^) r - if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {% K+ {: B ?/ k3 n- o2 o
- return;9 Z9 t+ B% A6 u! x5 ^ F2 u1 g' V$ |
- }
6 H3 Q' I9 U7 H8 m; ^3 O -
, [9 p0 ]5 F( @( Z. n3 ? - if ( get_user_option('rich_editing') == 'true' ) {
5 h- K1 O7 [ b+ s - add_filter( 'mce_external_plugins', 'add_plugin' );- g- a) n% |3 @2 N# r v& P
- add_filter( 'mce_buttons', 'register_button' );/ J g3 |! f+ V& B! ~: r
- } 5 y( V9 O- z8 c$ p! ~" F1 \
- }
銴鋆賭誨蝣 嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁( F5 M6 u% c1 I% [7 T( e% z2 [- V3 E
敺賣詨曉訖ordpress憪賣- add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣 / y) G4 Z( @4 v
4.3 冽
( l8 |, ?0 M7 W+ B
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
/ _6 Z$ V6 Q n' [$ A 暺嚗敶箏閰望霈頛詨交蝡賊嚗
& u2 ~$ |' b) i1 ^( Q! c x 頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
+ y9 P+ u1 S/ `0 A1 k' U
貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
* ]% R+ [' E) d0 V$ \; x# [. x
唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆 |
|