WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)
& _& |0 _& i }( w# |: T4 l ~* N* ^, O0 X7 B7 M/ b8 w
wordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐
5 a( H1 R" T! I& r& r2 D隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具$ X5 S0 t s* H9 e3 R8 {6 F7 {
\$ u" h4 x& S凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :/ g3 B3 e Q% G* o% V2 v9 F" V
甇文 , 臭誑閮剖銝訾批嗆蝡賊 :: L/ V; V7 m0 i- h' m
甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :6 C' {! ]$ x2 Y4 v+ B
- [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣 # w* R+ M5 o# W2 X# x3 F+ G
6 p2 j% T; Y! T1 J& S/ i" J
1. 蝪∪桃凋誨蝣
" c! P' ~5 ~4 e3 a, W- M6 w" X祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :
+ W$ C$ ~0 D( g: ]閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :
5 L2 H: \ T# H/ c- 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
- 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
- 閮餃賣賊蝯針ordpressaction銝
- B0 j/ Z# ^6 v 7 V) ~3 i" l& S( c9 W
祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准
0 J, x# K4 ?* R2 A; v1.1 撱箇餈賣( N) f0 h* k( A* g/ f2 a
嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡
5 p9 D! b; M, } k( l- function recent_posts_function() {
, Q" V7 v* |0 Z/ p3 E! ?0 d) r$ k K - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));0 K, ?+ S6 h+ M& x) T9 X6 v: n* E
- if (have_posts()) :* x, t7 o8 n* `6 P& r8 _: v: k" [) U6 ?
- while (have_posts()) : the_post();& d# r% z/ T* v1 K. I; m
- $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';' p5 q$ ^4 o0 a1 x' I
- endwhile;
+ ?, A' a# A/ D1 d0 a% v - endif;1 K" V' B5 S. }0 x
- wp_reset_query();' B- B3 ]0 F) m6 f5 \% [& E; w6 g" O
- return $return_string;
3 U! ^3 R" ^- O7 G6 m0 M) s1 E0 [ - }
銴鋆賭誨蝣 憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯
# B* h, I) H) k5 S% C9 s$ K
+ j+ I9 ], _7 I+ x; S
. C- G: p; h' o* o1.2 閮餃凋誨蝣6 I8 I$ @4 A/ U1 v/ ~
曉剁閮幃ordpress賣豢臭凋誨蝣潘- function register_shortcodes(){: Q8 U! v5 D# N# f$ d7 H: y6 {
- add_shortcode('recent-posts', 'recent_posts_function');
- `1 y6 F9 h/ A" u5 t: ~" ~ - }
銴鋆賭誨蝣 嗅冽蝡批捆銝凋蝙函凋誨蝣. t M! {: R- _& T
撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴
% Z6 a0 I" w5 p! i9 V0 `0 @6 b
8 |9 v4 c! W8 ?& K. \1.3 Hook into WordPress. Z/ _5 X7 h' w0 O
Q+ K. i# _) K( R4 ?3 }4 ^* ~+ H
箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准
1 h3 z0 |5 o* B" A/ P0 r- add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣
" j& W J$ X0 \9 {- L1.4 皜祈岫凋誨蝣
' z g/ y+ G* B, _ Y: c. y7 d蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :2. 脤凋誨蝣 2.1 凋誨蝣澆 凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻 閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼 撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 : - function recent_posts_function($atts){
3 ]3 t9 i7 _7 t U$ W - extract(shortcode_atts(array(' ~, D+ r$ E% l2 _; @9 ] r1 r
- 'posts' => 1,, Y; F% X3 \; m, p I! A' q
- ), $atts));
% z; i U3 f b6 x P' M- v - + @- e2 {" j( b0 M5 m$ K: M4 k* u
- $return_string = '<ul>';6 o: N( l& \7 Y, c- b: u: s1 g
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));; y( x# ~ ]1 t" }1 f3 J
- if (have_posts()) :; e6 n1 p* r- D1 p( ?
- while (have_posts()) : the_post();
6 w& i; A) ?( n, L* u+ J0 m - $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';1 }( o. z# ]' P" ~
- endwhile; I7 p N" ~4 o/ V
- endif;
8 B& {& Q; j' Q- n$ _! C. ] - $return_string .= '</ul>';
. s. B: J: u" Z1 `4 Q - . @/ _# y2 v/ _, _3 {
- wp_reset_query();
% k& D" r" \$ z; r$ H - return $return_string;
( C) u5 U5 l2 h4 ^( Q' g& E) z8 X. k - }
銴鋆賭誨蝣 憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :
4 w; W3 C d( K# S( `% x2.2 凋誨蝣潔葉憓摰
臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗 - function recent_posts_function($atts, $content = null) {! l* ?- E2 {/ ]( v7 h4 H
- extract(shortcode_atts(array(+ R% K. R, h& s' w
- 'posts' => 1,+ r3 C- }! C7 f. ?$ ]: t
- ), $atts));
6 a; d6 o5 E) w& z1 E+ B7 P7 M - 7 U" N( ~. H% X
- $return_string = '<h3>'.$content.'</h3>';. n4 z3 S9 D3 e4 I
- $return_string .= '<ul>';
- q( _1 w" w- W8 u8 r - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));9 }) f1 F# n t, r1 E
- if (have_posts()) :
3 x4 _: P4 c0 x1 D9 C1 P - while (have_posts()) : the_post();3 M- A' R& _% y% @ \
- $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';7 f7 z3 s2 t" K- H- f
- endwhile;
! M6 K" `9 K5 C1 w$ O7 F! E - endif;
8 T F5 }- H. K$ W# ?- h - $return_string .= '</ul>';3 m6 z$ _9 J9 p& g) B5 s1 D4 F
- 3 R8 e& R$ v5 q4 c% s/ C- e9 L0 x
- wp_reset_query(); X2 c6 j4 j* r& b: e
- return $return_string;
; r0 T1 _; ?# t6 u* I9 U5 Q - }
銴鋆賭誨蝣 蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝- [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣 支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗& T) \( a' q7 ?- J5 y) J
3. 其遙雿唳寥*蝷箇凋誨蝣 3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣 閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛嚗典臭誑撠甇斤撘蝣潭曉典撌亙
, d1 ]( ^0 p) i- r! j- l臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 : - add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣 曉其閬靽格嫣遙雿嗡唳 , 瑟, 湧甈撠甇蝣箏圈*蝷
2 q5 F1 t* h$ |6 A/ X" z; ]$ c7 P璅嚗刻隢銝剖函凋誨蝣潘 - add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣 冽閬銝剖函凋誨蝣潘- add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣 4. TinyMCE蝺刻摩典凋誨蝣潭
2 S' i% Q. Y) b1 b/ O2 z
6 l% y+ c2 k" `# V L嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗 - 箸迨撱箇銝 JS 瑼獢
- 閮餃JS隞
W% L# M: \4 B" d% U! x 2 C* ^6 |. r" B( V
4.1 JS隞嗡撱箇JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗 - (function() {
/ t4 ~" ?# E, Z" I5 h( i b - tinymce.create('tinymce.plugins.recentposts', {
: v( O% k Q7 \& j t% R- T, X0 V - init : function(ed, url) {* t9 y/ j+ D! I8 }1 t! ?+ i- f( N
- ed.addButton('recentposts', {4 J" N. O6 X D) D: u
- title : 'Recent posts',
+ ]: U" [" L C3 e' j - image : url+'/recentpostsbutton.png',$ I4 z M! C1 T+ x) q3 f$ _3 M$ ?
- onclick : function() {
7 N1 I1 m+ V/ B3 _8 Y) N0 L - var posts = prompt("蝡賊", "1");9 H6 J+ ]* {/ N) q% O
- var text = prompt("隢憛怠亙銵冽憿", "唳蝡");
t' ?" p8 R3 H" ^6 n" s - ( I% j; G9 P6 Q" s. ~
- if (text != null && text != ''){
. `, J0 w c, n" B" V - if (posts != null && posts != '')/ W) U, T8 a& \5 X6 C
- ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');
0 F4 x8 T! o: ~' L - else
8 b" g a- p2 s) t - ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');5 `4 ^6 V; K0 p" [% r6 y; R6 {) E
- }8 l# Z4 K x. _8 b0 `. C
- else{* g# r8 k8 t7 K- P
- if (posts != null && posts != '')
+ X* R% _7 e- `/ T0 y: }1 E - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');
( ]' F/ T; W2 X - else
' N1 _; k. e3 I- G - ed.execCommand('mceInsertContent', false, '[recent-posts]');% p7 P8 w: O7 g5 f4 b) D4 ^/ O: X
- }
- G$ U( \6 t: ^* C - }$ l! B0 \# u' x/ v1 r
- });
2 N; y$ B& s6 @- g - },
( _0 N: H4 {5 w1 J: | - createControl : function(n, cm) {9 U9 G2 `' \1 d7 b; C
- return null;* V2 w' P6 n: K: |- k6 e; x9 _
- },% a) L% v+ |. p" K' {0 y4 o
- getInfo : function() {8 M% Q0 b2 G) i1 Z( d
- return {9 D! g" j ~5 E7 N% D* V6 Z e$ U
- longname : "Recent Posts",! H$ m4 y4 v @+ K! n# {5 o, X
- author : 'Specs',
% _9 O7 t& P6 g - authorurl : 'http://9iphp.com',! `5 J6 n1 i {; @; S Y
- infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',
9 D8 B6 ^# S: l7 Y+ Z: x: ? - version : "1.0"
$ D# @; g# s* u r$ D$ q - };1 g `# S4 k) J
- }
) P+ b2 n9 \. G' z3 I - });/ U8 \; {5 U3 G. T! p1 p
- tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);
0 _5 d4 m" K7 R - })();
銴鋆賭誨蝣 澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨 onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉 敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准 Y7 i4 c l' W4 O8 N. D0 |) k3 C8 q
4.2 閮餃TinyMCE隞
( j2 m- t: R9 \3 m9 o& [( _7 J0 r, r* R
嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉 蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩 - function register_button( $buttons ) {
2 X8 P. V5 b% t+ J9 A8 ? - array_push( $buttons, "|", "recentposts" );" B# t- @) g) T- |* x
- return $buttons;7 A G; r7 O% f- @% N; j( a
- }
銴鋆賭誨蝣 蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅7 A" _' ?" R5 D/ Y* J
- function add_plugin( $plugin_array ) {, I' m1 ^9 Q( x
- $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';
2 O. L* `2 I2 I9 W: p% Q - return $plugin_array;
, D- g+ v+ U3 a4 U( p - }
銴鋆賭誨蝣 銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁2 Z; M/ g9 l: C- {7 E" l
- function my_recent_posts_button() {
; U5 S+ K8 Q" c" D5 R- {! ` - if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {
/ E4 i! c* ]- a, t b- _ - return;$ F6 @2 b( Y6 r2 f& o# T! m2 z
- }
2 ? X7 T0 K M B4 b& W& F4 A* A -
1 J& \: c: |# ^: {) L/ } - if ( get_user_option('rich_editing') == 'true' ) {
" V- R) S/ A2 c - add_filter( 'mce_external_plugins', 'add_plugin' );
4 C+ l% `" h& u7 K1 z - add_filter( 'mce_buttons', 'register_button' );9 q, C. l {, r) ]' X0 Y- b# g- J
- }
@' j) w. j G$ b - }
銴鋆賭誨蝣 嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁" V. c( U1 [5 B5 v8 g6 w
敺賣詨曉訖ordpress憪賣- add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣 $ }; B2 }5 J& R+ e
4.3 冽+ `- U0 v- f0 h4 X9 K
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
7 O. w1 X$ n, U6 h# q; J 暺嚗敶箏閰望霈頛詨交蝡賊嚗
- Y# Y, @ ]# B( V1 \1 q" s c y 頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
" g* x# G" p$ I2 ^
貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
; m- p, h2 L/ N
唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆 |
|