WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)8 x, l5 g( \. v+ m3 b7 ^
! M" x# K( T {- w. I+ C. kwordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐- w2 x- O& j, e& _7 ~) b0 _5 U
隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具! ~; e0 c4 J! C2 D0 R" x E. N& A
% B f' \" A, V6 }4 w' @) k( ^; b; A! ]4 u凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :1 C) A3 c0 V# G: l; b7 t
甇文 , 臭誑閮剖銝訾批嗆蝡賊 :: ?9 i% V' r5 I3 J$ b
甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :
( x* }. Z6 l4 i8 w* C7 e( o- [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣
/ L; p9 o) m- h: X# `5 K: C. {, q r9 G$ T
1. 蝪∪桃凋誨蝣
) s! c8 m6 a: B1 Z祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :
+ x+ z) X5 _# d" R- I( K, w閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :- u! P p8 T$ {3 o
- 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
- 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
- 閮餃賣賊蝯針ordpressaction銝
0 C) P6 c; x2 r2 [8 n4 w9 S
7 W F+ T' z8 L' H W, z7 f+ `$ e祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准
- G$ Q9 G, E X+ O3 M1.1 撱箇餈賣& X: b4 v. y; ] x v: i
嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡
8 T5 |' \) Q" A& W, }- function recent_posts_function() {
$ Y8 b9 N' {; K - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));: {. ~+ u% T2 p- c( M
- if (have_posts()) :6 h: F; Z7 P# u5 i
- while (have_posts()) : the_post();
% h& J9 w3 ]# ~0 F3 f: q( z3 U - $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';
! {8 u" b1 M( r1 u) q. L% X! K% R - endwhile;; U. C" |& n. \( }0 D: w
- endif;: O6 ]$ j6 |; P" z5 T. T& I) z
- wp_reset_query();. D5 w/ J+ ]8 x' \/ q% ? z1 q
- return $return_string;7 `( m* F$ h4 a6 X1 a4 p8 L3 c1 j
- }
銴鋆賭誨蝣 憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯. j6 p5 l( |2 W" B& s: `
6 E _ Y8 G9 U7 H6 g- V' t3 F; u
1.2 閮餃凋誨蝣
; b' J* E% [5 T' S, v9 y8 A x曉剁閮幃ordpress賣豢臭凋誨蝣潘- function register_shortcodes(){% K- G5 D# D* G% M7 _
- add_shortcode('recent-posts', 'recent_posts_function');
7 V1 N; \8 B" R% B. ]6 u/ T6 | - }
銴鋆賭誨蝣 嗅冽蝡批捆銝凋蝙函凋誨蝣
8 A6 a* H- u j+ W+ w' \# Y5 ?撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴
% m# ~& `# \% t2 u# o8 |2 m: n( u0 _6 a% P! ~' g
$ L) F& t! z5 }# _! q
1.3 Hook into WordPress
' a% X7 S5 E/ F1 p- T. N& N3 O/ M) I, P2 `3 T6 E
箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准) N, z4 b" Q1 O2 u" f
- add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣
& y0 m6 c# z4 H& ?" B1 a1.4 皜祈岫凋誨蝣
" x* L% m; Q5 ?% @- A7 Q蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :2. 脤凋誨蝣 2.1 凋誨蝣澆 凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻 閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼 撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 : - function recent_posts_function($atts){# P: X8 ?6 g4 g7 J( y# {& Y, S
- extract(shortcode_atts(array(
- i/ G$ B; K+ `8 t( ?8 M# Z - 'posts' => 1,
0 t# C+ Q5 G" B# D/ e% Q3 V - ), $atts));
6 Y. N9 ^, X6 M! \ -
5 H Z e% k; D3 E3 ~ - $return_string = '<ul>';. q! J/ _6 O0 f' f0 K9 N3 W8 {$ L
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
/ N, C7 I- h4 M2 n2 v* H2 ]) g - if (have_posts()) :
# Q2 w' @5 P; Z - while (have_posts()) : the_post();
, N' C" \* e- H1 Y& q+ F# Z, F - $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
6 w# w$ H _8 m% S: R - endwhile;7 m" G/ A% a( M
- endif;
' j' x% d% C8 B( T; f - $return_string .= '</ul>';1 R: h- K3 Z6 @2 C
- 2 r! g/ e+ U$ d( Q
- wp_reset_query();
: M: ~# S) h6 }+ W/ W3 U; { - return $return_string;
9 ~/ k* p% z: Z" ]) ^ - }
銴鋆賭誨蝣 憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :2 B- r) ]8 Z$ E" }" k
2.2 凋誨蝣潔葉憓摰 臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗 - function recent_posts_function($atts, $content = null) {) W: P% }; O. ?) G
- extract(shortcode_atts(array(
5 y9 d2 n5 V" E; `# S2 x/ p( e - 'posts' => 1,) I3 q# l) @. N' ^$ A) F
- ), $atts));
- }7 K% Q! z( H1 I+ @; @# R -
V: u& F d" [& A. M$ w - $return_string = '<h3>'.$content.'</h3>';1 F! W( u$ N( z
- $return_string .= '<ul>';
' S( S0 l; t- p7 r" H - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));6 U) f5 g/ h, d, M p
- if (have_posts()) :
6 R* f, a. R9 H& v- I( A* m - while (have_posts()) : the_post();
- ~, H( x& J7 P2 ^* c - $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
5 p3 {% e; M* r8 e - endwhile;
, R7 d o* q" J) R! q/ e6 x - endif;2 s: b7 U6 v8 U1 |- o
- $return_string .= '</ul>';1 C' e/ m7 I5 B/ N4 U$ v {9 P
- 6 |, B! c9 y6 y4 b1 `- ~# n
- wp_reset_query();
3 M7 K8 K* @& G. O# I& m - return $return_string;
' L, E/ I |4 B1 t2 G% ?/ A) Y - }
銴鋆賭誨蝣 蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝- [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣 支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗. r( X9 O1 P* K9 s* f/ K
3. 其遙雿唳寥*蝷箇凋誨蝣 3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣 閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛嚗典臭誑撠甇斤撘蝣潭曉典撌亙
$ l7 d9 }# F( r0 U) v, z臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 : - add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣 曉其閬靽格嫣遙雿嗡唳 , 瑟, 湧甈撠甇蝣箏圈*蝷
/ z( h( N; |* ^9 ~9 H. v K璅嚗刻隢銝剖函凋誨蝣潘 - add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣 冽閬銝剖函凋誨蝣潘- add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣 4. TinyMCE蝺刻摩典凋誨蝣潭
0 s" q, b' p- r& W9 G& p0 } R
. y& S; m" T# v嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗 - 箸迨撱箇銝 JS 瑼獢
- 閮餃JS隞
8 ?* B+ }. l9 r9 s1 o, D$ g
2 ]% |( l: l! e2 ^+ D2 `# B" B4.1 JS隞嗡撱箇JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗 - (function() {3 {! @6 L# E+ B' F; L/ G
- tinymce.create('tinymce.plugins.recentposts', {
3 j! g- m! Z- a' A! ]; ^ - init : function(ed, url) {
@: ^6 A4 ]2 f% v* I/ N( Q - ed.addButton('recentposts', {
' {: e, V7 k7 B: p - title : 'Recent posts',
) L) W! D3 I% d7 y, d; F - image : url+'/recentpostsbutton.png',
7 _5 x% y2 j, P - onclick : function() {
0 I* v5 f- M4 U - var posts = prompt("蝡賊", "1");7 R4 c5 M8 J0 ^; M1 n& p3 e
- var text = prompt("隢憛怠亙銵冽憿", "唳蝡");
, j5 V( f. [, G. _ -
7 j+ E! s( r% m - if (text != null && text != ''){
3 m, u7 Y- d: R( J& I - if (posts != null && posts != '')7 J6 m/ @7 d( h/ C2 O
- ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');
: \/ P! J2 V; h - else
( G. q. b# a2 ~/ q9 V: \7 e - ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');* ~. I! |$ z' f( K+ l R
- }6 X4 F9 @' I0 s4 t$ |* U9 y
- else{: i, V) f7 s5 S
- if (posts != null && posts != '')( g) `7 l# |9 k
- ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');
8 x S" i i4 |7 Z+ C - else6 Z1 s0 W6 M7 G* {, y
- ed.execCommand('mceInsertContent', false, '[recent-posts]');
( _0 f) S0 w( O' A1 E - }. E |/ Q+ k1 G+ _4 w1 Y
- }6 f: \* ]) P, t- b& {
- });% m% }6 B( m% |% c
- },
" |# Q1 ?, m8 K# J. c3 v - createControl : function(n, cm) {, v) o/ c& f- Q0 [7 ?. s% p% z+ |0 O
- return null;
$ [8 F: d: r2 n5 u1 a - },% N4 M L) L5 \( Y7 _) X; K5 M
- getInfo : function() {
) m- y2 o' K( k5 w9 e. z3 C - return {
/ b) @# J. |: A- A. F/ V: C% R - longname : "Recent Posts",6 Z" \4 D+ J& @2 c. M
- author : 'Specs',
R: }1 B7 @- y1 { Q( ~- x, Y5 Q - authorurl : 'http://9iphp.com',
5 w- k u/ r: m! _ - infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',8 g3 K5 T/ a. Z3 s# ^; |& `
- version : "1.0"
1 J0 I/ h4 {7 J1 i# Y: S" A- H - };- ^1 t4 b- y. j$ c3 z
- }
+ h- D$ o7 ]) [# x0 _. b6 y - });/ i. y! Y7 v/ g( P/ F. f1 g
- tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);
1 [( F: ~1 ]! `; ^ - })();
銴鋆賭誨蝣 澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨 onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉 敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准
# ]; ?4 a4 ]6 t
4.2 閮餃TinyMCE隞
# i4 h' W; O( Q6 E1 [1 M
' a2 P( N0 t: }1 w9 z 嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉 蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩 - function register_button( $buttons ) {9 |! e0 o/ f; N e6 e' Z% `
- array_push( $buttons, "|", "recentposts" );) ]! {$ d$ w3 Q9 O! R
- return $buttons;& I* \/ M/ r# R' q5 B1 l. j/ B- E
- }
銴鋆賭誨蝣 蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅: q' \/ T# ~$ J. b' a0 _
- function add_plugin( $plugin_array ) {) ^4 |3 y5 Q$ J: n
- $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';+ F2 r* }( | p5 s0 A4 V) S
- return $plugin_array;
) x- p! ~8 J" U+ R. f' \ - }
銴鋆賭誨蝣 銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁
7 N* b7 q; k$ `3 m9 D+ g5 z9 D- function my_recent_posts_button() { 5 Z* x" K+ I1 i5 f8 K! u+ E
- if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {
2 a2 r# B8 U5 ?3 p' y: C - return;- ~) c* n% ?" X% ^" u
- } g( S: x% I; R* h8 d
- ; b Z! M: f" d- w: A) V. C
- if ( get_user_option('rich_editing') == 'true' ) {
- ~; n4 V( x. K* I: f - add_filter( 'mce_external_plugins', 'add_plugin' );
8 r$ K2 z' {- O. u5 Z - add_filter( 'mce_buttons', 'register_button' );
+ r2 V* W; ~2 p7 [" e e - } ) A( l/ H7 ^& P
- }
銴鋆賭誨蝣 嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁' |7 ~6 S& p V2 n4 K/ s. z e
敺賣詨曉訖ordpress憪賣- add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣
1 P, v( ]0 M3 y) q6 y7 k. V9 g4.3 冽! A# D2 K6 M7 p# G) Q6 {
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
0 {' A( }; A- u& K7 U: B3 @ 暺嚗敶箏閰望霈頛詨交蝡賊嚗
5 |& {; W. g1 Q! R/ F
頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
" ^" \1 }; f# p5 c6 _9 n 貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
6 y0 b# t3 L$ ]7 \ |- k( _ 唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆 |
|