WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)% |7 Y$ [0 d( t. S- v
~8 n5 A+ K$ k& D" I: Zwordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐" s. [2 H$ r1 v
隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具
3 N' x& c t8 o
1 c# N0 X$ n, s Z6 @' o. e+ b凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :
9 Q* l+ L( V9 Y6 h" i甇文 , 臭誑閮剖銝訾批嗆蝡賊 :
; V( A. P' z8 g9 V$ y甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :
8 ~5 a( S! @6 p0 v; ^) ~4 h* a- [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣
4 q: X& t- L6 R7 I1 W q; G- \$ c
1. 蝪∪桃凋誨蝣
$ w/ Q. x6 Y+ F$ }祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :0 n8 h" w+ r% ]6 \8 J
閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :
) E+ I- K6 h1 h" u8 C- 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
- 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
- 閮餃賣賊蝯針ordpressaction銝1 d! F( r& o9 K) o( N
& K& y: w7 K6 p, j. x4 ?0 H6 P祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准- R+ z6 k. U g2 W7 ~
1.1 撱箇餈賣
1 d) L: h- B* c6 A* a+ F嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡5 P+ z l8 V! v/ U1 S
- function recent_posts_function() {
- p( r: q+ `+ n& M& a/ c - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));
+ \0 [7 j( A$ o2 D - if (have_posts()) :
; g Q. T+ W+ t - while (have_posts()) : the_post();" U2 }/ @/ S/ y( E& q$ Z S) A K
- $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';
; ]& [, N, q c3 u' \' H) D" o* j - endwhile;2 W; n* ^" |6 V) U
- endif;+ j# \* J, W7 b- G
- wp_reset_query();
2 r$ _$ y: ]# H - return $return_string;
8 n3 C- ^! d. a2 S3 H0 e - }
銴鋆賭誨蝣 憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯
* I' T) b) K m
, h9 \: B! N8 F, w
/ n" n4 h6 t& H4 v K* q1.2 閮餃凋誨蝣+ K$ {) w5 g- Z+ N: L( W5 V- M
曉剁閮幃ordpress賣豢臭凋誨蝣潘- function register_shortcodes(){
8 }% _$ q: }7 ?# h* ^ - add_shortcode('recent-posts', 'recent_posts_function');
& S# u, p- E! }% d5 F- R& T - }
銴鋆賭誨蝣 嗅冽蝡批捆銝凋蝙函凋誨蝣2 T6 w; |0 P; t) V5 M. ]( X
撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴
% n( t! ?$ m& l H! y( A7 C5 f( ?5 Y# S; a: |: C8 [4 ]
Y) r, G. f* x- g: f
1.3 Hook into WordPress
) D. U, X& C2 b1 Q j0 q4 a; U4 P! h. [0 l8 }9 ?/ Z! A
箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准4 ^' e G6 h7 _( M
- add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣 ' X5 _: g( e# s$ {0 f+ S
1.4 皜祈岫凋誨蝣
( D: q' W' P' w% f4 u' ]4 X蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :2. 脤凋誨蝣 2.1 凋誨蝣澆 凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻 閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼 撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 : - function recent_posts_function($atts){# t+ U% O& P4 a3 ?2 K' J( Z
- extract(shortcode_atts(array(
3 z8 { w$ u2 Y- L b( N1 D, S - 'posts' => 1,
2 e9 X- T' D, y$ X. z. Y - ), $atts));
; w) E# N1 x2 L; a) |8 V6 { -
4 k3 ~/ T; V5 J2 L( d% c1 f - $return_string = '<ul>';
2 d4 p Z, e4 U& k$ G% k7 n - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
. j8 T/ P6 G8 m: M3 M' k$ o7 ] - if (have_posts()) :, x8 y- ] \) }+ _ @
- while (have_posts()) : the_post();
8 P4 {# M* \1 x( X$ C" n1 `! |& p - $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
& Z6 |# y) ?7 ~" b$ g2 g8 [6 z* K - endwhile;* |5 @( ~( E% z( T) t* n
- endif;3 n7 q. Y9 G& D/ W
- $return_string .= '</ul>';
3 X( }/ q% s* `" Q! ^8 ] - 8 q1 O: r; b3 \9 n
- wp_reset_query();
! N4 u: B$ b/ C: T5 ] - return $return_string;
% n; n. C, W2 M4 h/ T% q0 d7 z( \+ q - }
銴鋆賭誨蝣 憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :) p; W2 A, E$ X2 Q3 ~$ V# b
2.2 凋誨蝣潔葉憓摰 臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗 - function recent_posts_function($atts, $content = null) {( } s* m( @% M: j9 X
- extract(shortcode_atts(array(
" j+ |( Y* G+ `/ S+ u7 ]$ a - 'posts' => 1,5 l7 }8 k+ p6 E9 l4 M- J7 Z
- ), $atts));. K1 i7 C: E+ w
-
8 {2 E3 m5 }& D- M/ Q/ g& c" s - $return_string = '<h3>'.$content.'</h3>';
4 \' {# e$ G( }+ Q$ G% ?8 _; t: m/ V - $return_string .= '<ul>';
! x, h1 O) N9 D - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));. D# j5 {6 A) q4 L
- if (have_posts()) :
n) c4 k, u# S - while (have_posts()) : the_post();1 J8 c0 |; G! [) j* v. ^2 w
- $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
! _5 P" ?& G- M o l - endwhile;
. g9 Z- D. I; u) e8 E - endif;
$ k1 n' p) V7 c. ~ - $return_string .= '</ul>';
* r1 q, ]8 @4 ^! m - 0 d4 I, N* D9 O: `: n3 e' z I
- wp_reset_query();9 X* c# \' M9 s0 {
- return $return_string;
5 M3 b1 f+ S7 k6 m6 b( I - }
銴鋆賭誨蝣 蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝- [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣 支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗
1 f6 W, ^0 {1 z6 ?3. 其遙雿唳寥*蝷箇凋誨蝣 3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣 閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛嚗典臭誑撠甇斤撘蝣潭曉典撌亙 ( ~, [2 a/ I4 N- E. Q0 x
臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 : - add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣 曉其閬靽格嫣遙雿嗡唳 , 瑟, 湧甈撠甇蝣箏圈*蝷 5 p# y* t6 ] Q2 {2 }: @
璅嚗刻隢銝剖函凋誨蝣潘 - add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣 冽閬銝剖函凋誨蝣潘- add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣 4. TinyMCE蝺刻摩典凋誨蝣潭
. _- T2 `2 |2 `/ d, ]
( X0 u1 j. h M1 r' h& |0 Y& s嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗 - 箸迨撱箇銝 JS 瑼獢
- 閮餃JS隞
3 R6 z" }7 Z, B- W , q d5 J! H. O+ m6 f
4.1 JS隞嗡撱箇JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗 - (function() {3 u8 H! S2 @) m
- tinymce.create('tinymce.plugins.recentposts', {' ^# `! |$ p% Y9 [6 a4 ?
- init : function(ed, url) {
7 w+ ~! ?9 K8 p6 C9 Q - ed.addButton('recentposts', {6 F1 O, j* P- D1 N1 D* @/ H' I, ~
- title : 'Recent posts',
/ J" N# D+ u# X2 W - image : url+'/recentpostsbutton.png',
. K" e* i8 D5 P. j% J - onclick : function() {
9 O! H3 j& {/ @3 M& s, B6 K6 [4 K - var posts = prompt("蝡賊", "1");5 X V4 e: I+ ` e
- var text = prompt("隢憛怠亙銵冽憿", "唳蝡");9 L1 z7 H$ F! n/ R3 C. x; |
-
5 R! z9 k0 u7 t9 |9 M - if (text != null && text != ''){
5 |# `" p) I7 |8 C - if (posts != null && posts != '')
5 m9 \/ i& M, B6 ^; X' J% x - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');2 K: f5 o# e* x6 k
- else9 N( L; h& D- \( S
- ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');- E6 I4 ?7 g7 d
- }+ m4 K# M2 B( u' I" @# C
- else{
8 S4 z% c! G. P! @ N - if (posts != null && posts != '')
( A! e( ^' j: f - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');/ A6 p$ }9 b$ X8 L4 }
- else
* {& F, @) e% E - ed.execCommand('mceInsertContent', false, '[recent-posts]');/ H" d4 t! A `: b7 D8 b$ S
- }
# ^% B; i$ M* N5 ~+ k# X" G - }
5 q! _' L X _% I1 u+ o1 \ - });
5 \$ M q3 t1 k - },) f& \3 c# E5 c) Y
- createControl : function(n, cm) {
% K# M5 V# [; Y0 t* A2 Q$ u - return null;
% i2 _$ v4 T5 m3 b9 ` ]" w - },
, w6 }4 H1 A$ U y+ g - getInfo : function() {
. e+ J/ {; \( ~* N - return {3 Z" ]7 l8 C W0 y0 Y
- longname : "Recent Posts",' e* R& Z( A& c# k
- author : 'Specs',
4 s- v$ z: z/ P, S8 u - authorurl : 'http://9iphp.com',5 R, c. M8 }2 A
- infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',1 n2 \, p, ^. L( Q! c0 V0 O
- version : "1.0"
0 ^. d2 `9 v j2 `: z7 J0 { - };5 E+ {+ b; s M0 \* S5 p
- }' I3 l5 C# c8 r* l: k- J) D
- });
8 R3 ]9 u X) Z6 @0 E - tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);" K8 \% D: ~" q# N( Q
- })();
銴鋆賭誨蝣 澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨 onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉 敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准
8 O; ~) m5 {2 W& N) P) p
4.2 閮餃TinyMCE隞
% Y1 _- u, r: k& z! F4 b; u1 B6 z& w# x# M. o0 Z: \
嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉 蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩 - function register_button( $buttons ) {
: m4 f0 J' @, r* o" c4 @, P - array_push( $buttons, "|", "recentposts" );
4 k! ]! F: T- p$ S - return $buttons;
! Z% j; ]. H4 k+ W) y - }
銴鋆賭誨蝣 蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅
) \" r) h" P; \- T& ?- function add_plugin( $plugin_array ) {. W4 O* @2 P0 M, {
- $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';0 V: V& _( D' s1 E+ F6 z; n
- return $plugin_array;! J2 r: v9 m# z. T0 q0 ^( @/ }* q
- }
銴鋆賭誨蝣 銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁
3 N* A# W( \* u- h& d$ g$ m" N- function my_recent_posts_button() {
* w* X% i% k; L4 ]) t8 \ - if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {# Y2 [5 J& c) \' {5 a( z5 r
- return;+ B2 j+ H! S2 d: P
- }
# L9 t5 p- a0 V+ k3 [ -
. v$ O, A S$ }/ k/ ?: e2 V; V! | - if ( get_user_option('rich_editing') == 'true' ) {
a. H& I F5 v$ H - add_filter( 'mce_external_plugins', 'add_plugin' );
* b- K4 E9 t( u; {6 g - add_filter( 'mce_buttons', 'register_button' );1 z- R# C1 D- D( n6 a
- } 4 O8 @& Q! g* @; z' U# A. _7 w/ r7 U
- }
銴鋆賭誨蝣 嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁
0 z( G) y1 M2 q" z0 {# T. C! X敺賣詨曉訖ordpress憪賣- add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣 . H) @" D; ]2 m9 G" k% m1 F
4.3 冽* _. s- R+ d: X2 N* ^
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
8 F1 t1 q' X& [( B0 ]+ Q 暺嚗敶箏閰望霈頛詨交蝡賊嚗
2 q& d1 h2 S$ ~
頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
+ Y# k7 g0 d5 C. B 貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
9 F6 P+ W: h$ H* W ?) q0 e7 Y/ e! m 唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆 |
|