WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)
! s! ^7 M+ }4 d/ b1 ^# B7 O5 a+ d. e6 c
wordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐% F6 N- a: B' U; X+ L5 Y3 Q8 }
隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具
% \( Q& {9 O6 c" h9 P9 }0 P/ o' u$ j5 N4 Y$ V: U, H
凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :9 V) I& b: p* @. Y: Y
甇文 , 臭誑閮剖銝訾批嗆蝡賊 :& k9 q8 @! c( [8 h, y
甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :
3 \/ T0 A% a/ t2 ]2 F5 j- [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣
. \% |3 H$ Y" v, h2 F
+ E# |4 e6 u% B' B1 n6 ^' s1. 蝪∪桃凋誨蝣/ k' z) c% J. T) M, B' Z x! m
祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :
2 H3 J. r7 d3 \* M閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :
, p4 _( ]# {' q$ Q: b4 ?" B- 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
- 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
- 閮餃賣賊蝯針ordpressaction銝
3 [7 w/ P& x) g' T6 G. V
' J5 L1 Z% N9 a5 a* N4 C) a( Y祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准
. I, b& X) i9 D1.1 撱箇餈賣1 w/ j' c3 W; q t+ U0 r& H
嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡
6 E3 D: Y: {9 P; T* [( T- function recent_posts_function() {
; l4 a0 I* m+ j: Z: E$ ] - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));. t; z2 Y3 I; D7 G
- if (have_posts()) :8 {" e0 F+ L9 `( C
- while (have_posts()) : the_post();/ E9 V' V1 \" Q0 y
- $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';+ [( `2 ^$ G+ ?0 d. D3 ~9 \2 T: S
- endwhile;- W3 {# I P. r3 K/ {& H: n, f( E! n
- endif;7 s( a0 _9 z. _8 r! o$ [
- wp_reset_query();
: N! {9 u" A% F H4 b - return $return_string;: D6 D$ W, e+ y0 }5 k9 S
- }
銴鋆賭誨蝣 憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯
* q. w: `( b$ `: X7 X6 P+ X, X; r" [0 C/ J8 A1 Q
+ G! f7 T$ _. y+ U* Z
1.2 閮餃凋誨蝣/ o6 w+ T C* ?, u3 Z% p. H
曉剁閮幃ordpress賣豢臭凋誨蝣潘- function register_shortcodes(){* |/ J* b1 P$ W5 r) o! s
- add_shortcode('recent-posts', 'recent_posts_function');
8 K* T: h' `, w# G* { - }
銴鋆賭誨蝣 嗅冽蝡批捆銝凋蝙函凋誨蝣
9 ?1 y2 y+ i$ U; B撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴
: \4 O6 }: I. h. Q+ s$ U; b+ @( F, b8 |, K) @9 B* v' D6 }9 c% q5 T9 _
7 b" Z+ l6 s4 D# r# U- f3 h' ^6 I1.3 Hook into WordPress- s7 p7 Q- v$ l5 b. U: L
1 m9 |, O! Y) m
箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准9 O1 S& t7 s& W# y: {4 m4 [9 w6 X: w
- add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣
) K8 ~& d- W% m# F) K+ c1.4 皜祈岫凋誨蝣& ~( X3 s9 C' T, @# n0 J
蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :2. 脤凋誨蝣 2.1 凋誨蝣澆 凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻 閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼 撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 : - function recent_posts_function($atts){5 F% y) l: P- t3 ?( \& c
- extract(shortcode_atts(array(
9 A+ T4 @! @# o - 'posts' => 1,
0 W: d2 U D! R8 k% {. m! T - ), $atts));
0 d: B) Y6 `8 C5 L; N - 2 R! e# i: l1 A/ [
- $return_string = '<ul>';1 l: Y- G4 _& q
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
& @1 n$ y- q; @ - if (have_posts()) :# r/ Y# ]. n1 S( X# s* \
- while (have_posts()) : the_post();
, ^6 `3 j" z% G: f3 ^0 |3 S - $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
& I0 A: M0 z+ I3 q% q - endwhile;
6 n9 O) o$ N7 @, G" z; L% B - endif;
7 R! _3 h' v! p: g, q. K3 N - $return_string .= '</ul>';
- g! j* {' h# M1 E - ; X" T& f4 [+ H' I
- wp_reset_query();6 i; h; s: h4 v- X9 |/ |
- return $return_string;
9 m# `1 R/ _4 F+ z - }
銴鋆賭誨蝣 憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :% t5 I! Z2 M; d! {3 d4 l0 l3 t
2.2 凋誨蝣潔葉憓摰 臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗 - function recent_posts_function($atts, $content = null) {# Q) n! v' V; T6 x' i
- extract(shortcode_atts(array(
% t& M2 l1 c( w, ?- h8 A - 'posts' => 1," |6 M6 X& @, G/ k. U3 f
- ), $atts));
, v% ^. F2 m# `- O3 ~3 T- X -
6 O* h% @6 y/ a4 L - $return_string = '<h3>'.$content.'</h3>';
9 X# U4 s: ]7 I2 ?" z* B+ W1 {( Z - $return_string .= '<ul>';
& a9 `$ C! Q9 S$ y( M: i3 [' f - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));! D5 W+ i l8 I8 R* R
- if (have_posts()) :
) B# s5 d. }5 M7 I N - while (have_posts()) : the_post();
/ z [" h0 Q3 z- U - $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';$ U" t0 Y5 _5 P, k. z
- endwhile;
; v+ u I8 V- S2 U - endif;+ P G$ t* W w( x- b& _9 {
- $return_string .= '</ul>';! Q: u8 g8 Q2 n8 L: F
- 9 ]5 L! w8 I4 D8 f
- wp_reset_query();: M7 m$ q- m! r; m
- return $return_string;
0 u' a p- e* F) _ - }
銴鋆賭誨蝣 蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝- [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣 支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗
- H" x1 |* X- N- I3 K0 ?9 c3. 其遙雿唳寥*蝷箇凋誨蝣 3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣 閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛嚗典臭誑撠甇斤撘蝣潭曉典撌亙 ' }& O$ ~2 t! X1 F% v+ z
臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 : - add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣 曉其閬靽格嫣遙雿嗡唳 , 瑟, 湧甈撠甇蝣箏圈*蝷
! |2 o5 |6 z8 W# D! i6 N, s璅嚗刻隢銝剖函凋誨蝣潘 - add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣 冽閬銝剖函凋誨蝣潘- add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣 4. TinyMCE蝺刻摩典凋誨蝣潭
7 k: _% V' m5 k' I) S- h
0 W$ a# R7 v; }/ V嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗 - 箸迨撱箇銝 JS 瑼獢
- 閮餃JS隞
. e( F+ _6 ?1 c @7 Z4 M" E+ ~. I 6 W/ A8 _6 p1 d/ q' Q
4.1 JS隞嗡撱箇JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗 - (function() {
3 l1 b* V; @" x - tinymce.create('tinymce.plugins.recentposts', {, }- r+ ]8 U0 @! B
- init : function(ed, url) {
/ Z! \( r% I) F( n - ed.addButton('recentposts', {* l7 v& B4 Y0 Z' G( o6 A
- title : 'Recent posts',* }. n9 _) D& K+ S0 H+ h
- image : url+'/recentpostsbutton.png',
, b4 u- w" ~" s" C - onclick : function() {+ T, E% E, }, l$ q
- var posts = prompt("蝡賊", "1");
! J$ b9 l" G* U3 Z6 W9 ?( H0 J - var text = prompt("隢憛怠亙銵冽憿", "唳蝡");
S0 l, U# @+ c - 3 `; {9 | i. _- R5 k
- if (text != null && text != ''){+ b$ m6 c5 D6 f* k$ R
- if (posts != null && posts != '')" e4 e" \; i' G8 M! { V
- ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');
* [4 C, G5 V! l - else) L% f5 R4 C; z0 D& S
- ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]'); d0 o, ^ s! R* I. {/ Y
- }, {4 ]' X# R6 r9 j1 K
- else{* m0 \0 z' r# o$ ~) o9 K# i3 ]3 E( Y1 a8 r
- if (posts != null && posts != '')
& R' G- g- a! P. L( D9 ? - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');
' h5 z2 h, d( T" S% e: X - else5 M8 o! e& x, W% o/ e
- ed.execCommand('mceInsertContent', false, '[recent-posts]');
/ d4 k) t7 l6 l' \) ^ - }
; _& `7 E3 @ J3 ~ - }( k( X1 x5 G6 t- }0 y& `
- });
: d9 a! {9 X$ V; M, \0 B1 R - },9 m1 v I# J5 _) T8 E
- createControl : function(n, cm) {
# Q( Y, R+ ?( g3 t9 z6 M - return null;
. f5 Z: e1 \: U4 G5 I - },
! A# T. c) ~: f8 A6 T+ _/ A% ? - getInfo : function() {$ [7 K. o1 N" [5 m! ?# h
- return {2 J+ d# P" T! c9 |/ O
- longname : "Recent Posts",* Q$ k. T3 M# N$ }
- author : 'Specs',6 w! `- U+ Z/ K! O: ]3 |/ Z
- authorurl : 'http://9iphp.com',9 E1 i. T2 ~) P3 n
- infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',5 S& `* |0 L# v/ l: i+ T! Q
- version : "1.0". Q$ y6 u. S: S: s6 I3 D
- };, [$ x# S4 V+ V
- }
! P/ D+ J7 q# o- i; B0 o - });
2 V* c& w6 X5 }- r' i - tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);! M+ E0 u* H- ^8 {: e( P* _
- })();
銴鋆賭誨蝣 澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨 onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉 敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准 ) J$ ~" M& N6 L
4.2 閮餃TinyMCE隞
. i- r9 g% M' C' h- Y E- d
% V: N$ G; r8 K# [ 嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉 蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩 - function register_button( $buttons ) {
8 h* e9 [, V8 T$ K - array_push( $buttons, "|", "recentposts" );
; Y m) y* a7 T7 ` - return $buttons;3 K, I- c( W& l
- }
銴鋆賭誨蝣 蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅1 i5 ~/ {; c; A: M5 t ?
- function add_plugin( $plugin_array ) {% q" X' p3 d4 F6 t% K0 L
- $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';
. n* x- r1 G1 M" {, d. M7 A - return $plugin_array;! ?5 X' U* V: E/ t1 \$ B* O+ t
- }
銴鋆賭誨蝣 銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁
4 J5 V! V% e; P6 t- \2 @- function my_recent_posts_button() { ) K# u- c1 k: A/ _
- if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {) E | u7 v8 q3 r) N
- return;; ^4 x% Z. Z/ P
- }
3 w, `2 Z5 d$ j0 Q$ ~: R -
) @& S1 r. ?3 I* j* S. o- o9 C - if ( get_user_option('rich_editing') == 'true' ) {8 ~! }. }3 a [6 _& A( T
- add_filter( 'mce_external_plugins', 'add_plugin' );
3 P+ v9 D9 H) N; P. H - add_filter( 'mce_buttons', 'register_button' );: w9 r# ^; q; Q4 t5 V. k) o
- }
5 c) I8 e! H& P# I: u0 s l - }
銴鋆賭誨蝣 嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁
/ J( N; [& U7 P/ c敺賣詨曉訖ordpress憪賣- add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣
9 w# D3 U, [: r( q8 E4.3 冽
/ f8 v+ B4 h/ o8 m# \
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
5 x. F1 V- p, H3 C4 A 暺嚗敶箏閰望霈頛詨交蝡賊嚗
. p5 ^6 U) Z% G. M: @
頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
; f9 S2 A( ~- Y7 v 貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
7 [) B5 ]1 M+ e% r7 f. I: q. n' @
唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆 |
|