WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)
t* X+ z& |( m; [! _7 ]
( z& E4 `8 n) }3 s( U2 \5 |wordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐
8 L, k n' W% Z3 ]隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具! r, K! Q8 t! a0 [
( Z [9 S7 d1 L' L3 [9 U% P* b
凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :8 H8 m) U2 |3 j3 I3 N8 H" X
甇文 , 臭誑閮剖銝訾批嗆蝡賊 :+ P8 N+ S* o1 D6 t+ h9 s. f
甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :
; p9 M4 z; @; L8 b& ?- [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣 ( \+ \/ R5 U- B+ b
. @5 W7 a7 O) P7 [% @1. 蝪∪桃凋誨蝣
' w" o; n- c( |: p6 y8 F- ^0 ?祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :) D$ N& l/ w0 Z1 m
閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :
8 j) D) D2 K' }& [( H% v- 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
- 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
- 閮餃賣賊蝯針ordpressaction銝' _3 c' D4 D/ N0 t' h" i
8 v: V6 g% A% j" g8 ~- S祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准
0 b) z; o2 I3 _% S* e1.1 撱箇餈賣; n: f* x" a9 q- r" n+ U
嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡
4 ?' l( j% a: d. \0 R# {! o z- function recent_posts_function() {. ]$ F7 u8 a- S2 Z; O& ]2 l5 E9 }
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));; }2 e. U: P: {% N* @0 ]: ^
- if (have_posts()) :
8 U3 h7 ?* B$ H% ^9 G$ ]9 | - while (have_posts()) : the_post();
+ t- D7 Z5 a% |( @( S( \ - $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';
& E" A/ G9 F. @5 C3 N, c* r+ ~ - endwhile;
j, A E4 p0 m5 d) W - endif; j O. A% e* q+ {0 T
- wp_reset_query();; |) m+ A; Q, k
- return $return_string;4 S! i& Q+ T2 i) \" t$ F1 Y' v
- }
銴鋆賭誨蝣 憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯$ D+ J3 V: h+ b& B) P
9 v' V: Q; T+ ~5 v" K$ H! x2 w4 Y9 [8 W Z# H
1.2 閮餃凋誨蝣 k# a4 Y# ], B! i; t7 [
曉剁閮幃ordpress賣豢臭凋誨蝣潘- function register_shortcodes(){
5 _2 v& B( v& I& s# a% X6 j# x - add_shortcode('recent-posts', 'recent_posts_function');+ Q6 h$ ~7 A" e' r
- }
銴鋆賭誨蝣 嗅冽蝡批捆銝凋蝙函凋誨蝣
$ Z' g& P1 n7 N! I' O$ F' U+ o撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴; P6 d! ?) |# b, F$ W
" z c7 E! Z" K4 _4 p4 ^6 S
* x% S4 o# ~ h0 j5 v- ?! Q1.3 Hook into WordPress
0 \. V% v! B8 m" k) p* p* t- V1 G
" T8 z! x' u' V7 g8 w+ T箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准7 K$ Z' T9 A7 i. r# m& U
- add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣
4 J- B1 |0 E7 ]4 j) q0 J1.4 皜祈岫凋誨蝣
% N6 H6 S0 Y9 @; a6 {5 A8 ]蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :2. 脤凋誨蝣 2.1 凋誨蝣澆 凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻 閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼 撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 : - function recent_posts_function($atts){
* n( o$ N1 n A! A - extract(shortcode_atts(array(
5 @4 ]2 R. K, r5 j3 y - 'posts' => 1,
4 ~4 A$ D" w5 u% ?) l" D - ), $atts));
7 C; D% H# ?# R; { Y$ U+ ~: X# n -
4 l& P4 e0 y# _3 E - $return_string = '<ul>';
7 H; \5 Q5 l+ @# h$ i: H$ s# }, X - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
7 Q1 {! H |0 I - if (have_posts()) :
8 O, L6 R r- c4 T - while (have_posts()) : the_post();( R' D3 o$ l, q3 E
- $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
, [6 a) t0 H. S/ T% T: G - endwhile;
1 \0 i, X8 D+ r& Z1 t0 N - endif;
1 g* i$ ^' l7 J A# [/ X( ~: Q) L9 _4 u - $return_string .= '</ul>';
( h4 H9 M$ k g" R2 G! Y" o% g - 9 A' I: ^9 z) l J% F
- wp_reset_query();$ j. H* D% f/ N5 T2 V
- return $return_string;
, V3 N. H3 [: L/ ~) o% F1 v - }
銴鋆賭誨蝣 憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :6 I' y& ^. z1 ?0 w' _
2.2 凋誨蝣潔葉憓摰 臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗 - function recent_posts_function($atts, $content = null) {$ _2 `/ O2 K/ E! c, n- d
- extract(shortcode_atts(array(
% A0 p0 a4 U- E, [! w! d8 I8 [ - 'posts' => 1,
# T3 q+ R# g& V2 s- n - ), $atts));- _" P' [0 V0 X% d6 W' ^
- : K* x0 V1 l; }
- $return_string = '<h3>'.$content.'</h3>';3 ^) D ^4 V, u
- $return_string .= '<ul>';' U6 F/ X( M# W. ?& X" z! l* j! H
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));3 t& Q( g! N# n6 V; z" o, `
- if (have_posts()) :$ J& h8 u6 |( T0 V; p/ p
- while (have_posts()) : the_post(); W9 s7 V- X! g
- $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';: e- q0 }% |: I. W1 o
- endwhile;
( U& V2 ?& s, {0 J, h7 l% M5 k - endif;, L x* d3 F6 U" Z. f& T
- $return_string .= '</ul>';6 P8 [. ?. ^4 m6 F
-
m6 P% u9 c. M$ w: R: B( G0 K - wp_reset_query();6 S$ n& e* |! a ^2 K) K) c' X
- return $return_string;; a' a8 ~9 f, X+ k% h- Z8 w3 G8 @
- }
銴鋆賭誨蝣 蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝- [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣 支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗
8 P( }% J3 K& C Y/ ^- X+ P3. 其遙雿唳寥*蝷箇凋誨蝣 3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣 閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛嚗典臭誑撠甇斤撘蝣潭曉典撌亙
" E/ |8 ~( [/ Q, I- I+ }* X2 h& `& s臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 : - add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣 曉其閬靽格嫣遙雿嗡唳 , 瑟, 湧甈撠甇蝣箏圈*蝷 x8 O9 H' I) E% X
璅嚗刻隢銝剖函凋誨蝣潘 - add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣 冽閬銝剖函凋誨蝣潘- add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣 4. TinyMCE蝺刻摩典凋誨蝣潭
9 V- S% \6 x9 s, B% @3 \# Y4 Y3 c1 f& S, h3 m# P
嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗 - 箸迨撱箇銝 JS 瑼獢
- 閮餃JS隞
% ^9 z" q2 K: D% r4 f2 z: h2 e
3 H) I) t; [$ e5 c( s" L5 ~) D4.1 JS隞嗡撱箇JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗 - (function() {
& b4 d1 {6 d0 f5 ^0 A - tinymce.create('tinymce.plugins.recentposts', {/ @3 r% b; l+ [( \
- init : function(ed, url) {7 q* r( V- [. v) W
- ed.addButton('recentposts', {+ p$ u$ p: }2 f
- title : 'Recent posts',7 [+ B6 o( _! @4 x% [7 z) _3 M( Y
- image : url+'/recentpostsbutton.png',
+ C! c( p5 [' d; P/ }' r - onclick : function() {) B* `; A7 }& W( t7 x
- var posts = prompt("蝡賊", "1");
( K2 s8 d; g% I* a3 h - var text = prompt("隢憛怠亙銵冽憿", "唳蝡");
7 E' b6 r+ ]* n$ H1 a. s - 0 q* q! d! [( `) b1 |. i. v5 W
- if (text != null && text != ''){
! {! B$ A2 U. y l - if (posts != null && posts != '')
$ \+ ~/ N& ~" J9 C, R3 ] - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');
& ^3 M; p# R k% p7 ]7 a, y2 j - else
0 ^+ k; H, Y2 x - ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');
% c9 K+ q! t; o3 J - }
. v+ H- F# J1 K, R$ u) e - else{( B- n7 @! o7 H# T6 @# Z; ^
- if (posts != null && posts != '')
, S, E: y+ ]- Q7 x& I' @ - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');
0 T4 ?. d: ]5 Y: @5 B) X - else: y+ V8 r& b! I
- ed.execCommand('mceInsertContent', false, '[recent-posts]');
5 u! K# w6 [$ K: v0 o( G8 `1 G - }
5 n9 R1 S1 T. _' y8 p - }
$ W: m" ]1 w& B7 K4 ? - });0 _$ ?% X1 q, V% ?
- },/ n/ W) G6 g2 a6 t3 _
- createControl : function(n, cm) {
. C3 g+ h0 c* W8 T; G - return null;
( V: S. D* A% a) p1 p6 ~- S* w9 d - },
& k8 o* D. K/ t8 M0 u - getInfo : function() {0 j. O- w! B% u1 b5 Z9 A5 p
- return {
( b! S; a& t3 P - longname : "Recent Posts",& S* W, n+ P5 K9 T
- author : 'Specs',8 }9 v* t: a7 \4 d1 d
- authorurl : 'http://9iphp.com',
e7 z$ v' |% J2 @. `4 u# { m! X - infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',0 W. ]/ w& K7 U# f+ N9 [
- version : "1.0"
4 m; G/ v7 W4 w) d& S - };: f7 b, Q* y) p' X7 Z
- }. r! ~6 Q& m" ]) m! y2 y0 W
- });
3 A! J& J+ i- n7 `0 I - tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);% G; x# Q# @6 A6 g3 e% g( h
- })();
銴鋆賭誨蝣 澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨 onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉 敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准
# D. G! d' v2 b% v
4.2 閮餃TinyMCE隞
% U; a( H7 a- B% V0 A
' u w0 I3 w* u0 I1 E Q1 Q5 b 嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉 蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩 - function register_button( $buttons ) {0 d6 Z2 I* o3 r1 F; g4 r
- array_push( $buttons, "|", "recentposts" );
9 @% d8 T$ J% a' B3 b& l - return $buttons;7 x1 \1 {8 _$ q) F/ _ }
- }
銴鋆賭誨蝣 蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅% k# w, Y( K; N5 I/ p7 ]8 [- s
- function add_plugin( $plugin_array ) {
6 E+ t2 f- @7 n. j - $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';
# T+ R3 h% M" m5 [* W8 d0 w, }% R - return $plugin_array;
' _! ?! V+ p+ w - }
銴鋆賭誨蝣 銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁
+ {- G \- R. U% t: u8 E4 y- function my_recent_posts_button() {
# Z+ W* y! [, S' j) @ - if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {+ L" r$ r0 d+ K/ p4 i
- return;
+ G, ^% v' ?( A& B" m - }. u& `; i0 q* p, w* d* T4 c5 ~
- , g+ M8 K% ~- q
- if ( get_user_option('rich_editing') == 'true' ) {0 m v7 q/ l& g7 B
- add_filter( 'mce_external_plugins', 'add_plugin' );9 W; E6 H( r8 s5 A: \1 K8 `" W7 _
- add_filter( 'mce_buttons', 'register_button' );2 c3 {8 F: R" z
- }
2 W, i) a8 H* c7 C, K2 ~% k - }
銴鋆賭誨蝣 嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁
3 c7 b& e c5 d1 P; B敺賣詨曉訖ordpress憪賣- add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣 & v8 A4 K7 C' n2 O7 p
4.3 冽
6 e* B/ Q, j# j$ `* f# p
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
- z- [- R; m7 |8 R* e# \4 I W
暺嚗敶箏閰望霈頛詨交蝡賊嚗
4 ?) w$ Q2 q6 i" F' ^
頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
( b4 `- T+ k$ j* N 貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
% L- L4 ^" R; T 唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆 |
|