WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)
8 b1 e- A) ~8 \. Z, {* h6 r% o/ U) H, I. p1 G2 \7 `/ p1 y
wordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐. o. ^3 E" n$ S: h4 g3 n/ q
隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具7 z! v% u# h" \5 B, f3 ^
5 F5 U: }$ @* g8 z. y
凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :# X5 K+ c- J/ N; Z9 k
甇文 , 臭誑閮剖銝訾批嗆蝡賊 :
; l: ?6 E# r6 Y9 \- n- F4 h甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :9 l- x/ |9 S. x, r* b* ~
- [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣
) H# P! G$ }0 `) I9 {4 M4 h& l
0 j2 h O9 k, w4 J3 C1. 蝪∪桃凋誨蝣+ M* Z. @2 W" y6 I* A3 r
祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :0 N5 |# P" ]9 R' C7 o o0 }
閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :1 S- a# r1 p7 k9 e9 \7 y
- 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
- 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
- 閮餃賣賊蝯針ordpressaction銝 _* z0 J; N. u7 O9 ^4 n" ^
3 u' p1 C5 }' L7 o3 e3 X1 f
祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准4 t8 J3 K( _* q' J+ c
1.1 撱箇餈賣
' S1 P% N" g' ?5 m/ j6 S; l4 Q嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡
0 D$ a, g% W8 [6 b" y- function recent_posts_function() {4 T; }3 }' D0 ]- d4 L1 W4 b
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));! s! o- ^3 c, ?) j$ O" L+ n. @
- if (have_posts()) :
4 z8 a p1 S$ o# c7 \2 k: @1 _ - while (have_posts()) : the_post();3 ^# F3 P: }# ~6 Y$ d6 @
- $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';( k" G9 a. a3 k9 e* Q
- endwhile;
; s0 C; m3 A- E1 c# F D8 f' c - endif;
4 I9 b8 {/ h: X# `! ]! r. V* h - wp_reset_query();; z% I( r/ g( D! l, ]9 Q
- return $return_string;
2 g4 h2 J$ G; _ - }
銴鋆賭誨蝣 憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯7 l/ X8 t o" ~ ` ]
. J8 D S2 J$ H7 T- M* M U
+ b: e) E Z1 g. ^. L4 f* |
1.2 閮餃凋誨蝣
4 o' `: [' e, a0 n6 w. Q1 t& o曉剁閮幃ordpress賣豢臭凋誨蝣潘- function register_shortcodes(){% _( G3 ?( m+ y3 m, m3 B9 P: z
- add_shortcode('recent-posts', 'recent_posts_function');
3 K) t9 q/ v% P C9 n+ F3 ^ - }
銴鋆賭誨蝣 嗅冽蝡批捆銝凋蝙函凋誨蝣" W- O& \' c/ @1 T5 s
撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴
& v$ K9 E* a7 C! ^0 S
7 b# U5 y, l9 D; h$ j9 X6 l7 G
) E- L2 U m' k- I+ J8 @/ W3 l1.3 Hook into WordPress& C! @: g2 Q6 a0 d1 y2 w# l
- \" L2 b! T& f! N箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准! H8 h$ N0 |2 l& n1 s
- add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣 3 h1 j5 I3 B- w2 _8 {2 t
1.4 皜祈岫凋誨蝣5 Z1 J& T" z! P* u* f1 R
蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :2. 脤凋誨蝣 2.1 凋誨蝣澆 凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻 閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼 撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 : - function recent_posts_function($atts){
( l2 s ]- `! e* q7 \6 S& | - extract(shortcode_atts(array(
, t3 E1 k$ r) u9 `0 \; Y - 'posts' => 1,3 ?' {6 ?- K* g$ ~, _
- ), $atts)); K# V4 J: G2 k
-
5 L; L z& P4 M5 L' b; o( u: X - $return_string = '<ul>';
0 |8 ]/ E k9 E" v( ~: `, a - query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
4 J8 O' e# A# _9 [8 a - if (have_posts()) :
3 A/ c3 ]4 N, h$ q - while (have_posts()) : the_post();
1 |8 B* }' Y2 v5 y% Q: G/ c6 Y. G& O - $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
) w2 g2 E' d9 @% J j/ v& o - endwhile;. Y- ?/ f$ l" m1 }
- endif;
9 o" N$ B. e/ D" A- y$ a - $return_string .= '</ul>';
% m( F; n1 {- e0 { - ! `3 p9 {1 e+ {1 ?/ t
- wp_reset_query();
7 T7 H" N6 A, R# c0 N - return $return_string;5 y$ q _9 O4 C' x2 U/ g+ O
- }
銴鋆賭誨蝣 憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :# u9 {& @" D7 A4 g% \2 z
2.2 凋誨蝣潔葉憓摰 臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗 - function recent_posts_function($atts, $content = null) {, C+ A" M1 M- p, q* u# }$ E' Z3 d
- extract(shortcode_atts(array(# h+ f1 j* ]' a, C' R( s
- 'posts' => 1,
+ L! y/ U; C/ i - ), $atts));
1 h+ `. D3 A7 Y( ? - , B) t+ n# g; ~ c6 i
- $return_string = '<h3>'.$content.'</h3>';
! q7 t/ }6 c5 e* i8 d - $return_string .= '<ul>'; e R5 {; u+ j* R$ J
- query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
9 B) y' N$ y* o5 ^ w; m5 ?6 K - if (have_posts()) :& n; i! X% g9 J
- while (have_posts()) : the_post();/ h# g6 }8 e6 p" r5 Y$ e1 `. G8 V7 p
- $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';; _' X) z* w0 K) T1 o1 S
- endwhile;
# t3 f) b2 u3 G. t: p' b - endif;3 f7 j$ d7 h+ P8 c I( R2 w
- $return_string .= '</ul>';- {- S6 m0 L$ C4 M
-
( @) p2 P1 x0 G q" }& T - wp_reset_query();# c/ U1 s0 `# x& B; _0 @" g
- return $return_string;
/ I4 y+ Z" P8 g A& m1 D - }
銴鋆賭誨蝣 蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝- [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣 支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗
6 t1 L) f9 r2 u: r) {. t7 R3. 其遙雿唳寥*蝷箇凋誨蝣 3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣 閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛嚗典臭誑撠甇斤撘蝣潭曉典撌亙 1 a- L6 \8 U3 S; K; m
臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 : - add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣 曉其閬靽格嫣遙雿嗡唳 , 瑟, 湧甈撠甇蝣箏圈*蝷
- B. m' w5 N) c- O+ v5 A璅嚗刻隢銝剖函凋誨蝣潘 - add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣 冽閬銝剖函凋誨蝣潘- add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣 4. TinyMCE蝺刻摩典凋誨蝣潭
$ L. s' g: \# g" A) H
, y8 ^9 w( b O/ U4 v嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗 - 箸迨撱箇銝 JS 瑼獢
- 閮餃JS隞
8 C) _5 ^. ], g8 f: y" @
! C$ z3 R6 o6 Y8 h! f J& h, E0 K4.1 JS隞嗡撱箇JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗 - (function() {
; m' Y* l7 m Q# I7 Z. C. A - tinymce.create('tinymce.plugins.recentposts', {
2 ]& d7 V2 ~9 Y - init : function(ed, url) {9 B* k3 R3 e* I; B% b$ y& Q
- ed.addButton('recentposts', {
: @. a2 v8 y, ~9 W( ~1 y; j2 e - title : 'Recent posts',
6 H7 l7 v3 _3 j1 K8 E* s; S - image : url+'/recentpostsbutton.png',% H* ]5 _6 X. m' S
- onclick : function() {
( N/ m. l) i2 T! b1 D7 q$ u& `4 j - var posts = prompt("蝡賊", "1");6 o" h7 g% b# ?. ^% U! l. t D/ b
- var text = prompt("隢憛怠亙銵冽憿", "唳蝡");
8 u' {' x( a! H# C6 j$ c+ {# v -
; L. F) h7 p* e) Y. _ - if (text != null && text != ''){
3 t- g7 h& f, J, G; _2 N8 Y! L/ C% R- e - if (posts != null && posts != '')9 j) u4 O7 _4 z
- ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');
5 h& n, S/ {5 K( C4 \ - else" |- p' c5 L- W1 z) ]
- ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');
/ Y1 k I" G( o - }
/ r4 @( Y) J( v2 F - else{
+ T) }" L. f5 F/ F - if (posts != null && posts != '')
0 e" P1 g5 O) }* n& i T' M - ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');
% j4 o+ q7 t1 ^+ |- G# q - else6 Z8 J7 W3 `& G7 T; O( ?
- ed.execCommand('mceInsertContent', false, '[recent-posts]');
2 M! m% u7 i% n, ]9 }+ ?3 I - }4 {6 M5 R) [, x. R' f
- }+ ^5 M y$ w" ?" ^" h, b
- });4 @0 Y9 ~4 F9 Z- V. w3 P
- },
* [. b; ?# S/ ]" z) z. H - createControl : function(n, cm) {; j% t8 ~0 d6 @/ _! P: E. e& j
- return null;' S, U: Q- h. U
- },0 _5 i3 Q u) K( o7 N% s- [9 r
- getInfo : function() {0 G2 T* C; s& [2 L! b/ `. l6 |9 M8 K
- return {
$ D+ B- R4 f6 F* s! ]: b - longname : "Recent Posts",2 i0 M. d+ v w9 Q6 Q/ Z
- author : 'Specs',; Z e$ P7 R* |+ ^
- authorurl : 'http://9iphp.com',
: V8 `" o' D5 Z& c) Y. y# j - infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',0 b2 Y! j0 u P8 z
- version : "1.0"2 ?2 p- S0 ?2 r3 ~# l. C! M7 r
- };$ k) V7 r% H! U* L8 B1 [2 m
- }
7 |' R/ Z2 O( Z9 p - });
) E7 ^/ |$ C% M; W8 M' Y! N2 V - tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);
) L, V1 P6 p9 V/ |! y( k2 e7 L - })();
銴鋆賭誨蝣 澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨 onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉 敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准 0 Q) N5 J' w& W* O
4.2 閮餃TinyMCE隞
4 S8 W! G% _9 i Z9 M3 q- i" o! i) u) Z7 d
嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉 蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩 - function register_button( $buttons ) {, K; J: t2 o l8 u
- array_push( $buttons, "|", "recentposts" );
+ T1 b+ [: R% i2 \) v* T- b; R - return $buttons;) X! Q. f+ n7 L1 V& t8 J. E
- }
銴鋆賭誨蝣 蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅' d6 A5 @- |6 G* j: b& M
- function add_plugin( $plugin_array ) {
- q( ?2 q2 [- D z - $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';$ g; Y4 P; Y6 l' j4 i0 L
- return $plugin_array;( o6 U R+ T. h* P
- }
銴鋆賭誨蝣 銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁
$ N$ ]# y9 \% R: Q- function my_recent_posts_button() {
, a S- x9 M% z - if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {; ]* I' I) V" E( x' x, Y
- return;, e. M& n/ u4 b) \1 B0 n# k! X9 y9 r' \
- }% a* M( { s4 Z
- 4 S, w# H# p2 _# w" N9 l
- if ( get_user_option('rich_editing') == 'true' ) {7 t* z B+ P' N4 a& `
- add_filter( 'mce_external_plugins', 'add_plugin' );$ K% y% Y2 f3 J p& p! o3 u( k
- add_filter( 'mce_buttons', 'register_button' );
n9 Q1 X! v V( P0 n - } 2 f9 l8 |5 }$ E
- }
銴鋆賭誨蝣 嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁1 s- v! T$ N. H, h: z
敺賣詨曉訖ordpress憪賣- add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣
5 g2 x4 ^ l8 o8 r1 M, E4.3 冽5 P6 h2 }' J5 H) ~' J
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
! {/ [! M4 i# Y4 b& c3 w) c 暺嚗敶箏閰望霈頛詨交蝡賊嚗
3 w& Z; Z8 J; x6 U6 l0 t& J& } 頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
7 R: a7 D0 X B 貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
9 _/ X R4 t {0 r( ^5 U
唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆 |
|