WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)
3 y$ _3 }* \( N+ v& w
& W9 |5 a- n0 e Y; Iwordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐1 ~, a% g: S. N* a
隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具
- F- x! B- _' t! I9 m! B' w/ M& H p$ f, j( p F$ o0 c4 b4 v
凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :. Q8 H- L* F1 D" O
甇文 , 臭誑閮剖銝訾批嗆蝡賊 :
) z7 @% A0 Q7 p甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :) `4 L& W7 T$ B- U) k
- [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣 ) o4 r% k t! ]4 L
' P' B1 l7 J9 [1. 蝪∪桃凋誨蝣
; a" N* s, P$ N3 }. \- z祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :
! b$ O2 l7 o: E" d閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :7 O+ G: R* h) G8 J
- 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
- 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
- 閮餃賣賊蝯針ordpressaction銝
1 n/ z; ?' v6 ~1 d9 F! k 9 _, D% S$ E# E9 Y& V; I/ c
祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准9 G; I& }* }7 S1 X& B! s
1.1 撱箇餈賣 w( o7 _2 N+ a& Y* S8 j# d) O
嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡
( {* P2 A; r0 ~9 b+ k" v/ Y: x8 L- function recent_posts_function() {$ w9 ~0 p' h4 x: z
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));9 l8 M% F8 N8 X1 _. o
- if (have_posts()) :
( R, H8 c o+ ^/ L0 Z1 A' ] - while (have_posts()) : the_post(); H4 z$ _: J" T# x) b a7 W
- $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';8 ]+ y8 w! K9 u/ q q$ `4 n
- endwhile;
?( W; R* U- s" E - endif;
& i( S7 V8 T/ O# i( F+ v - wp_reset_query();& C! W0 l4 ]9 c o4 R
- return $return_string;7 H9 ]4 u9 J& T% _2 X; q
- }
銴鋆賭誨蝣 憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯
6 n, ^: l u: x+ ^* H* u6 f
7 ^' u) c, U9 i% c* Q' P3 L+ H A
1.2 閮餃凋誨蝣
+ w& J4 t" b1 L- X曉剁閮幃ordpress賣豢臭凋誨蝣潘- function register_shortcodes(){
' { n7 J$ ?) J, {$ T X - add_shortcode('recent-posts', 'recent_posts_function');
% S0 w+ }/ N8 t1 E - }
銴鋆賭誨蝣 嗅冽蝡批捆銝凋蝙函凋誨蝣( `2 S' o- Q$ u; d" A
撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴
2 a9 u+ U O. V/ @. N! f& y( ], B
) V2 ?4 k9 h4 S0 Y5 Z2 B1.3 Hook into WordPress4 @. I; f( q, h9 a; ?4 t9 O
* A+ V- I' q: w" L1 Q* o箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准
) b. K% ^& u; a8 F8 w5 v' \) }2 z5 d: `- add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣 6 O+ @. u7 C, w4 [
1.4 皜祈岫凋誨蝣
1 \3 ~8 F m, t' ~6 Q蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :2. 脤凋誨蝣 2.1 凋誨蝣澆 凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻 閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼 撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 : - function recent_posts_function($atts){
3 o2 f( p; t" I# K) F - extract(shortcode_atts(array(
- ]: j8 z5 p% b4 g( k4 L - 'posts' => 1,
4 \# Z' _/ U* ]/ E5 I - ), $atts));
% r/ k0 b9 Y5 y$ [$ i - : O: y/ y5 _6 F5 N2 M$ r
- $return_string = '<ul>';7 `+ L* g+ p+ V3 j+ I! C0 t4 g
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
6 \; J3 U% c9 ]2 h/ l) I8 v$ V - if (have_posts()) :
! C3 l4 s# o* n! L6 p, q - while (have_posts()) : the_post();
5 f+ a* Y( V: Q, | - $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
4 n- _/ E$ F( l- L% r" X) P - endwhile;
, k/ ^3 f9 ]% H+ j2 V4 h - endif;( R2 S: L/ Z& N, G
- $return_string .= '</ul>';
. n' ?3 \, k$ ^! |) j6 o9 K -
( S& Q- m7 E' O# I# R - wp_reset_query();+ Z5 T+ i3 T3 q2 q2 r% \ n
- return $return_string;/ N) V+ A# w7 E! p& X7 a7 E! n
- }
銴鋆賭誨蝣 憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :
0 ?2 \4 V' q; ?! `2.2 凋誨蝣潔葉憓摰
臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗 - function recent_posts_function($atts, $content = null) {. ?6 k# U3 s4 y' z7 g
- extract(shortcode_atts(array(
5 |9 z' N8 M5 a - 'posts' => 1,1 o' w8 T' N+ Z4 }7 Z: n$ v( `
- ), $atts));/ C- X* O Q- z$ t# x5 c3 {2 w
-
- P8 J$ ^, s1 B# ` - $return_string = '<h3>'.$content.'</h3>'; a7 k6 c# [* {( h, j: p4 J
- $return_string .= '<ul>';$ [- ~* |: `6 j3 h8 k: O5 A' [0 @# Z
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));1 I( d! k: z4 [' C: o5 i5 l
- if (have_posts()) :* y' x, d; {7 Y Y7 ?+ W
- while (have_posts()) : the_post();
) _8 g0 Y; B+ U: D - $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';, w. l% N! a! e9 p
- endwhile;% S& N% R) h# N2 @% ^ |8 T9 H
- endif;
* o) }3 j( R% P, q' C% z - $return_string .= '</ul>';
1 I+ w0 f# p4 d7 h& q# G' C, L - 0 |& z3 q. |* V0 u+ ]& s# ~
- wp_reset_query();$ A: K' ]; G3 \$ U% @4 H& o. y$ r
- return $return_string;
! ^1 ?' ?4 _# v3 G( f& }3 t - }
銴鋆賭誨蝣 蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝- [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣 支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗/ ^; @6 {7 @& f. H7 U9 v
3. 其遙雿唳寥*蝷箇凋誨蝣 3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣 閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛嚗典臭誑撠甇斤撘蝣潭曉典撌亙
- S( n% P) V6 t' A臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 : - add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣 曉其閬靽格嫣遙雿嗡唳 , 瑟, 湧甈撠甇蝣箏圈*蝷 - a4 I, H8 f. {$ Y. ` t3 v3 y' }: E
璅嚗刻隢銝剖函凋誨蝣潘 - add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣 冽閬銝剖函凋誨蝣潘- add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣 4. TinyMCE蝺刻摩典凋誨蝣潭
) q+ j) z8 ~1 T
" ~( a) l1 S& D3 j" G+ w) _嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗 - 箸迨撱箇銝 JS 瑼獢
- 閮餃JS隞+ A$ m* |9 ?. J4 c6 \ {
5 A6 f6 u$ j& n! b- z1 y( a/ _
4.1 JS隞嗡撱箇JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗 - (function() { X9 m h% d7 I2 Q
- tinymce.create('tinymce.plugins.recentposts', {
& H O) z' n$ |2 C - init : function(ed, url) {
9 G. `9 R+ A t) r% x' ~ - ed.addButton('recentposts', {
0 _+ q: p7 ?( r& m2 d - title : 'Recent posts',% X8 ~3 I. b: {+ Z! \
- image : url+'/recentpostsbutton.png',0 o1 f# M( N1 y
- onclick : function() {
+ U& J+ h# B( R& Y( M; D - var posts = prompt("蝡賊", "1");' R" f0 M( v0 B. M* f6 m+ V
- var text = prompt("隢憛怠亙銵冽憿", "唳蝡");
- o S, x2 k: N# {- Q& Y - 5 s/ B5 v- a( @. f9 {1 k
- if (text != null && text != ''){
6 F* \6 a/ v4 Q% P- Z! M - if (posts != null && posts != '')
* F# f j# J) S& M - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');( ~7 S* E+ G4 j' g
- else
, e* h. @0 h) E9 X9 z0 w, b' \ v - ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');) D- k v/ w% u% i
- }5 K# k1 z6 I" Y+ E9 q
- else{2 u" w. V' Z$ A% r7 Y1 S4 l9 e6 p" b
- if (posts != null && posts != '')1 S/ ^; n. I- ^
- ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');" Y8 W. s$ {0 z& s- U
- else' C$ P, v9 z+ | Q( |2 M$ Q/ b, t
- ed.execCommand('mceInsertContent', false, '[recent-posts]');
/ x- X `& {* @9 W - }3 R4 b. P3 `$ ?7 D& L* D3 ]
- }
& B/ @% g3 L$ }# t q3 m+ D( z) } - });
9 S3 ^0 P5 q: n' B9 D# g5 @7 D, v - },
% I% X. K9 d7 c4 X; F8 ]4 I4 r - createControl : function(n, cm) {- P5 ]3 I1 `* O6 B9 L6 ~5 M: Q$ B8 c
- return null;0 ?) F& { d# n4 {
- },
$ v8 e$ K( A0 s2 C+ i" J - getInfo : function() {( o t7 n: Q: c
- return {
' Y' Z9 f2 {, \. @& K, S7 X - longname : "Recent Posts",; P8 g* m6 }; o( R
- author : 'Specs',. b: x6 s1 t; l4 Q/ }
- authorurl : 'http://9iphp.com',4 @6 P: N( E9 V
- infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',( s1 I$ ?$ A) _! D' B& ^* S6 m2 z3 `4 V
- version : "1.0"
/ S4 J6 ^6 G; j# R0 k) M9 C4 ~ - };
7 L$ N# U7 Q# U/ k$ D V - }
8 G4 F+ Z/ p% f% S - });, J a( E0 t" E" `: u, e3 ]8 [& ]
- tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);1 L' J* l: a- b4 V" }4 T* n
- })();
銴鋆賭誨蝣 澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨 onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉 敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准
3 b3 j% y' B8 M3 A1 U
4.2 閮餃TinyMCE隞
r" }2 M5 G# w1 n# X3 Y$ }: b
嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉 蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩 - function register_button( $buttons ) {% S1 O9 E% ]5 Y4 S
- array_push( $buttons, "|", "recentposts" );
5 I6 f* ]8 I' O! ?2 T2 a8 w" o6 `% [5 s - return $buttons;
* D; B% z$ Q* y/ x - }
銴鋆賭誨蝣 蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅! M3 m$ C( v" S- }" c3 H6 e! z5 A( |
- function add_plugin( $plugin_array ) {
$ ?0 z- m; D4 l4 m3 q( F# J - $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';3 }0 E+ [7 M/ N5 h5 i; i* p
- return $plugin_array;
7 M+ n: S1 T+ F' x8 N: H1 ^' Q - }
銴鋆賭誨蝣 銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁
s6 ]8 s0 m9 f+ T# k% u& k) @; o- function my_recent_posts_button() {
6 S5 C- y; | @: O& ~ - if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {
2 `$ H$ X* V# r% g. k, x - return;( r/ E; Z) T! Z- b1 G+ k. j m
- }
' F4 S+ C$ I1 m* h4 t' A$ C$ Q - 1 p' B, f) L8 g4 w8 G# l" q8 b
- if ( get_user_option('rich_editing') == 'true' ) {% ]1 I" r/ o+ R6 k( C
- add_filter( 'mce_external_plugins', 'add_plugin' );& C2 f0 r" v: R: w% f a
- add_filter( 'mce_buttons', 'register_button' );* O9 q( C( y! ~, W" Q
- }
H! M/ ~5 |# x, }" V# N; N7 v - }
銴鋆賭誨蝣 嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁; B. i& E! a1 g' r" W, [
敺賣詨曉訖ordpress憪賣- add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣
5 i2 P( T% P9 D# S$ w5 c4 R4.3 冽
' W& t- c# k5 S9 q3 w+ ^
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
9 v% {3 i( ]- }1 g" @! w 暺嚗敶箏閰望霈頛詨交蝡賊嚗
+ i3 z+ Z1 h9 f y
頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
& N- y; o- U7 O5 u. K4 s( O1 S' f
貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
' a. g. G( a! I6 q 唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆 |
|