WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)
' ^ m* V" A& k, F. n1 l
' }& j0 F) c* o, Dwordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐
( G1 G' u: k( o( L隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具
3 Q4 [0 K0 w. Y
* W N. Y8 I- l; s# t凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :
0 v6 N! @8 k* F/ I甇文 , 臭誑閮剖銝訾批嗆蝡賊 :
4 R; ^% T3 o a1 Z" E4 X甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :' K& _* l; I% R0 ~3 U) K2 E
- [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣
1 m0 X( }! U) q3 Y- k; y% T
% {6 ?2 H5 K, S. o4 W1. 蝪∪桃凋誨蝣, h1 v4 X* V) A0 |* a9 c' `4 }
祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :
# k0 P% Z+ k( s2 b9 A3 p+ m( E閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :3 J, ?; J! T6 x; Q! _8 T; A
- 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
- 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
- 閮餃賣賊蝯針ordpressaction銝0 A! U) g5 B( o5 j
; ]* N- W/ j% n( {. O# z _5 |+ Z祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准$ s; e8 e; H6 H/ V1 c- E- J3 R
1.1 撱箇餈賣4 \ O7 l! i' s' X! i1 M
嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡1 N( |, B, Y* t' L
- function recent_posts_function() {( L2 I, Y; B$ \! \
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));
- L9 ?9 ^( ^, G' g9 { - if (have_posts()) :
% M2 C6 t9 ?# m$ e - while (have_posts()) : the_post();
4 y$ ^: g, S- L - $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';) s" Q( ]* l6 J* J% m9 G. v0 a
- endwhile;6 u" r% O( j$ {" f3 q5 x' q# U
- endif;
, G1 K S! I" J3 W - wp_reset_query();8 X! c1 V2 ^% T
- return $return_string;
* U! N) z8 j+ Z+ B9 _; T" r* ~ - }
銴鋆賭誨蝣 憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯
4 ?% N* k2 U* ~! \7 _" e7 w! g3 M* i# f6 V4 O8 l
$ a* N$ s- c' g; P5 {1.2 閮餃凋誨蝣
. |% z% y4 [! b曉剁閮幃ordpress賣豢臭凋誨蝣潘- function register_shortcodes(){
, G$ c3 H2 S& M5 d% I. d0 t - add_shortcode('recent-posts', 'recent_posts_function');: Q' Q4 B0 U- ~& b. {# f7 e9 H% P/ |
- }
銴鋆賭誨蝣 嗅冽蝡批捆銝凋蝙函凋誨蝣
; P6 ?: x' [0 i7 J撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴
$ `: g3 @$ {: M# F& T
% J' E' U+ h/ ?* p0 H1 l8 N0 ~1 Y0 ]% k" ] D: N" L' Y% h2 l
1.3 Hook into WordPress
. R) U; }9 K6 z( Y3 J3 ?
& J2 g3 u" m1 `箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准
2 {' a L# P2 [) w9 y- add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣
+ V0 @: A) f" O3 i \7 } |1.4 皜祈岫凋誨蝣
2 ?' h, y% q8 L) i/ l, f7 |# n蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :2. 脤凋誨蝣 2.1 凋誨蝣澆 凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻 閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼 撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 : - function recent_posts_function($atts){; c6 t1 S; o. u: G2 q
- extract(shortcode_atts(array(
$ B" c& A8 Q5 b8 u+ @ - 'posts' => 1,! I' D6 V# q+ K( x
- ), $atts));
$ e" ?! x: @: B3 c& l7 J: C -
1 M: L( ~2 j" z- A - $return_string = '<ul>';$ i+ |9 u% m) G' o( @. b4 i x+ d
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));9 O. A0 I4 f4 e/ I; J
- if (have_posts()) :
& O$ ?; d* h7 k& ^ - while (have_posts()) : the_post();# H2 {' @; c0 s; F- e0 o
- $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
3 @2 V9 G1 R5 F" F - endwhile;: _+ C4 v* b8 Q* c. ?9 v7 T
- endif;
. t& J" c7 K+ V" L C5 l' @, ~) r% O - $return_string .= '</ul>';9 N C: J9 [$ R3 X$ k
- . S1 w* K% B5 j' ~9 G0 e
- wp_reset_query();
2 L/ Y( O) m M! S - return $return_string;+ g- S F5 D$ t1 c2 @% d
- }
銴鋆賭誨蝣 憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :
3 }# U. Q8 g' p2.2 凋誨蝣潔葉憓摰
臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗 - function recent_posts_function($atts, $content = null) {; P, d2 b/ Q9 l4 D6 r A; }, m; j2 t* @
- extract(shortcode_atts(array(
: W" d7 A( {0 w0 I+ K: Q - 'posts' => 1,
* q0 b% k" K8 e9 a! `; D - ), $atts));8 K+ T/ f/ ?" ^ q, k/ L4 L1 r! F
-
3 s6 J# s* F" T - $return_string = '<h3>'.$content.'</h3>';
) s4 b9 J# g# ^4 f& ]: z: W1 q - $return_string .= '<ul>';
' i' b( `3 h9 j - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
1 O2 P* `' }8 c2 N) M) M2 Q - if (have_posts()) :
1 V! I9 F! i. V! N% J - while (have_posts()) : the_post();
5 b9 h$ q& ?; }3 v' R - $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
* |' ~) F t1 C0 M- r4 h - endwhile;
2 U3 q# s& U! k+ X R' T, o - endif;% c8 v- e# ~- b# f$ {1 _$ s9 w
- $return_string .= '</ul>';
" G) x$ E+ L" [: n9 @3 c - 2 Z( ]7 U. U9 ^* _3 \, t/ T
- wp_reset_query();
" G) u5 v7 v& \1 N. O+ c - return $return_string;
' P& ~3 r% n' x* j4 E" W% n - }
銴鋆賭誨蝣 蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝- [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣 支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗
1 @3 ~ Q1 C2 f) V3. 其遙雿唳寥*蝷箇凋誨蝣 3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣 閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛嚗典臭誑撠甇斤撘蝣潭曉典撌亙 % K; K* r L- R A7 u: d$ q9 }( c
臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 : - add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣 曉其閬靽格嫣遙雿嗡唳 , 瑟, 湧甈撠甇蝣箏圈*蝷 3 R1 o/ `5 j3 k) E, b1 l
璅嚗刻隢銝剖函凋誨蝣潘 - add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣 冽閬銝剖函凋誨蝣潘- add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣 4. TinyMCE蝺刻摩典凋誨蝣潭7 m, n+ y4 v5 d" o/ F7 V, i
/ m$ y( F) G/ e w
嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗 - 箸迨撱箇銝 JS 瑼獢
- 閮餃JS隞 M* _ @" A% K3 B( l1 `- H3 I
% `) G* i; G' I) t, R) ?6 I" v
4.1 JS隞嗡撱箇JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗 - (function() {
, _0 l& z' V* @0 l) ?/ E8 h - tinymce.create('tinymce.plugins.recentposts', {, I3 t/ Q& g1 l; D
- init : function(ed, url) {
; y" ?" Y8 q- S8 n# E - ed.addButton('recentposts', {
' G1 Q$ i7 E9 s0 u N - title : 'Recent posts',
4 u- }( N6 h; T8 }+ ?: Q* ?' F - image : url+'/recentpostsbutton.png',' S. H: L6 W# S+ y. d" q
- onclick : function() {
; D* T- b: J/ M5 U, t - var posts = prompt("蝡賊", "1");7 f. Y* P. R1 C( }5 S
- var text = prompt("隢憛怠亙銵冽憿", "唳蝡");
7 V5 T \4 N9 w, {' {- a - 8 r6 P7 H; W) p" v% l& p
- if (text != null && text != ''){
" D+ L7 K; d" K; j1 I, ^- g6 ~ - if (posts != null && posts != '')
& H: G9 D6 \9 I+ q7 I! @/ I - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');; D' R( i$ w+ ]8 H
- else
X* X& U1 K6 I$ `! A( g - ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');" q; s7 ^( f7 i0 {+ D
- }
! x) {; U: I& U, M" b+ ^* v" A& `: ~ - else{
- Z( d5 k: |+ k" y - if (posts != null && posts != '')
9 _* G; H* }5 j% ^5 f+ U - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');9 k7 V2 K2 e1 r0 ]* k
- else
" n4 \3 `- F$ t& t6 K2 l - ed.execCommand('mceInsertContent', false, '[recent-posts]');, W3 Q8 Y( l2 u4 L. L; e" F
- }
W* h+ L* \+ x$ G, M - }
( Q* \$ {1 C4 L4 _, s8 |" y - });2 h% C2 p- q# W( F" G9 u7 k" \( [
- },9 c+ Q# _ i7 B9 m4 u- E8 c
- createControl : function(n, cm) {
* p$ q |3 e$ i+ E* a4 Z - return null;0 I0 ]' ~/ m0 s1 s Y, V
- },
: V5 T/ ^/ `2 E5 {# k2 Z' T - getInfo : function() {
; c; M# J% r6 j, k6 G; o: g( p - return {2 f X) {/ w3 x) E
- longname : "Recent Posts",7 ]# q, O: i9 x' ]
- author : 'Specs',
$ Z, B! @# v) G* |2 p& @" c) N - authorurl : 'http://9iphp.com',0 a7 M+ i$ X0 P+ m( u
- infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',9 G! r5 S* g# C9 o
- version : "1.0"
7 l s, ^1 o+ c6 `8 U7 }' A - };& W! m7 e! ]9 L9 [" x
- }0 t" |% a5 d+ z+ d
- });- R7 l' x2 c6 c8 ^
- tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);/ T5 ^/ I% \: M2 l4 Y& M( x5 y
- })();
銴鋆賭誨蝣 澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨 onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉 敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准 & `3 E1 L; B. S; n0 j
4.2 閮餃TinyMCE隞% A( H$ p q) D4 e4 [
. {9 f* `+ n( B" e" V 嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉 蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩 - function register_button( $buttons ) {
! K4 [) K, C$ I7 U - array_push( $buttons, "|", "recentposts" );( v6 t' J1 D9 t( \) Y/ I
- return $buttons;' |' c0 D3 O6 _9 q o, h" y8 V( {
- }
銴鋆賭誨蝣 蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅) D+ |1 w" F* R% v' p
- function add_plugin( $plugin_array ) {1 z5 e: G* y8 V6 |' x* C
- $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';
4 L; e3 s3 {1 J) M' N2 a - return $plugin_array;7 W$ H. @! ^8 w* w7 g, W
- }
銴鋆賭誨蝣 銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁, x) M7 F7 }; ?# \# i- n
- function my_recent_posts_button() {
( Y. Y1 ]- V3 W& y" s4 N4 J - if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {
) n _; Y9 e/ }4 i { - return;
/ U; r- t, S( r8 ^ - }
; H# A/ P6 H: G6 S* i -
7 R0 M+ J2 j+ I$ @" y* o7 `; Y - if ( get_user_option('rich_editing') == 'true' ) {4 Q5 u2 A' y% ] T6 |0 ]5 D& p! B
- add_filter( 'mce_external_plugins', 'add_plugin' );
7 W+ n, O2 l$ ^+ W2 Z6 r1 { - add_filter( 'mce_buttons', 'register_button' );4 e! V1 Q" a3 p. y
- }
- }# c4 W5 S) j - }
銴鋆賭誨蝣 嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁( V# X$ E: v% s0 r M8 R
敺賣詨曉訖ordpress憪賣- add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣
& F" x3 M1 l! b% H4.3 冽
$ d' ?/ K' P* E1 f
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
' V- N R' `. R# U+ \. B7 K; r 暺嚗敶箏閰望霈頛詨交蝡賊嚗
. b1 t' \6 t: P; U% j
頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
( a% I) i3 d) G& i3 V- j/ I& ]
貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
0 H- [" y; _* \9 P1 Y 唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆 |
|