52AV璈A|52AV.ONE

 曉撖蝣
 蝡唾酉
敹急瑕
  • av隢憯BBS
  • 璈A
  • 芣瑟憭瘚
  • 鞎澆
  • 52av鋆貉摰
  • 銝剜-銝剖銝餅
  • 伐AI璁函移瘛怠伐
     
亦: 4041|敺: 0
銝銝銝駁 銝銝銝駁

[wordpress] WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭[4撘萄]

[銴鋆賡包
頝唾唳摰璅撅
璅銝
潸” 2016-6-9 12:40:16 | 芰閰脖 |芰憭批 撣 |摨閬 |梯璅∪
WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)1 e) @" @1 V& p$ Y. S
0 @1 Z# R: f7 i! j" @, e% ^
wordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐
  v( {: p1 s/ D" ?
隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具
3 Y3 S( f- \& C1 R! A; Y  Z% i. |8 k' A& C: w8 }1 Y5 c
凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :6 e8 J2 e" b( S2 Q& y( I
  1. [recent-posts]
銴鋆賭誨蝣
甇文 , 臭誑閮剖銝訾批嗆蝡賊 :
8 ~) t8 B7 W/ S' G& T) x
  1. [recent-posts posts="5"]
銴鋆賭誨蝣
甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :
7 J" ~0 p  z% ?' n6 Y( @
  1. [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣
% ^% `6 }. A; U9 B6 g

/ d, c* W* p  S& V' o1. 蝪∪桃凋誨蝣

' O0 I: s) t( M. o+ p祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :  g9 ?% [, s9 l! Y3 P
  1. [recent-posts]
銴鋆賭誨蝣
閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :
+ ?" F) z( Z* F* r, L) C# c
  • 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
  • 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
  • 閮餃賣賊蝯針ordpressaction銝
    - g7 ^2 p2 K& _2 c' H2 m6 X( n
/ j- S' [& Z; E: P- X
祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准
, o& \8 A9 [0 `% \) Y
1.1 撱箇餈賣4 ~2 D1 v  [$ l- s; m2 H7 Q
嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡$ |- ?* M; I$ {2 R4 b1 F
  1. function recent_posts_function() {8 j2 x' Z/ {0 {
  2.         query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));6 m& I) E+ E- h! H
  3.         if (have_posts()) :1 Y" p8 {0 ^# w# t+ U
  4.                 while (have_posts()) : the_post();
    0 l2 ]* f9 B1 W4 n& r
  5.                         $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';
    % m" E: h; Y! c9 N$ u; [7 i' n
  6.                 endwhile;
    + M( n# I5 l' j; O  s6 p7 S) C' V! U
  7.         endif;
    2 E# }( U4 K5 l4 E8 a
  8.         wp_reset_query();# s# u8 ?) a* W$ V) M
  9.         return $return_string;
    ; a; z. U  d- C) M/ J) V
  10. }
銴鋆賭誨蝣
憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯

  P2 R7 u* N. `, z# w1 a
" \8 g+ l- r. O' m1 w) E
$ Z) B# {& ^" J% `3 A
1.2 閮餃凋誨蝣
0 S2 ]7 H2 S* \1 G, G: G曉剁閮幃ordpress賣豢臭凋誨蝣潘
  1. function register_shortcodes(){$ ?! X# Q& b$ V2 T
  2.         add_shortcode('recent-posts', 'recent_posts_function');, ^. J' ~3 O* @
  3. }
