WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)" l/ Q1 K& [" G$ o' y& C
: f2 r4 s5 Y+ L) ~
wordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐
) M$ E) E( ~$ i1 C) f( }# C; u4 g隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具
7 o4 Y/ i8 m/ p! A
& D9 G8 V" n, D4 k( Q凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :
$ c4 s" H* |7 K6 |8 @1 K甇文 , 臭誑閮剖銝訾批嗆蝡賊 :
/ Q6 c$ h* V* o( Y. f甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :! S' P7 I( j9 W$ E+ u- V
- [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣
: ?! G( U, N8 k( j! ]- [- a5 I
( K9 U9 R* K$ z* `7 i1. 蝪∪桃凋誨蝣- |6 \0 U2 n6 _7 I
祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :4 F) |( Q1 j+ d; Q' x5 a
閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :
# h! k5 O9 P! |1 G% {8 B- 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
- 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
- 閮餃賣賊蝯針ordpressaction銝
. }. M/ L- o& v
7 h) S0 W3 A- z/ z9 ]祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准/ E/ \9 x: L4 u
1.1 撱箇餈賣# [% d4 \0 M. R: L
嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡
8 Q* a3 v# d( b- function recent_posts_function() {
7 G$ n# T4 G' {, q0 [) m o - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));' e' w" a" G1 m! |
- if (have_posts()) :
) f( N; {# o# s0 u2 s6 Q- r7 x - while (have_posts()) : the_post();' m" [+ b- j( @0 \1 a
- $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';
8 E9 p E# u# Y: L7 _ - endwhile;( [2 X, ~$ c( b" M4 ` o
- endif;, ]) q8 G5 { \( O9 D7 a% F' Y
- wp_reset_query();
4 v, L6 x0 f% P1 e9 F - return $return_string;4 ?2 x3 F$ R1 g3 y6 n
- }
銴鋆賭誨蝣 憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯
) d; u( {1 ]8 f) s& j6 I: F K5 M7 u t. @7 H) P3 N
) F n$ T# A' n; l [2 C1.2 閮餃凋誨蝣
; ~1 u8 T# q' h) m# n0 E4 T曉剁閮幃ordpress賣豢臭凋誨蝣潘- function register_shortcodes(){
0 ~. s; R, W" f5 U - add_shortcode('recent-posts', 'recent_posts_function');
$ @& D" }6 |' {$ N8 L# }: Q4 G - }
銴鋆賭誨蝣 嗅冽蝡批捆銝凋蝙函凋誨蝣
! I( ~! d3 c! h8 o& z; ~撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴
: r3 `0 u6 O N$ _( H% Q( D/ A e7 }) K! {& `/ E7 C, z+ t; _3 l% g" N8 r* d
! w7 T$ U2 `6 H5 a, d
1.3 Hook into WordPress
, {) _( w2 J4 \: V8 A4 w3 U7 Y9 G( t! M$ w8 M6 C
箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准
8 g' g9 g9 S0 p4 ]; |* y- add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣 % X1 t. q1 }% P8 |' P7 w
1.4 皜祈岫凋誨蝣4 C. X1 b; U1 w3 G: J/ X2 K0 w
蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :2. 脤凋誨蝣 2.1 凋誨蝣澆 凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻 閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼 撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 : - function recent_posts_function($atts){" p8 ]5 |0 j( v8 s
- extract(shortcode_atts(array(
! H2 _ w3 T) n6 E4 q1 d9 e/ s! D' g - 'posts' => 1,* N* f; u$ G* u! |
- ), $atts));
+ U, u2 V5 O* d8 {/ V -
/ v9 w9 J6 \7 u) } - $return_string = '<ul>';! i6 _. M S+ D# o
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));+ n# e: v. N6 I6 b \5 p
- if (have_posts()) :7 @8 Q' F! _/ z
- while (have_posts()) : the_post();
- T: O" k5 Y3 y6 Z+ h, f. W3 G - $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';7 g1 d+ W: p( B% v7 t6 a [- w
- endwhile;
3 v3 Y1 f, K: V* J! E - endif;
# Z# R' \. h( t- e - $return_string .= '</ul>';" A7 I7 E* F/ o1 ]
-
- e3 V; T1 o2 w) L$ e. f - wp_reset_query();
) Z1 @2 `+ Y1 [! g: S- S% ~ - return $return_string;
- q& b3 h( N$ z) M- c - }
銴鋆賭誨蝣 憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :
# J5 u; L" q! g8 Q2.2 凋誨蝣潔葉憓摰
臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗 - function recent_posts_function($atts, $content = null) {
% S* B4 h! ^/ M; [8 X - extract(shortcode_atts(array(2 Y& `7 v, u) w+ S: R4 k
- 'posts' => 1,( }# b M' Y# W. o0 |
- ), $atts));
7 G4 O, l* m1 ^. i0 G! u, i - - _* E3 j$ Y2 k; H
- $return_string = '<h3>'.$content.'</h3>';
& ?; J% s5 ~# T; x% j - $return_string .= '<ul>';( v+ ]9 ~! C* `* S5 j& V: a
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
4 x4 u# V% X. U8 H O - if (have_posts()) :
" g( E- r+ d1 y - while (have_posts()) : the_post();
* L/ c6 E7 H9 W0 } - $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';" Y4 W2 F; L ^
- endwhile;
8 i2 N7 M' P! `3 d4 ^' x5 \! j - endif;9 w5 |" S( |- h$ u t5 ~
- $return_string .= '</ul>';
2 f0 t/ L; W" i' N! J* n+ P - " V* A }8 F& e/ K
- wp_reset_query();
# f9 B2 ]# f) v9 N, `1 ] - return $return_string;) Q5 e& `$ M& v/ z
- }
銴鋆賭誨蝣 蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝- [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣 支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗; Q h. h4 Z( E; X; R- |& |3 ?' U
3. 其遙雿唳寥*蝷箇凋誨蝣 3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣 閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛嚗典臭誑撠甇斤撘蝣潭曉典撌亙
9 y5 f' {0 V7 [2 E8 o p1 m* u臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 : - add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣 曉其閬靽格嫣遙雿嗡唳 , 瑟, 湧甈撠甇蝣箏圈*蝷
! o! ?5 D' U; B" m& S/ q璅嚗刻隢銝剖函凋誨蝣潘 - add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣 冽閬銝剖函凋誨蝣潘- add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣 4. TinyMCE蝺刻摩典凋誨蝣潭; f& B% h8 H+ S& z
/ i/ {7 ]1 Q4 R+ _/ j
嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗 - 箸迨撱箇銝 JS 瑼獢
- 閮餃JS隞
* A2 q) [% y" t ?; `. l! o/ d- _
3 d/ `* _' F" j$ f3 ^7 K" w8 l: s4.1 JS隞嗡撱箇JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗 - (function() {
( q- E: [% d' T5 i/ _* T - tinymce.create('tinymce.plugins.recentposts', {
. L* }2 k# I. g/ e! s - init : function(ed, url) {# g5 c f* _/ R2 f( F5 x+ ~
- ed.addButton('recentposts', {" X+ B, N! Q# S5 L! @8 b
- title : 'Recent posts',5 z$ h0 J1 |6 |; T
- image : url+'/recentpostsbutton.png',
4 A/ z" W: M5 R* t - onclick : function() {
' e7 l, B, |" [4 i - var posts = prompt("蝡賊", "1");& o' k( e. _+ d: S+ o
- var text = prompt("隢憛怠亙銵冽憿", "唳蝡");4 K1 @0 B/ y) i3 a
- 5 _0 P: a: D! g" z+ c# c
- if (text != null && text != ''){
3 E6 S& m% G3 p1 |9 g0 `, F - if (posts != null && posts != '')! X6 j: K) e9 ]# j! [
- ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');
1 C! T' Q3 r0 U E/ r: U/ G# Z( W - else
0 v' S! X F: g% T" y: _3 u - ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');: w, D3 E2 M9 H" W' }/ h
- } S$ g7 E1 M- T$ v
- else{
R, V! x) N% n+ `& C( J - if (posts != null && posts != ''), p) i8 K+ p9 A" J1 z2 U
- ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');# p' \, l0 I8 o
- else
9 i. p. h) t6 h) D. m - ed.execCommand('mceInsertContent', false, '[recent-posts]');
( @3 y: }' m8 E" s Y' U - }
' l' `( a, x- u4 e+ m - }4 L* h$ W* a' ^" }! {$ X/ J& A
- });) W5 e5 @2 u( a/ q3 g W6 M/ S
- },
% J0 Q- A8 f( J, r$ x: o - createControl : function(n, cm) {
l K" H7 M6 A1 q6 P- A" _ - return null;( c* K" |, m; s1 B3 M/ D* ?
- },
* _9 \/ |1 k' e5 v! t0 Z$ \# W - getInfo : function() {6 O* O) ~8 W9 S# z
- return {' H6 C6 V3 o% H5 ~& L8 ?) }
- longname : "Recent Posts",
- y6 K( M: Z/ }$ a; B+ | - author : 'Specs',( U, C7 e+ c# B0 Q7 r% N
- authorurl : 'http://9iphp.com',
4 {& O% U p" l6 J* G s - infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',3 W/ e$ p# t2 q3 ]1 g2 ~, A
- version : "1.0"
7 N! ?. T/ x) x D( @ - };
7 W I& @& z" |5 T+ P! d$ I - }
" C5 ^% ^7 K) W - });9 Q% j. ?# \' G4 W
- tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);
3 L% h7 r' |' H. f J - })();
銴鋆賭誨蝣 澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨 onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉 敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准 ' s3 v6 I! T' C1 a& }
4.2 閮餃TinyMCE隞5 Z! R& y+ w, B( ^
, r: V5 n1 u9 x. I2 j0 b% |+ R 嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉 蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩 - function register_button( $buttons ) {) C* G8 I3 B8 M2 a: c! X
- array_push( $buttons, "|", "recentposts" );
& U& o5 N% ^" Z8 B - return $buttons;
7 j( t) Z( n( y) T" v - }
銴鋆賭誨蝣 蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅
1 W$ \0 U1 P3 s' c. H z- function add_plugin( $plugin_array ) {
" `2 N! `% G2 ]' x! [; v n1 t - $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';3 Z9 B9 H1 t& C5 E
- return $plugin_array;
( R j" G3 @0 ^& B6 T - }
銴鋆賭誨蝣 銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁
% R4 O* _; q% R, @- function my_recent_posts_button() { $ X3 X( V, p% ], Y N t8 O# T+ Y
- if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {
* d0 S9 |6 E- G. _ - return;
# V. g3 {0 \- v( `/ Q6 I - }+ N2 f- ]0 n' Q% r8 Y! n
- 6 a4 }6 y! A9 [: w
- if ( get_user_option('rich_editing') == 'true' ) {# G" W8 w! K6 g
- add_filter( 'mce_external_plugins', 'add_plugin' );
: t K+ n: F0 }/ Y; P0 W' U - add_filter( 'mce_buttons', 'register_button' );, ^2 r( s, t0 e* \* \2 g) P1 u
- } & p' ?" t' M$ q9 R* I
- }
銴鋆賭誨蝣 嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁- l8 C( R8 a) S$ |- s+ m; p) h A9 b
敺賣詨曉訖ordpress憪賣- add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣
1 e% J4 ?% ^: [, Q: @9 o4.3 冽( L; I( M# E! N% }# f
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
7 i. O. j3 n2 ?9 D1 [; e
暺嚗敶箏閰望霈頛詨交蝡賊嚗
9 U# y( r, ~& z" p+ J5 S: P4 ^: _
頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
1 p5 K4 t9 R. F 貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
; U3 R+ `" V. M& `+ b. D 唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆 |
|