WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)
' p3 W( \! S# e9 K: m2 H: O: x
9 O, ?2 ^6 d+ q1 s3 Lwordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐
3 `2 m7 d1 i R2 a; P" d隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具/ v5 `3 _0 S" J0 {
# K3 ?+ B5 r' j$ Y( g+ j
凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :. ?9 F5 R% p% f
甇文 , 臭誑閮剖銝訾批嗆蝡賊 :: B6 ? t- m' J. e3 s$ _( u. L
甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :6 t0 X6 N/ C: |3 Z v6 w
- [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣
. y$ u( r9 N9 Z6 c
% j; [4 U/ ^8 R. d, o3 v9 u1. 蝪∪桃凋誨蝣
) [" b5 D3 }* ~' J祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :4 h# F# m P- i* J! b) y. c
閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :
2 H# r9 U- i) G, t9 g- 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
- 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
- 閮餃賣賊蝯針ordpressaction銝7 J2 w' F' J2 q; y8 j2 x( Q( ^
* I5 m+ b% X% W1 @( \5 k祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准
3 B" T# L/ Z0 C% M* J7 Q1 b" o" U$ |1.1 撱箇餈賣% x$ Y0 P U/ G
嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡
. i( h/ [* L( f1 I* x# a- function recent_posts_function() {
2 w( p2 V1 S3 e( M% k- d. t - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));1 I" K S7 y+ V" w' X( D) t
- if (have_posts()) :
9 h; l7 s) a4 w9 _" O - while (have_posts()) : the_post();# M9 G) u4 K1 M( c
- $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';
* }0 @ E# K- R- y+ U# ] - endwhile;
' D/ m" _8 E, a' n' J( N - endif;& Q) c. f1 U2 H: B/ P& }
- wp_reset_query();
' ~5 H& d0 u% F( i4 f - return $return_string;
( u. E/ i8 l( ]- [$ P - }
銴鋆賭誨蝣 憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯: d& q% |" n+ U* f7 \% d
. e7 j- X( U( N& C4 d! G2 Z, w% |+ [
1.2 閮餃凋誨蝣
' q6 ~* s' @# K; d1 N$ R3 w曉剁閮幃ordpress賣豢臭凋誨蝣潘- function register_shortcodes(){2 Q6 g" {2 K9 X
- add_shortcode('recent-posts', 'recent_posts_function');
+ a; J$ W3 `, N7 [% i9 [7 t6 L/ d9 X$ ` - }
銴鋆賭誨蝣 嗅冽蝡批捆銝凋蝙函凋誨蝣4 P% l* E. [5 a2 l7 X$ A
撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴% l) `9 b0 E# U) l$ i; Z
8 `$ j6 s) B0 w" I+ ?, z6 x) s/ g* h! J2 e1 Y) ?* U$ \+ g3 `
1.3 Hook into WordPress9 p1 a3 P4 Q8 k7 H! u
6 v3 c2 }( E6 n9 A6 J) N箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准
' f$ { W# r1 L& x0 @" |3 b6 H- add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣
! _' r- U# n8 M+ [! s8 q O0 _1.4 皜祈岫凋誨蝣
8 Q% F1 j, {" U蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :2. 脤凋誨蝣 2.1 凋誨蝣澆 凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻 閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼 撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 : - function recent_posts_function($atts){
8 Y& H2 [! E5 c' z - extract(shortcode_atts(array(+ S$ L; J, `) r( n% S
- 'posts' => 1,( k9 W: V; G" S7 S: m
- ), $atts));
/ _1 g( e5 R3 C" y0 u$ K. a7 V5 e - 7 Y( h6 H1 D1 V
- $return_string = '<ul>';
( o V( c0 p, x) \1 `! r - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));( x' e. n; G1 _5 i
- if (have_posts()) :
Q- x# q9 e; m) l - while (have_posts()) : the_post();' r5 ^) h( w6 {# `# s" p1 u: |
- $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';4 T6 t# w7 z! N3 C7 T# L$ |
- endwhile;
2 O; [; Z( m' j* Y3 C - endif;5 _8 A- F" e) ?2 p6 J
- $return_string .= '</ul>';) n( V5 b7 M3 d% n$ w
- 4 x1 ]) f# f+ g0 l) K0 w* V' b3 n
- wp_reset_query();
8 L1 x2 T( M3 V, k# R* ^; N, Y - return $return_string;* X/ k& s. \0 V7 c7 n1 O
- }
銴鋆賭誨蝣 憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :" r% j1 m! I% x
2.2 凋誨蝣潔葉憓摰 臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗 - function recent_posts_function($atts, $content = null) {6 d3 l; A+ _& ~# Y' L( } i/ _
- extract(shortcode_atts(array(
) w' m. m. I3 m W* y - 'posts' => 1,
& L1 N3 W0 S. J - ), $atts));- e' ]9 `7 n3 f$ T
- % }- }, P; }+ o
- $return_string = '<h3>'.$content.'</h3>';' B, e* S" s+ X6 Y
- $return_string .= '<ul>';
- C8 a7 m% G- V: B7 Q5 S - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));% F9 t8 r1 j! N; r9 P
- if (have_posts()) :
* T9 k/ c/ d8 `( A' z: ^ - while (have_posts()) : the_post();$ r) a- }" t0 g3 @1 T2 V5 ~. m
- $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';, Q/ E7 K* `' r1 K
- endwhile;
2 N5 S3 P$ I0 D8 K! s3 V) O1 } - endif;, N6 B% @" m/ \) x9 h. f
- $return_string .= '</ul>';
& C/ B e( V' J: M' f/ F6 g0 Y -
- }1 K/ h' J8 c& D" r! f- \ - wp_reset_query();; b2 b/ E+ t# q
- return $return_string;
' ?0 A% B0 A/ K3 W: a5 _* _% r - }
銴鋆賭誨蝣 蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝- [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣 支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗
, o' m3 \ i) f# ]& ]- K% E& T3. 其遙雿唳寥*蝷箇凋誨蝣 3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣 閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛嚗典臭誑撠甇斤撘蝣潭曉典撌亙
( A+ ] V5 J& F臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 : - add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣 曉其閬靽格嫣遙雿嗡唳 , 瑟, 湧甈撠甇蝣箏圈*蝷
, M0 J: A+ w8 i" b璅嚗刻隢銝剖函凋誨蝣潘 - add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣 冽閬銝剖函凋誨蝣潘- add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣 4. TinyMCE蝺刻摩典凋誨蝣潭! e: ?; s7 ?: Z+ S
* K3 n. V- l- i, W# K& P* P1 g/ }嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗 - 箸迨撱箇銝 JS 瑼獢
- 閮餃JS隞
6 S7 |; ? P! {) s ; e2 c7 t# P C, K9 m1 N" d
4.1 JS隞嗡撱箇JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗 - (function() {
5 \1 l: n' O- c2 M8 A/ g - tinymce.create('tinymce.plugins.recentposts', {
# x1 A8 t8 U+ ^) O - init : function(ed, url) {
, k, L) j: n' _! [( U/ q% ]! c - ed.addButton('recentposts', {* B# y! e4 U9 e0 u! @0 v8 U8 @6 H
- title : 'Recent posts',
9 I* N5 Z; n5 t - image : url+'/recentpostsbutton.png',( T; G8 G$ @0 j' R
- onclick : function() {9 j8 V7 R- R: S. r: D
- var posts = prompt("蝡賊", "1");
, W9 d4 S7 Y6 | N9 J - var text = prompt("隢憛怠亙銵冽憿", "唳蝡");
6 q* ~: M, v3 u8 r - 9 {+ F! V/ ^. Q% [
- if (text != null && text != ''){
& B- _9 p0 U# F- ` - if (posts != null && posts != '')% ]; \, }- }2 r9 W
- ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');
, {) H3 B% k8 n - else
" R0 K9 g# {/ A1 B' D; e# ] - ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');% Z, Z4 E* y3 o. Z4 T' H
- }
. q( A' z: w5 w Z - else{
! g: u% f$ d/ y) a; H - if (posts != null && posts != '')
: O7 g& y2 G, O5 C, O- U3 b9 x2 e - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');
3 H+ m% l7 f/ y7 z5 r- p - else" B8 t5 D- X# Z% r% E
- ed.execCommand('mceInsertContent', false, '[recent-posts]');$ d+ j- }4 W3 v2 }, ]7 E0 m, U7 f
- }, D) R Y# C" m5 n+ A/ f2 M
- }
% C9 P$ o9 `2 f2 x7 p6 C+ | - });+ c) K3 x3 y3 V/ c' a9 w( ?+ x
- },8 z+ k [' i* _1 C8 k, _
- createControl : function(n, cm) {; _( V; a+ _7 n! F1 E
- return null;
- f/ c- d! d. w5 A' W - },
' t6 y4 s/ A6 d+ M1 X$ |3 y# L - getInfo : function() {5 i/ f$ H3 R3 P- } S7 ^
- return {, {7 G, P& J; H1 n8 U" R
- longname : "Recent Posts",9 C7 H7 e, I; f$ L" H
- author : 'Specs',8 t, |% x" i6 }+ G, {5 N! @
- authorurl : 'http://9iphp.com',1 C | W5 b5 v* ]
- infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',. e" t6 x# k: `8 _; {% @
- version : "1.0"
0 r4 a& o) W6 N8 p - };
9 z9 I7 X; a0 U - }- _! H7 V7 E$ `, n
- });4 C9 z1 o/ f7 m# t
- tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts); K; j3 j% e6 x$ S! x0 f. |
- })();
銴鋆賭誨蝣 澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨 onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉 敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准 : F7 P& y' {! O% K( J
4.2 閮餃TinyMCE隞2 P8 @- N# @" T- f2 V5 ^ D
" X- p% L1 H9 Y- r1 i
嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉 蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩 - function register_button( $buttons ) {
+ T9 N* s6 ~' w2 b$ {. m - array_push( $buttons, "|", "recentposts" );
1 s5 q4 K; ]0 L4 `2 e - return $buttons;6 x" _; B; T& D% k n7 O
- }
銴鋆賭誨蝣 蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅
' e. g1 M, S1 T- G1 a% E: U/ M- function add_plugin( $plugin_array ) {
) E. d3 o3 j" [8 i1 r* d0 I - $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';
0 L, P; h: Y2 M* _: s - return $plugin_array;% k$ w3 _# N0 Y$ K5 N
- }
銴鋆賭誨蝣 銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁
+ M& Q/ R Z4 p* s. @% i2 Q- function my_recent_posts_button() { , \" m6 E: j: C' G3 r/ ?
- if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {
0 g: u5 x: C- e* T6 O! H5 C - return;9 U: v8 G* k$ c/ {: V
- }' D2 }# U7 v0 j4 \4 M. ]& z
- & C/ d* z d- A* Y0 A" z }
- if ( get_user_option('rich_editing') == 'true' ) {8 G) ]" f' k; ]" `8 z
- add_filter( 'mce_external_plugins', 'add_plugin' );
1 x! I0 B$ D$ e2 S5 `( h - add_filter( 'mce_buttons', 'register_button' );
3 l; H7 X# `( `2 L! ]% A6 w& Y - }
; C* e% {. A0 `0 E, { - }
銴鋆賭誨蝣 嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁
" N2 c& S/ f1 w( s: t9 s敺賣詨曉訖ordpress憪賣- add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣 & |0 ?! L# W" K t$ [
4.3 冽
9 w+ I8 Z# i# D e7 A! I- U
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
( k1 d5 f- j0 y5 P. O9 C$ n9 n" W
暺嚗敶箏閰望霈頛詨交蝡賊嚗
* z, B& Q: S; I% f/ y" _9 P" x 頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
9 a+ N3 t5 j6 r0 L$ _, i 貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
" f6 B6 O( ~& u2 v/ v 唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆 |
|