52AV璈A|52AV.ONE

 曉撖蝣
 蝡唾酉
敹急瑕
  • av隢憯BBS
  • 璈A
  • 芣瑟憭瘚
  • 鞎澆
  • 52av鋆貉摰
  • 銝剜-銝剖銝餅
  • AI脫憟喳
     
亦: 4188|敺: 0
銝銝銝駁 銝銝銝駁

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

[銴鋆賡包
頝唾唳摰璅撅
璅銝
潸” 2016-6-9 12:40:16 | 芰閰脖 |芰憭批 撣 |摨閬 |梯璅∪
WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)
6 |; @% C3 y+ b2 r: w0 A
5 f* V8 f0 d/ U% |1 H" E6 B$ lwordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐/ D% }0 n. D- z2 U) o) H
隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具' \0 }' O1 O  O) b8 e  s
4 r8 z% N/ ~0 H: b; P6 J
凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :5 w4 l+ \, L+ ^/ w1 \
  1. [recent-posts]
銴鋆賭誨蝣
甇文 , 臭誑閮剖銝訾批嗆蝡賊 :
" K$ q2 I2 l. Y1 h' f
  1. [recent-posts posts="5"]
銴鋆賭誨蝣
甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :
7 L5 _, V1 J% C. N
  1. [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣
$ d9 p3 r& u) |! ?4 o
. x2 X$ t/ e3 h4 h- G% X) |: E8 X
1. 蝪∪桃凋誨蝣
; g2 a6 M! B) e) Y
祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :9 _: M1 Y) P% p2 R; R
  1. [recent-posts]