銴鋆賭誨蝣
嗅冽蝡批捆銝凋蝙函凋誨蝣
6 x5 Q/ i: {% P5 Y+ h
  1. [recent-posts]
銴鋆賭誨蝣
撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴
  m+ _3 H7 C1 x9 s% y: [+ D

, m: m4 a1 \: Z- h" ~" R4 S

1 R+ Q8 z/ k5 |2 k0 C8 z1.3 Hook into WordPress1 @3 h. a. f! b& g9 t
6 [5 B9 |& ~3 O  {* X  B
箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准
9 j! `7 z4 n) K9 q
  1. add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣

  R! U" E, S' M1.4 皜祈岫凋誨蝣
; ~8 L' P: N$ U% g
蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :
  1. [recent-posts]
銴鋆賭誨蝣
2. 脤凋誨蝣
2.1 凋誨蝣澆
凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻
閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼
撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 :
  1. function recent_posts_function($atts){
    4 S3 D7 R5 J! p$ g  |, \# P4 V
  2.         extract(shortcode_atts(array(; B' e6 @  r6 Q: B, x
  3.                 'posts' => 1,; f. @" l% G, y
  4.         ), $atts));
    7 @8 X  d1 V0 y0 C

  5. $ W4 Y0 G% r% R( j& ^
  6.         $return_string = '<ul>';
    ! G: H& E% I  y: p" t% a! ?  z* Y
  7.         query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
    3 U( \. M1 a. Y4 \9 r
  8.         if (have_posts()) :5 H1 U% h( i8 {6 O
  9.                 while (have_posts()) : the_post();4 H' T' s5 G, ]$ L6 L
  10.                         $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
    # u+ N8 H% B& A" b/ e; q% V* A/ ?
  11.                 endwhile;
    ! p# ?2 t0 a7 C6 F# y9 c
  12.         endif;
    - v9 c5 C1 `* i+ |
  13.         $return_string .= '</ul>';- @- T+ ^6 H$ b

  14. / }3 U) n9 ~$ o4 {1 \) y
  15.         wp_reset_query();
    3 k4 h9 s- Y! n2 [
  16.         return $return_string;
    8 q* U& R8 G$ q" y* X+ X5 K. r  f
  17. }
銴鋆賭誨蝣
憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :
  1. [recent-posts posts="5"]
銴鋆賭誨蝣
園*蝷箸潛閬賢 , 典停臭誑唳餈鈭蝭蝡銵
% k; j" N1 X) g& s0 G
2.2 凋誨蝣潔葉憓摰
臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗
  1. function recent_posts_function($atts, $content = null) {+ ]2 |) V/ }! ]3 T- J; d  K
  2.         extract(shortcode_atts(array(
    4 |3 z3 c3 P, G- ?
  3.                 'posts' => 1,
    * D* s  r; a) d: l7 ?7 a' b
  4.         ), $atts));
    , j, p. d. c7 X* b4 s
  5. ( Q0 L- H) y, y! N8 m5 }7 L! n
  6.         $return_string = '<h3>'.$content.'</h3>';
    " n( E9 l. u. j: |
  7.         $return_string .= '<ul>';$ O+ |8 h" c* a3 r+ w) q
  8.         query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
      y2 [7 @: j. {
  9.         if (have_posts()) :, u! g0 r) H$ }# \, o$ m
  10.                 while (have_posts()) : the_post();0 j. o5 k2 n% _# K
  11.                         $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';3 V; _) \# Z7 j9 Q5 c
  12.                 endwhile;) I+ B; I9 N9 H3 }3 N1 ]; M2 B
  13.         endif;
    ) M$ z1 S0 \" \# V# @
  14.         $return_string .= '</ul>';/ ]: o: x- g# Y" @
  15. ( h! \5 H# a* i$ r0 i& ]7 s5 ?/ X
  16.         wp_reset_query();% b) ~/ @! o/ r( U$ H, x4 A
  17.         return $return_string;
    6 D9 k( ]0 D$ y( A
  18. }
銴鋆賭誨蝣
蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝
  1. [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣
支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗
3 _  C/ J$ C; s/ R1 d
3. 其遙雿唳寥*蝷箇凋誨蝣
3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣
閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛典臭誑撠甇斤撘蝣潭曉典撌亙
& y9 ~- r$ t4 p% X; ]% {5 N, H) y
臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 :
  1. add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣
曉其閬靽格嫣遙雿嗡唳
, 瑟, 湧甈撠甇蝣箏圈*蝷
# J$ Q& E; T" a/ U
璅嚗刻隢銝剖函凋誨蝣潘
  1. add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣
冽閬銝剖函凋誨蝣潘
  1. add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣
4. TinyMCE蝺刻摩典凋誨蝣潭. W3 S* `- L* w& K
6 J! V$ Z* U( z! l* g
嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯
閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗
  • 箸迨撱箇銝 JS 瑼獢
  • 閮餃JS隞
    0 H- r7 X' g/ s- @
4 X7 }5 W! J; m
4.1 JS隞嗡撱箇
JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗
  1. (function() {
    8 Y* I* K9 E- ]7 T" `, J
  2.         tinymce.create('tinymce.plugins.recentposts', {
    - W, C! g; _- H, |5 [' G
  3.                 init : function(ed, url) {( b& x- P' g; t8 N
  4.                         ed.addButton('recentposts', {! N% `% O5 e0 d, ]
  5.                                 title : 'Recent posts',# |% Q6 l* f0 M3 ?" G+ g
  6.                                 image : url+'/recentpostsbutton.png',
    ! B7 T  X1 l2 K
  7.                                 onclick : function() {
    ) ?& Z* e# b/ Z; p3 }8 Y% T
  8.                                         var posts = prompt("蝡賊", "1");' V9 O% J# S' P6 ?9 y
  9.                                         var text = prompt("隢憛怠亙銵冽憿", "唳蝡");& @* t$ U/ j! v) `% o

  10. 1 v7 {( ?, z+ a; B0 o
  11.                                         if (text != null && text != ''){% U" j% n2 ^+ G) G# {; w
  12.                                                 if (posts != null && posts != '')! a& x2 {3 S, |
  13.                                                 ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');+ _) I9 K, s* \/ A3 `" x
  14.                                         else- ?1 T8 ]  Q' T5 V# m  }' X' q
  15.                                                 ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');6 X0 B! R4 o' k. X9 S* y2 g7 E+ `
  16.                                         }& R8 ]/ y/ W! ?6 S6 |7 s+ p, s
  17.                                         else{, i7 C! V2 z( f3 ?2 e% Z' {
  18.                                                 if (posts != null && posts != '')
    - c& c# E8 C/ y& o% l( t
  19.                                                         ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');& r4 Q# Z8 ^/ L+ T+ w
  20.                                                 else" t2 V' G+ w. H
  21.                                                         ed.execCommand('mceInsertContent', false, '[recent-posts]');
    , n3 c) z  Z6 R, \. L. a2 Q9 g
  22.                                         }
    " v1 N! W8 c8 z* m4 d) n
  23.                                 }
    2 L( |4 g# C& o* R0 M* k
  24.                         });6 C; l$ ?5 I" P7 ^+ v; }
  25.                 },; b4 J/ Q8 y* x, v% P& e
  26.                 createControl : function(n, cm) {
    8 s) c3 N9 k  r+ f  [3 j
  27.                         return null;
    1 y+ K/ N0 ^" W0 _& S
  28.                 },
    / e$ t6 b2 ]$ W( V& [- p9 }' y
  29.                 getInfo : function() {
      }* Y* d9 Q% B9 Y% d. J) r
  30.                         return {( y# N! E  Q( a% K
  31.                                 longname : "Recent Posts",
    0 P7 x; R4 o$ o" R* P
  32.                                 author : 'Specs'," S( B2 c$ m/ B3 h5 ?2 S  O! e
  33.                                 authorurl : 'http://9iphp.com',- |) O* M! L$ q: i8 M6 u
  34.                                 infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',
    5 [7 R. _' W% [* O
  35.                                 version : "1.0"0 v! g; A. z( u, Z% x1 v. n  W
  36.                         };
    ; w: q+ D  ^  S1 I& K6 e% o4 C
  37.                 }
    3 W" T; p% \7 P! T! a2 H. T8 p
  38.         });
    ; |- H  d: c* B
  39.         tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);* t' ~* B9 E. }4 N( z8 U" h
  40. })();
銴鋆賭誨蝣
澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨
onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉
敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准
' M. X$ q& S+ `: T5 j
4.2 閮餃TinyMCE隞
6 L6 \" i; O# `4 c9 A
1 g$ B2 V* O* F$ d1 ?
嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉
蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩
  1. function register_button( $buttons ) {
    " ]+ P; D3 P+ b' Q) |8 e
  2.         array_push( $buttons, "|", "recentposts" );
    1 ^9 J2 }2 M8 O. u! d, ]3 v
  3.         return $buttons;% Z0 f. v/ b: {- K, ?( {
  4. }
銴鋆賭誨蝣
蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅
) K5 \0 Q8 Y' Y
  1. function add_plugin( $plugin_array ) {( f" O' x  B% e$ A/ W
  2.         $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';
    $ G; i" X5 T0 d1 w* D" M3 ]
  3.         return $plugin_array;8 C- u* x+ w  Z1 M5 B; D, u
  4. }
銴鋆賭誨蝣
銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁
6 W% K3 N5 K: |; G
  1. function my_recent_posts_button() {
    . I7 p& r# E/ E
  2.         if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {) P) c7 y; J% |5 O4 C+ _
  3.                 return;7 h2 L& D" O$ g' G) U
  4.         }
    0 {& N2 s3 g4 S2 u5 C% x
  5.   I: I1 Z+ r/ M. ]7 l& @
  6.         if ( get_user_option('rich_editing') == 'true' ) {
    ) h( [" M2 }, Q
  7.                 add_filter( 'mce_external_plugins', 'add_plugin' );
    . Q8 e- E' O0 g( \1 w& f9 T
  8.                 add_filter( 'mce_buttons', 'register_button' );0 H+ `7 q; }5 h0 G) L* h. C
  9.         }
    ! H: ~. [2 k. l7 r" _- Z
  10. }
銴鋆賭誨蝣
嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁
  ~. I& j( ?. x# L2 ~敺賣詨曉訖ordpress憪賣
  1. add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣
% x3 l2 y7 u1 ^/ m" e! m
4.3 冽

1 ]% Q9 w2 x3 ]. B+ x
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽

0 h& B! p- d2 M# M
暺嚗敶箏閰望霈頛詨交蝡賊嚗
/ X- t  N; C6 b$ u5 _
頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
$ Z! J9 G% C/ l9 c5 _
貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
- T0 G# g' O" `
唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆
蝡: 1 2

雿輻券

祉蝛閬

砍憛批捆靘餉衣雯頝臬批捆蝝颲行粹嗥蝬脩嚗摰撟湔遛嚗嚗甇脖誑銝嗅啣摰嗆摰撟湧翩鈭箏ㄚ孵舫脣伐銝憿亙祉璇甈橘芣遛18甇 雓蝯脣亦閬賬粹脩芣遛18甇脖芣撟渡雯閬賜雯頝臭嗥批捆鞈閮嚗撱箄降典舫脰蝬脰楝批捆蝝蝯蝜ICRA蝝摰鋆閮剖 (粹蝯行霅 祉蝬脣銝蝝瘛函隢憯啣嚗祉閮剜蝞∠)

QQ|撠暺撅||52AV璈A

GMT+8, 2026-1-26 03:11 , Processed in 0.074380 second(s), 22 queries .

蝯∠.撱

[email protected] | QQ:2405733034     since 2015-01

鋆貉憒 敹恍敺 餈銵