WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)
) F8 d* x! r# D8 A5 U
2 o5 d& j0 d9 S5 owordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐& P8 n- c2 T4 J/ ~6 }2 Z
隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具
% i! G- y9 r; k
7 C: W, e$ q! u2 C: h! u1 {( X凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :2 I0 x% ? W& ?. c, B1 Q- A1 Q
甇文 , 臭誑閮剖銝訾批嗆蝡賊 :
: J) [, ` z7 k3 x甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :
- @8 p' V6 u @- [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣
3 s0 B6 b" ^6 F' V6 u
3 r) l. L. q+ n$ L% i1. 蝪∪桃凋誨蝣
: e' }: v7 w8 c6 H, Y+ Q祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :1 [5 Z* u6 }8 c( x, X; q: s9 C( I
閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :
% G: {( B2 V3 ^6 C+ ?- 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
- 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
- 閮餃賣賊蝯針ordpressaction銝4 u; u" i- b& G Q* ~( M" \
+ R! v; h" }- @- M" h V# i祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准* M- b$ Q. c% H- J" p0 U
1.1 撱箇餈賣7 z) j$ ]5 z5 p6 ^, V& }# K; c9 W3 K; I
嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡' F/ Q; _5 q- k4 E) ~; d
- function recent_posts_function() {' m2 ]/ _( ]2 q0 n3 y* d Z2 i# u8 j
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));
* m( B/ _* b7 k4 U; U - if (have_posts()) :0 s$ r4 V2 L2 w3 y) L8 W
- while (have_posts()) : the_post();
4 S+ g& a* V s. S1 R" G6 {/ v - $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';
! x& q& `4 g" P: E5 I& A - endwhile;
r0 z+ M! d; n( x( W# Q& b - endif;
% A' o1 Z! w# ~+ C( A - wp_reset_query();1 Z8 I# I+ L3 b, q
- return $return_string;
$ X3 G- J9 x" G# o# l e7 t - }
銴鋆賭誨蝣 憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯
" ?- E. M m( H# x9 L1 ?9 u' u* D" D+ e
7 t( H, K; {6 u# h5 Z' ]& j1.2 閮餃凋誨蝣0 E6 P* t; H8 f
曉剁閮幃ordpress賣豢臭凋誨蝣潘- function register_shortcodes(){, `3 N4 o) i% U+ n& u) |
- add_shortcode('recent-posts', 'recent_posts_function');9 J& T' b8 S/ r$ {8 _$ p
- }
銴鋆賭誨蝣 嗅冽蝡批捆銝凋蝙函凋誨蝣
6 p7 `0 R( C f0 c; b/ r* f撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴
* F2 A s( d: _- p4 o; m" h. O g E
$ p( f7 `: @% a7 _6 ~1.3 Hook into WordPress
# ^( K* E4 @* m* } f- Q; y: v
箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准
$ f* D/ }5 g& [) @. V4 f2 x% y- add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣 4 x3 r7 k% z+ n2 L" l# y! p; j
1.4 皜祈岫凋誨蝣8 @: \$ m, {* g( K
蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :2. 脤凋誨蝣 2.1 凋誨蝣澆 凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻 閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼 撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 : - function recent_posts_function($atts){9 j2 d- ^" H8 r8 W% R
- extract(shortcode_atts(array(
$ @+ }5 ]% r! M: E - 'posts' => 1,
8 t2 V/ {# f; k$ l3 ?; a - ), $atts));
8 \3 E" x+ {3 a9 u% f, r6 U - ; ?7 A$ a/ m2 _7 ^5 `* D. u0 P
- $return_string = '<ul>';
U, L2 S' w! M$ k1 }; o - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));- T7 t# z2 a. k. V
- if (have_posts()) :& w4 Q t; {* c: u
- while (have_posts()) : the_post();* f. {( f; Z- j3 {# U! ^
- $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';% w! E' t; W, e- R( s# I
- endwhile;
2 i# X8 m5 m% X - endif;
9 Z& f3 Q$ c6 V7 i& h - $return_string .= '</ul>';
" [. Q) F) m- }+ g! K& l* R -
: x- h$ k8 [8 Q - wp_reset_query();
9 d1 w3 H0 j7 [3 `7 A - return $return_string;
, x# X0 Q7 p) t4 A0 [8 Y - }
銴鋆賭誨蝣 憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :
! V2 s4 X# u* A; Q' o2.2 凋誨蝣潔葉憓摰
臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗 - function recent_posts_function($atts, $content = null) {
- v! I9 z5 @" ?: d3 P - extract(shortcode_atts(array(
2 E( a, W" v j# J - 'posts' => 1," f0 p/ t' }* V, R
- ), $atts));
. `9 E, @5 s+ J% ~# S. m4 N - ( ^( C& [ z2 {; A0 n
- $return_string = '<h3>'.$content.'</h3>';; a6 N6 b1 _% a" P, C& l. n
- $return_string .= '<ul>';0 I- r/ @- j$ i! z* _
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));; T( u7 O7 g. I2 d1 R
- if (have_posts()) :
0 H- @5 ~; k" Q3 T* U - while (have_posts()) : the_post();% n8 |6 `* H2 k1 W
- $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
5 w/ a/ s& Z8 a1 {1 } - endwhile;
* }8 S( L& o$ m0 W3 F; \ - endif;" z0 Q. d# N" L$ M- S; l$ e8 N
- $return_string .= '</ul>';
. M7 I8 w9 |0 c6 ^" x" B6 h3 D) e" L -
. h% G. O1 M) L; A4 s$ s - wp_reset_query();2 m7 ^5 {+ m8 h3 m" [1 `. t
- return $return_string;- B" Z0 T; H d0 f9 O+ w4 O u
- }
銴鋆賭誨蝣 蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝- [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣 支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗+ I- w% H4 g! j0 ?7 z
3. 其遙雿唳寥*蝷箇凋誨蝣 3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣 閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛嚗典臭誑撠甇斤撘蝣潭曉典撌亙
9 x/ d' P* A5 p0 B* I$ O: X臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 : - add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣 曉其閬靽格嫣遙雿嗡唳 , 瑟, 湧甈撠甇蝣箏圈*蝷
( \; a* g' v% H$ B ^$ X' r# n( E璅嚗刻隢銝剖函凋誨蝣潘 - add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣 冽閬銝剖函凋誨蝣潘- add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣 4. TinyMCE蝺刻摩典凋誨蝣潭/ P2 ? U( B0 m; ]2 ?7 D
2 W& h: K; J$ ?' C. c f/ A5 P嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗 - 箸迨撱箇銝 JS 瑼獢
- 閮餃JS隞
" [: Y# L0 u+ X% t* Z; ? # e) r* V5 _3 T' O
4.1 JS隞嗡撱箇JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗 - (function() {
7 g* o) b; d5 w' O- \ - tinymce.create('tinymce.plugins.recentposts', {
+ h3 _' M: Z/ j - init : function(ed, url) {' ?9 r% [2 @6 o
- ed.addButton('recentposts', {( A6 I# ^1 h2 h9 l. ~ E' [& ^
- title : 'Recent posts',# ]+ N$ X" z: |5 v1 P% o
- image : url+'/recentpostsbutton.png',3 T$ a4 t2 z9 ~" d" p
- onclick : function() {
0 B* R2 }7 X( s; f' O - var posts = prompt("蝡賊", "1");
# ^( ~; N/ `+ L1 ? - var text = prompt("隢憛怠亙銵冽憿", "唳蝡");6 {& v! K+ D* A) n7 \, A b( k
-
' k' Q$ z5 g% ]1 [ - if (text != null && text != ''){: r$ F$ L% g$ Y a4 D9 j
- if (posts != null && posts != '')
% q8 Z" e3 D; j* l2 i4 _ - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');
/ n( P2 G/ j h! u+ U' k. v- _7 w0 a1 W+ w - else
, \. N5 \5 g: P8 p2 C" d; U& N( v. a: S - ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');
# k' r1 V5 j. _8 V - }; |% t& T' n2 T; g% I
- else{
$ [ l; ~* ^8 F - if (posts != null && posts != '')
; P" H* R3 u4 R# {) t - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');
' A/ H7 W$ H" {# ~: E1 u# {, ] - else
" E" E! x, H9 M# v* ^ - ed.execCommand('mceInsertContent', false, '[recent-posts]');7 J- n! \, V$ i2 q5 h6 z
- }
. L% U9 N9 M0 z5 N6 _, t4 d - }
7 W" T; ^8 Y" w- q" T& F - });/ D" d6 J# U- p- N4 F5 C
- },0 t: q+ D+ S5 @& L
- createControl : function(n, cm) {5 T% \6 ]0 B2 W
- return null;% P! R( k, j) O9 |9 ^1 _
- },
& [1 M5 ], M$ h1 p! V - getInfo : function() {
: J% _; |. N2 b& `) e2 l - return {. r2 o) O7 C* |: Y( C3 O
- longname : "Recent Posts",, F# ^1 O9 a5 ?/ ]& v' j* Z
- author : 'Specs',
- k& v& E! d+ V0 c - authorurl : 'http://9iphp.com',
" u, `- U1 W% K( Z - infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',
# k0 L; Z6 q, O. |. m; Y) B - version : "1.0"- q' U! s% T" ]
- };7 X* `3 Q: N/ _4 @
- }) e, l* Y. y! @& y
- });+ J! d! [! O5 l' V' n4 h
- tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);
# k# A/ ~8 r9 y/ L% @4 I2 c% I3 L - })();
銴鋆賭誨蝣 澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨 onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉 敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准 $ Y$ a" W$ D1 {( {+ s: x
4.2 閮餃TinyMCE隞
( q4 K$ _- { J( g q
- z0 t }6 x% H& z' y% z 嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉 蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩 - function register_button( $buttons ) {
7 N5 l% \, U- e - array_push( $buttons, "|", "recentposts" ); w# Z! g! _$ e
- return $buttons;
4 ?. `9 J! R* T - }
銴鋆賭誨蝣 蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅2 ^+ Z4 P, q- F5 Y0 V
- function add_plugin( $plugin_array ) {
( J5 s' D/ [. h0 ? - $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';- ~3 p+ o1 n; G3 T: y( \
- return $plugin_array;
! F5 O' @# H. F( R" b' }, A+ ] - }
銴鋆賭誨蝣 銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁% i% g+ a2 R! T. J* C; e6 O/ Q- n
- function my_recent_posts_button() { 2 u% i( }9 @/ E7 K
- if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {
& A6 b q. f2 N1 v - return;
- T3 v% A" U8 b# l - }
+ T% J p- ^/ M, t/ \. {( \ - % F) B" i1 e3 k$ T
- if ( get_user_option('rich_editing') == 'true' ) {) o0 T5 P7 }9 ^ K
- add_filter( 'mce_external_plugins', 'add_plugin' );9 |( z* _1 l" w. J3 h
- add_filter( 'mce_buttons', 'register_button' );
8 ]. t1 `. h) a - }
; Y5 }( n) x9 @0 L5 j' \: _3 o - }
銴鋆賭誨蝣 嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁; B4 v0 T+ b% U$ z9 K2 O
敺賣詨曉訖ordpress憪賣- add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣
' `7 ]/ p% ]% o4 z; P) ~4.3 冽
9 Y7 [" l' s0 E1 P9 [( N; C
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
* e0 Q: t1 {" b2 Q+ K3 K 暺嚗敶箏閰望霈頛詨交蝡賊嚗
6 p2 K z8 X8 b$ N5 t4 G: ^" ] 頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
( _8 h" _7 g8 T& m* r l- @ 貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
# `$ I0 v; b& D* V/ w: F- v 唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆 |
|