銴鋆賭誨蝣
閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :
$ |$ k( X/ D1 K& Q
  • 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
  • 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
  • 閮餃賣賊蝯針ordpressaction銝
    5 {3 _) g- ^, z1 Q, c
5 O1 t- c  i3 D  t0 t$ k
祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准
2 w; F, F# J- n( s
1.1 撱箇餈賣
% U9 u- @8 Y6 H* N' b嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡2 t, I1 e. A( }7 X1 S! V
  1. function recent_posts_function() {8 Y! T8 r8 ^6 W
  2.         query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));
    $ B5 b9 x- h4 H
  3.         if (have_posts()) :6 E' J+ E5 I% ]$ x6 p
  4.                 while (have_posts()) : the_post();$ t- H) G1 l" P" t
  5.                         $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';6 L& V4 J. G4 W5 E0 r- O
  6.                 endwhile;
    ( i. n6 J; v+ O. D# U' J" a
  7.         endif;
    0 |! o& w, R7 N( c- |9 T, D
  8.         wp_reset_query();
    $ I+ r8 I- q! o. m+ m7 e
  9.         return $return_string;
    4 ^8 G* D+ \5 {$ b! C0 C8 J0 {; Q/ W
  10. }
銴鋆賭誨蝣
憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯
' s7 X/ X% Y8 y1 C( x

+ H* ^* O' B6 a$ S

/ Q# F( \$ V" I: `, j1.2 閮餃凋誨蝣
9 g, t6 ^% Q# C; E" J' t曉剁閮幃ordpress賣豢臭凋誨蝣潘
  1. function register_shortcodes(){) j( b- [, b' K
  2.         add_shortcode('recent-posts', 'recent_posts_function');
    9 O2 p! q7 y/ w( ]; D0 n2 ^7 O
  3. }
銴鋆賭誨蝣
嗅冽蝡批捆銝凋蝙函凋誨蝣
, \5 r" u" Y$ P* e! j/ S' M: z
  1. [recent-posts]
銴鋆賭誨蝣
撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴

+ H% I6 @- S8 @- A4 Z* h& V
- G, j: r& {% N2 Z

5 [0 N, Q0 ?+ d) I1.3 Hook into WordPress- E1 X# b% m8 w# ?+ S
' W3 i6 n) w/ B; M8 w
箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准' \4 g4 m6 l7 `
  1. add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣
  x) e0 n! r* N6 w
1.4 皜祈岫凋誨蝣
1 Y5 x- X: U. p& y, m; ]6 [
蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :
  1. [recent-posts]
銴鋆賭誨蝣
2. 脤凋誨蝣
2.1 凋誨蝣澆
凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻
閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼
撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 :
  1. function recent_posts_function($atts){
    * \8 T* l( M0 r# A$ @& y
  2.         extract(shortcode_atts(array(
    7 [& o! L2 }+ K$ u7 x8 o( ?
  3.                 'posts' => 1,% k- @" m4 `# L+ }
  4.         ), $atts));
    ; h0 ?7 h' r- s5 W5 {/ T" z
  5. % O5 c, p2 }! C% y
  6.         $return_string = '<ul>';
    * o! k% i8 s" w9 S: F
  7.         query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
    0 `1 q4 f: |! Y2 w/ y  a' A
  8.         if (have_posts()) :$ }& u+ L4 e  H, P  _( o
  9.                 while (have_posts()) : the_post();$ C1 c5 i6 K" v- w
  10.                         $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';) D+ Y! w% S+ y1 f# J
  11.                 endwhile;
    6 F* A  W9 T: U+ {6 E- ?
  12.         endif;
    ' p4 E  H. g$ s% s" A3 j, J6 ~
  13.         $return_string .= '</ul>';3 y% V' t9 Y0 q( k# i0 k% v6 S
  14. , w0 t1 J( f' {, y
  15.         wp_reset_query();# u8 g" y6 q; i% D# q# B
  16.         return $return_string;
    , P& g: I6 Q% U' A
  17. }
銴鋆賭誨蝣
憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :
  1. [recent-posts posts="5"]
銴鋆賭誨蝣
園*蝷箸潛閬賢 , 典停臭誑唳餈鈭蝭蝡銵
  _2 {- ?" Q. H" q5 w/ V
2.2 凋誨蝣潔葉憓摰
臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗
  1. function recent_posts_function($atts, $content = null) {+ j0 w+ Q* Z/ y
  2.         extract(shortcode_atts(array(3 \2 R9 z8 R9 u2 D
  3.                 'posts' => 1,/ J' X1 B9 I/ |+ \. _
  4.         ), $atts));
    , r1 u- ~# V9 k

  5. ( H% \: `; ~8 _# e1 A$ K6 K
  6.         $return_string = '<h3>'.$content.'</h3>';& Z- ?' Y+ C1 o6 p$ }( g
  7.         $return_string .= '<ul>';
    : f1 z, N8 [7 f5 r4 T6 ]
  8.         query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));# h# M2 u9 ~9 O/ O' b2 k) D
  9.         if (have_posts()) :8 O7 m2 h; H4 y
  10.                 while (have_posts()) : the_post();
    ( c1 n5 s( b* ^1 B6 W. N
  11.                         $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
    5 j' ~$ o4 D1 l" l6 ~/ o
  12.                 endwhile;2 D+ W! y% e+ m: t
  13.         endif;
    : ?* a5 j) f( f' v+ s0 D) ~: Z
  14.         $return_string .= '</ul>';
    . p( c/ f; Z0 d+ l9 ~
  15. $ d' K2 e) j: F! A
  16.         wp_reset_query();; e+ N2 x& ~4 w0 H) @: D, i1 g) a
  17.         return $return_string;% y* ]9 c! r5 K% i0 t# ^
  18. }
銴鋆賭誨蝣
蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝
  1. [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣
支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗

& y0 t' q! A( ]$ o- n( @
3. 其遙雿唳寥*蝷箇凋誨蝣
3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣
閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛典臭誑撠甇斤撘蝣潭曉典撌亙

5 `1 g  B  J# B, x' T( p) p+ y
臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 :
  1. add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣
曉其閬靽格嫣遙雿嗡唳
, 瑟, 湧甈撠甇蝣箏圈*蝷
1 w+ x1 ~7 Z* }
璅嚗刻隢銝剖函凋誨蝣潘
  1. add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣
冽閬銝剖函凋誨蝣潘
  1. add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣
4. TinyMCE蝺刻摩典凋誨蝣潭
9 y% k1 j! }% L
4 W! S4 N( Y) W5 `嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯
閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗
  • 箸迨撱箇銝 JS 瑼獢
  • 閮餃JS隞# w' e3 E8 }3 k" D# Q; F* L
; p7 m! V/ [  n
4.1 JS隞嗡撱箇
JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗
  1. (function() {
    % g/ p5 `! l1 Y  d4 c
  2.         tinymce.create('tinymce.plugins.recentposts', {* o/ x; _* C  g# S
  3.                 init : function(ed, url) {5 ^6 E3 z* T3 t* h4 B. W
  4.                         ed.addButton('recentposts', {
    . ?+ I- u& X9 p' |0 [* |: @! T. g
  5.                                 title : 'Recent posts',
    % r+ E$ X6 v* q( W% Z. [
  6.                                 image : url+'/recentpostsbutton.png',9 H, \, A/ R; j# [( T1 z; B9 X
  7.                                 onclick : function() {
    ! e2 k" j5 \% s* o
  8.                                         var posts = prompt("蝡賊", "1");8 W' g# m$ ]" `' o& W+ G
  9.                                         var text = prompt("隢憛怠亙銵冽憿", "唳蝡");
    , P8 O3 Y& f  V; h

  10. ) t6 J" l& l  ^. n$ B* X3 K
  11.                                         if (text != null && text != ''){, M9 d4 E3 A: a0 R9 b2 V
  12.                                                 if (posts != null && posts != '')
    4 o, D1 P/ ^# p  S3 J
  13.                                                 ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');+ ^6 M2 ^, L0 {$ X) A
  14.                                         else
    5 v1 l; A0 P; V
  15.                                                 ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');
    ( ^9 O0 K1 C/ N  U( z5 U/ }
  16.                                         }* S& v$ X  }9 y3 \% O
  17.                                         else{* X6 `8 c% A3 `
  18.                                                 if (posts != null && posts != '')
    4 i/ P! ]3 q0 ^6 b8 W
  19.                                                         ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');8 L' d% b6 `+ u1 R. z$ U' u
  20.                                                 else  V/ u" w3 Z, K! C
  21.                                                         ed.execCommand('mceInsertContent', false, '[recent-posts]');
    : a5 l3 d# J. \3 _$ `
  22.                                         }
    ' C0 H' H! f( h% L0 Y  `6 ^
  23.                                 }
    $ K5 z+ Q& \" J: ?& Y$ e
  24.                         });3 l% {. ^6 `# P- r
  25.                 },
    4 {6 P3 M7 j* G
  26.                 createControl : function(n, cm) {
    , \! \+ |5 X( @1 B
  27.                         return null;1 @! l+ Q4 x3 L' F) j
  28.                 },
    2 p4 r+ n; _; j+ W- ~6 \1 N# _9 d
  29.                 getInfo : function() {
    # ~$ n9 c" j- F; k6 W1 \
  30.                         return {' C& I+ p% C3 N' R( P0 y; Z
  31.                                 longname : "Recent Posts",1 s  o4 M+ [; @# |9 f+ Q
  32.                                 author : 'Specs',+ \) W8 `  W, T* E! v) x
  33.                                 authorurl : 'http://9iphp.com',9 H. d- S2 r- T3 A# A$ R
  34.                                 infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',
    # R! J$ C' c* l) U5 i' H, G
  35.                                 version : "1.0". R! c" f2 Y; }9 k
  36.                         };; Y) j2 X7 E6 A
  37.                 }
    / D0 A& w! ~# G# J
  38.         });+ Q: S. W9 h0 `
  39.         tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);* a: D* [5 y! w3 N+ b
  40. })();
銴鋆賭誨蝣
澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨
onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉
敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准

  [% y+ _5 M# r( {9 i
4.2 閮餃TinyMCE隞
+ C% [% I  ^) l9 N  Z# r, Q
! V& o" |$ b$ }1 F
嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉
蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩
  1. function register_button( $buttons ) {
    ! d. ~/ L- w; K" g& \- a
  2.         array_push( $buttons, "|", "recentposts" );, q8 ]5 @& L$ k0 Y9 a
  3.         return $buttons;* F# D, w/ i! F
  4. }
銴鋆賭誨蝣
蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅5 s9 j/ F$ @: |, o. g5 U
  1. function add_plugin( $plugin_array ) {
      M1 F  D" t( J" I: \) C& G0 [3 t
  2.         $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';
    9 ?& e! O; i8 V  C
  3.         return $plugin_array;
      z: }2 o/ s1 g: x
  4. }
銴鋆賭誨蝣
銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁
6 @( @9 C1 V9 y0 H, F
  1. function my_recent_posts_button() {
    - W0 S, G3 f+ [, p1 I
  2.         if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {5 ]+ L6 N2 u# @6 |$ ]- n- k
  3.                 return;$ w  P: ~5 a1 ?! G, G
  4.         }* `" O. F1 z* t) x, y% H

  5. ) J8 A7 f2 k( O
  6.         if ( get_user_option('rich_editing') == 'true' ) {) `" D& f% p0 ?" a; h% u
  7.                 add_filter( 'mce_external_plugins', 'add_plugin' );
    ) v* P# h, |* r+ k3 \5 ~7 m, M
  8.                 add_filter( 'mce_buttons', 'register_button' );
    6 g  W* [* }' }3 j9 |% j2 K, w
  9.         } 3 }  a$ M% H1 [. {. W
  10. }
銴鋆賭誨蝣
嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁
3 b! _. X4 [8 I敺賣詨曉訖ordpress憪賣
  1. add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣
" H8 t! u; r5 A+ q
4.3 冽
7 r+ t4 T1 l% P4 o
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽

5 L% U) K% q- q6 C8 e
暺嚗敶箏閰望霈頛詨交蝡賊嚗

2 }( h2 @6 A( X6 t: X: Q. P
頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗

9 E" o( ]( i* _' p# q
貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
& k1 ?2 D6 R1 B0 O7 x% c) t6 P
唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆
蝡: 1 2

雿輻券

祉蝛閬

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

QQ|撠暺撅||52AV璈A

GMT+8, 2026-3-28 16:45 , Processed in 0.068282 second(s), 22 queries .

蝯∠.撱

52avtv@gmail.com | QQ:2405733034     since 2015-01

鋆貉憒 敹恍敺 餈銵