WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)
5 C5 k8 X) _2 S w' [
6 v- T. ^& v% |0 q4 z" C( Fwordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐+ c& Y9 W$ M5 R$ H
隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具/ T3 J8 M1 k" s9 G& K
# C+ ?$ k5 W$ I( }! s9 c# J凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :
$ g4 a) r' A+ N# |, C甇文 , 臭誑閮剖銝訾批嗆蝡賊 :" ?1 ^- v5 g# {" b" Q1 d7 x
甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :
% |& H6 F4 s4 p8 F: e! i* r4 s- [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣
* [: [, h$ _ L8 v% o
; G% y5 c! }2 s6 p( _7 o! Q: Q1. 蝪∪桃凋誨蝣/ V3 |8 D9 z# ?5 _- E, s( B9 m
祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :. ~$ I& f9 S# z
閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :
k' T8 G8 u3 t2 Q. T- 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
- 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
- 閮餃賣賊蝯針ordpressaction銝' b5 k& }+ s6 e, |$ R6 x6 W
- I3 {% h7 |; H+ i1 o2 b祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准
6 I6 U1 s( b ]3 [; V+ L. ~2 b1.1 撱箇餈賣5 H" D! @8 \/ V+ v2 C5 F( ]2 m
嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡
/ C! h0 `6 C4 l' g) O$ S0 ^- function recent_posts_function() {
& a. ~6 b; m# Y- \ - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));6 y/ g. G- A5 w
- if (have_posts()) :
6 Z: R+ F) v+ U: q - while (have_posts()) : the_post();
( {0 V$ S5 B* q) o+ z+ D2 U7 _% w - $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';
! M% r( l: ?( z - endwhile;
* U( f" z8 Y6 M - endif;; y) M9 [& P$ l, `5 ]
- wp_reset_query();
b- k/ x. E9 B% ]7 _7 @ - return $return_string;7 @" `/ f0 U+ e4 r' c& b( j
- }
銴鋆賭誨蝣 憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯$ C: N/ k+ D, m( x" Z
* _$ H" [+ M5 p" L7 H! `
" D% f; ^: K, x1.2 閮餃凋誨蝣
8 f( Y5 u. K" M) S# t曉剁閮幃ordpress賣豢臭凋誨蝣潘- function register_shortcodes(){
$ g. N& P3 j. a6 d+ m - add_shortcode('recent-posts', 'recent_posts_function');- v( ^! y i1 V/ a, ?
- }
銴鋆賭誨蝣 嗅冽蝡批捆銝凋蝙函凋誨蝣
# M; J5 k" \0 O0 ]7 Z; w1 o% y撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴/ c, o& d* ~1 u- Z2 k& _7 @
( n5 K" S: b$ W# |' c; T6 q/ A+ [+ e
1.3 Hook into WordPress
! V) q& z2 {6 C: N9 `. W3 v* l- ~& L! P% ~1 ?( h9 \
箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准3 ?( |$ A- J0 `0 w( x$ ~8 N! A
- add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣
5 r* [3 m$ x3 ^& H1.4 皜祈岫凋誨蝣
6 l/ n8 I3 |6 {5 `7 ?3 t# F" k) e& U$ q蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :2. 脤凋誨蝣 2.1 凋誨蝣澆 凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻 閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼 撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 : - function recent_posts_function($atts){
; e7 Z( M% h) W- F% k - extract(shortcode_atts(array(
* i6 t) c: ~( t" t9 A5 x5 W - 'posts' => 1,
2 z% r, G0 k V+ y8 ~# h' w" } - ), $atts));
% ^/ T* V$ q7 ~- Z - " G/ z' K, W( G6 R; I# o
- $return_string = '<ul>';* Z! z; `% d# X9 Z1 N+ O1 W
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
* H( m7 K* ~2 |. h, m) y, G - if (have_posts()) :
+ K$ n9 T5 g% s - while (have_posts()) : the_post();
. G3 E+ ~4 x) U# ? - $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';' ]) |: i: L3 L* k/ W! k
- endwhile;. n6 a7 s/ J2 l6 p
- endif;
9 R* y5 Z" Z2 _( E5 f - $return_string .= '</ul>';
1 u# {8 @+ Y8 w7 n4 Z4 E( } - 2 \* q( \$ \! N m$ G* ^
- wp_reset_query();
+ V) u' _& c F1 r - return $return_string;
+ ` v7 B# L) c( P9 b4 w& J C - }
銴鋆賭誨蝣 憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :
V2 m0 p* R: ~) @2.2 凋誨蝣潔葉憓摰
臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗 - function recent_posts_function($atts, $content = null) {8 e$ S |1 R7 D
- extract(shortcode_atts(array(3 K: d3 U) k% u8 `; V9 K
- 'posts' => 1,. n& Y4 H9 l5 u: ?8 P2 d9 j2 J
- ), $atts));
" a1 i; e1 f) Y$ Z9 X& j, K3 | -
5 g" S& d3 @) T* u! T x - $return_string = '<h3>'.$content.'</h3>';
. W3 P; }1 B, Y( Q" K - $return_string .= '<ul>';
; A4 {- S& M4 a! D4 T6 Y/ j - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));! o, D3 Z+ c2 n6 c
- if (have_posts()) :
; U1 c" w# s4 a - while (have_posts()) : the_post();
: y/ Y9 m9 C' f0 F: \, w) o. c - $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
: G* D% o# n: Y( S: W# w, o$ a - endwhile;
: g, Y, ^( ]/ A0 ~% z* ?% { - endif;
" B. W( J. E! N% p$ | - $return_string .= '</ul>';; F- \/ i4 F+ Z& y! B `+ v$ V; D
-
" r- L \( ]! A, b, r6 n* w - wp_reset_query();
5 Q6 T* p' S9 @/ G: c - return $return_string;1 j+ t$ I2 p0 y9 [+ d8 P2 N6 y1 r. c
- }
銴鋆賭誨蝣 蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝- [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣 支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗
& Y8 i$ Y5 m/ z3. 其遙雿唳寥*蝷箇凋誨蝣 3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣 閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛嚗典臭誑撠甇斤撘蝣潭曉典撌亙 6 Z4 m" P+ u0 @$ L: X. j
臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 : - add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣 曉其閬靽格嫣遙雿嗡唳 , 瑟, 湧甈撠甇蝣箏圈*蝷 3 J# F# ?5 O1 {$ [: [. t
璅嚗刻隢銝剖函凋誨蝣潘 - add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣 冽閬銝剖函凋誨蝣潘- add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣 4. TinyMCE蝺刻摩典凋誨蝣潭
/ D" L- Q" E% G# Y9 A" ^! S X
8 f" C3 p/ V8 S5 t; H+ F) Q- V嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗 - 箸迨撱箇銝 JS 瑼獢
- 閮餃JS隞0 f8 B/ {- d! I9 f: i' q
/ k3 h. A# u5 R4.1 JS隞嗡撱箇JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗 - (function() {! s9 z% j/ ?, q
- tinymce.create('tinymce.plugins.recentposts', {
* Q! L/ g) d# N' Z) N - init : function(ed, url) {
, {: i H4 _) \( {8 m. {& S - ed.addButton('recentposts', {
$ l V, v1 n7 D( L7 K - title : 'Recent posts',
$ {; X8 P* U% f' d5 H, | M( _ - image : url+'/recentpostsbutton.png',
: }: g' `% q7 ?1 S - onclick : function() {/ Z8 l9 Q u5 x/ q9 s0 R8 R
- var posts = prompt("蝡賊", "1");
# z" h5 E5 ~; r" [; g5 y - var text = prompt("隢憛怠亙銵冽憿", "唳蝡");
. _- {5 \# [# |! j: g - " e& `, d: t- G* H" |7 Q& W$ s
- if (text != null && text != ''){
2 @1 c, g; q# w+ L - if (posts != null && posts != '')
+ {6 q$ q# P% m: d4 M - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');( {) s, o/ t2 Y* ~2 Z# U/ Q4 P
- else: D" Z) N& v7 ^& U( d6 U
- ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');
2 V+ m: K; l% D" \4 f - }" v) P! t' O2 N; @% l$ t
- else{, y& a2 Y* J4 r3 |! J4 l
- if (posts != null && posts != '')
' N# u8 f3 G3 u$ { - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');
4 ^% l7 Z( N0 o - else( L+ G) m. m' l U; m+ Y+ _
- ed.execCommand('mceInsertContent', false, '[recent-posts]');
4 N$ ^. ^3 q4 v$ N+ S. d - }
) P- c$ C; e0 A) z: Z6 _) X5 D" u - }: u/ o% Q# t% \# O& j- J+ I
- }); s( n5 s# g6 }# p: x
- },
2 {# T+ z% N. B1 A" Q - createControl : function(n, cm) {
( s& u$ q" o2 n0 @ - return null;
% G' Y' ^2 M7 ~' E- K1 M: E - },3 z# V' n& r, C7 X' p4 P! {0 |9 z
- getInfo : function() {0 f) ~3 V) Z! Y9 y/ [
- return {
, b$ b0 q2 t: C - longname : "Recent Posts",' F' u+ }( N( P7 D8 g
- author : 'Specs',; N$ y/ ~# K" I) f, F- \& `$ R! j
- authorurl : 'http://9iphp.com',6 y5 k# G# G G+ q
- infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',3 V* }8 i$ U, }% Y" K" [. z
- version : "1.0"
% m& u( U% b) Y6 [+ T - };
; s( J# @* p8 w" ^( A" Q. ` - }
, @7 _0 v; A5 i8 @( ^ - });
, K7 G5 ~" l( T - tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);
$ l' K5 O) B2 j* W - })();
銴鋆賭誨蝣 澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨 onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉 敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准 ; h* {7 ? K& z- U- t
4.2 閮餃TinyMCE隞2 q: s \9 t5 [% c# Y
' Y C1 K0 w* Y# C 嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉 蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩 - function register_button( $buttons ) {
1 ~7 @& W, t4 r. s9 e - array_push( $buttons, "|", "recentposts" );
5 |7 I5 [/ w) m0 [ - return $buttons;
6 c3 @0 b0 A" c+ r! Q/ [% f/ _ - }
銴鋆賭誨蝣 蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅
; w& X7 H7 B# K/ f n- function add_plugin( $plugin_array ) {0 i" a! R+ p% V9 d
- $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';5 Y) f1 A$ X4 x/ z
- return $plugin_array;
! n1 ]& ]& }* E0 ~& G" H9 Q - }
銴鋆賭誨蝣 銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁 s! w0 g$ T v' {. [$ Z
- function my_recent_posts_button() {
* f& }' T2 f) h A/ m1 Y% E - if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {
/ ^4 W) n- {7 @ - return;
E) B" {0 v/ @2 }) H! N2 z - }
+ L) s' [, b* p9 E - ) w5 l2 y) ?" X2 P( a
- if ( get_user_option('rich_editing') == 'true' ) {5 Y& V* b' G5 K9 c0 m
- add_filter( 'mce_external_plugins', 'add_plugin' );
5 m8 T/ ]8 C) v2 s! Z' C; B- K - add_filter( 'mce_buttons', 'register_button' );" V! Y( q$ @$ W
- }
4 a0 G& {+ R1 d( m/ T - }
銴鋆賭誨蝣 嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁# T' G+ |- a# |. y
敺賣詨曉訖ordpress憪賣- add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣 + w8 {8 D* r3 R; S' h1 v
4.3 冽
, ]$ ~* x9 i% v
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
s/ O0 t$ y' h" f: ~: z( F 暺嚗敶箏閰望霈頛詨交蝡賊嚗
4 v4 r7 _! J5 p- k; t5 H2 o6 T 頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
' O9 S( a& g; v& Y2 d3 o/ R* i/ [
貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
& B% z b$ I W. X. q 唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆 |
|