52AV璈A|52AV.ONE

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

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

[銴鋆賡包
頝唾唳摰璅撅
璅銝
潸” 2016-6-9 12:40:16 | 芰閰脖 |芰憭批 撣 |摨閬 |梯璅∪
WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)7 h9 M9 _, t% A& S

" A+ U6 F  o9 [  H! d0 f5 hwordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐2 n* ?% A6 N1 \) U6 N. f8 b
隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具3 m( f! ]. e; Z  x5 J
, \8 U, k) [' }2 |3 G
凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :
  F3 `& [* i' ^* a, ?. `  J
  1. [recent-posts]
銴鋆賭誨蝣
甇文 , 臭誑閮剖銝訾批嗆蝡賊 :
. d/ X6 J: u7 S4 D! ]( M
  1. [recent-posts posts="5"]
銴鋆賭誨蝣
甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :
& d# \' q3 G1 I1 d
  1. [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣
! n/ Z. ?: ~0 V9 z0 x
6 A  i( `: N  _. Y# g
1. 蝪∪桃凋誨蝣
8 w9 S5 [& t3 e
祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :1 z( I2 e* o* W, s1 u
  1. [recent-posts]
銴鋆賭誨蝣
閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :

9 a+ a2 g4 p7 g8 V; o
  • 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
  • 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
  • 閮餃賣賊蝯針ordpressaction銝
    ( G5 l& Q0 a) `( e! [2 m- |3 `

, S# n6 Q$ g. O0 y
祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准3 E% u# u/ k; I7 i& c1 ^
1.1 撱箇餈賣
6 v2 V9 f; R! y1 a2 {& W/ ]7 z嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡
& Q3 L4 x! t! X
  1. function recent_posts_function() {5 o6 K) G- s4 `) M5 }
  2.         query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));
    " |1 m; `( D! p! O  H2 w( t. r8 F
  3.         if (have_posts()) :- r1 W! g7 ^1 S, `) z' J
  4.                 while (have_posts()) : the_post();
    * E! t8 b' E: k  d* R# l. q
  5.                         $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';
    - C* v7 N' x% [
  6.                 endwhile;: _( K" C3 {% X4 `, b
  7.         endif;9 \5 p2 z0 @6 ^# e: r
  8.         wp_reset_query();0 W% r, d9 o$ B! E! K' s7 }! X
  9.         return $return_string;  o4 P1 G; a7 W: @
  10. }
銴鋆賭誨蝣
憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯

2 ?3 H: d2 U% a( T3 P
' ]  K8 Z4 g" R7 o

9 o( h  l4 x4 r: p4 p3 P6 W1.2 閮餃凋誨蝣
& T0 C* g( g! q3 |曉剁閮幃ordpress賣豢臭凋誨蝣潘
  1. function register_shortcodes(){
    6 `- c: M& A7 W9 i. I! o
  2.         add_shortcode('recent-posts', 'recent_posts_function');; Q/ w4 d2 J( \; `( _1 o
  3. }
銴鋆賭誨蝣
嗅冽蝡批捆銝凋蝙函凋誨蝣
" P  \" L  J9 |( W
  1. [recent-posts]
銴鋆賭誨蝣
撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴

1 c0 k8 I2 q2 p# `! I# V
6 b5 [* `- K' S+ |) |( b0 N
5 p. T# _- N: X; m' l  \
1.3 Hook into WordPress
/ X6 W' s! h* h4 L1 R3 L8 A$ U' p
) A8 m) ]. c$ C' a0 J' I箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准
9 }5 Y  B/ W% b1 G6 [
  1. add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣
/ b1 M: a8 P& b* H. _2 }$ E4 v
1.4 皜祈岫凋誨蝣
" X  v) F+ c5 @5 ~8 e1 X! h7 p' f
蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :
  1. [recent-posts]
銴鋆賭誨蝣
2. 脤凋誨蝣
2.1 凋誨蝣澆
凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻
閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼
撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 :
  1. function recent_posts_function($atts){
    ) b- ]9 J9 m  D! e
  2.         extract(shortcode_atts(array(  A- |. l! u& M: Z" x
  3.                 'posts' => 1,! p+ P; c3 Q' |6 ]
  4.         ), $atts));
    * d8 C7 ^1 s2 W9 m3 F6 G
  5. $ ?4 d* J3 o) c) j5 E# P: e4 K
  6.         $return_string = '<ul>';
    9 o) T7 W- l" G( q& y0 u
  7.         query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));6 b4 K) T  E' \# ^, a/ ?! X
  8.         if (have_posts()) :9 V: S: {: A2 ~- {- T
  9.                 while (have_posts()) : the_post();
    $ d  N% }$ T8 g
  10.                         $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';3 p; D9 H7 Y2 o+ ?/ I! T
  11.                 endwhile;
    ( n% `  ~! j$ Q/ W, q
  12.         endif;
    8 G2 B3 |. J) d
  13.         $return_string .= '</ul>';
    0 [, r& E/ [* H4 o, l) Q: e7 q
  14. - Z3 t0 k: o7 g6 P+ E% ]
  15.         wp_reset_query();
    6 o7 k0 {# R0 p4 c, N3 T: o
  16.         return $return_string;( `6 J8 G4 U( o4 {- x% j6 s; \
  17. }
銴鋆賭誨蝣
憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :
  1. [recent-posts posts="5"]
銴鋆賭誨蝣
園*蝷箸潛閬賢 , 典停臭誑唳餈鈭蝭蝡銵
2 D5 Y, z6 T4 i
2.2 凋誨蝣潔葉憓摰
臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗
  1. function recent_posts_function($atts, $content = null) {/ F- O$ Z3 X. R1 T' e. R! ~
  2.         extract(shortcode_atts(array(
    ; p$ @- W9 |8 ?) w$ x
  3.                 'posts' => 1,
    2 D" s) x8 B/ [9 V* L
  4.         ), $atts));
    5 I. a6 a6 f0 U& ]& ?7 J. a! V, S
  5. & n. l/ V( `$ n' G  e- n
  6.         $return_string = '<h3>'.$content.'</h3>';
    & a. O! U! D0 C& h, [
  7.         $return_string .= '<ul>';9 G9 N- B" \6 ]/ }/ o, d( e
  8.         query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));+ u& R% N5 Q+ }4 v) r0 h
  9.         if (have_posts()) :+ \8 m! I: h! C$ l2 j& N
  10.                 while (have_posts()) : the_post();
    ' a, v( ?/ V+ n# d0 x, j$ Z: I! }
  11.                         $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
    ; ?% g$ ?, D3 A. X# ~
  12.                 endwhile;$ y6 g, U/ |: V6 Q. j
  13.         endif;
    ( v6 Q& H, i7 B3 Q; _
  14.         $return_string .= '</ul>';
    8 q: y# c) L7 p

  15. 0 x6 T7 ?: Y1 s
  16.         wp_reset_query();
    2 _* n' W; l$ B" O
  17.         return $return_string;
    - E) M' I, _" @% s
  18. }
銴鋆賭誨蝣
蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝
  1. [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣
支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗

7 }; x4 J1 r- S' ~1 d
3. 其遙雿唳寥*蝷箇凋誨蝣
3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣
閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛典臭誑撠甇斤撘蝣潭曉典撌亙
$ {" D+ [; |8 \( s) C& y
臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 :
  1. add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣
曉其閬靽格嫣遙雿嗡唳
, 瑟, 湧甈撠甇蝣箏圈*蝷
5 h& ?2 W+ |  ~
璅嚗刻隢銝剖函凋誨蝣潘
  1. add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣
冽閬銝剖函凋誨蝣潘
  1. add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣
4. TinyMCE蝺刻摩典凋誨蝣潭
4 ]# f! G7 l& C$ n% G* e1 {1 o% C- |! `5 N3 m: _
嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯
閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗
  • 箸迨撱箇銝 JS 瑼獢
  • 閮餃JS隞
    9 z* A6 c" e( a% h( F  t# [
. D. ~- U" C0 h, r8 T; O
4.1 JS隞嗡撱箇
JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗
  1. (function() {
    + r* X5 l( f3 l; r7 J
  2.         tinymce.create('tinymce.plugins.recentposts', {) i- m# {0 u2 h- f
  3.                 init : function(ed, url) {/ x+ y# O' ?" @4 |9 f: j+ v
  4.                         ed.addButton('recentposts', {
      R. m/ Y' d+ N0 C1 Y2 `, U
  5.                                 title : 'Recent posts',. E# [' p9 O; D  Z( `0 c5 F
  6.                                 image : url+'/recentpostsbutton.png',
    9 R, a' \* J* L$ j7 [& |
  7.                                 onclick : function() {) j: _6 `  u5 p5 a/ X) K1 }
  8.                                         var posts = prompt("蝡賊", "1");" ]! I2 V" L$ x; c/ S
  9.                                         var text = prompt("隢憛怠亙銵冽憿", "唳蝡");+ }$ c' c. r9 b1 I8 ]

  10. % q: z3 F5 S' d. X; z6 c
  11.                                         if (text != null && text != ''){
    ( _0 \  E/ c3 c8 n# w3 v# ~/ {# k8 k
  12.                                                 if (posts != null && posts != '')
    . r# L4 g# _  e- v
  13.                                                 ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');. r7 w% z# D# n9 Z; r: p% x, X
  14.                                         else
    7 _- c+ [- m# @1 {% D, _
  15.                                                 ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');
    ; q9 t+ F- G0 ~$ q
  16.                                         }) h4 w/ R( b3 Z/ E" \" J4 \0 I
  17.                                         else{
    9 {) n+ J4 B& ^; t( E# `- f2 f
  18.                                                 if (posts != null && posts != '')
    * C! V. |9 G; `0 y- _# P& J
  19.                                                         ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');
    % V  Z3 p7 D+ s$ e
  20.                                                 else
    , h+ ?0 B$ q2 D% V% l( ^5 k# S( }
  21.                                                         ed.execCommand('mceInsertContent', false, '[recent-posts]');
    % L* ?) g5 B: \' f6 c* X8 s
  22.                                         }
    ( E* A1 b6 S7 F/ g
  23.                                 }( [; A! I$ s& [
  24.                         });
    2 ^" z! O8 P8 `" `2 Y
  25.                 },
    ' R+ W& c! }* n. `- s0 j) ?
  26.                 createControl : function(n, cm) {
    ' y5 g2 U2 a* ^4 s
  27.                         return null;
    ' @; _7 j" o9 u" r; L1 e
  28.                 },) e7 }& `7 ^- G" H* h* B
  29.                 getInfo : function() {
    ) Y3 ~, e6 ?1 {* X( X) M9 D5 g
  30.                         return {
    ; k5 T+ I: n% H! ~
  31.                                 longname : "Recent Posts",5 z0 C7 E( O$ A. X
  32.                                 author : 'Specs',
    - l( m, z' `& a* P$ q' E2 U% A" k
  33.                                 authorurl : 'http://9iphp.com',2 M8 x) ~1 I- @" b! K/ v, h
  34.                                 infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',$ C. l4 r+ x" F) Y, V
  35.                                 version : "1.0") o3 W* P5 J" `# z- f, M
  36.                         };
    + ?$ e  ]$ X9 U$ q
  37.                 }
    6 o  d2 y. j' Z. A
  38.         });
    . P( e# H: o+ z5 b& p
  39.         tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);
    9 T, {2 B' _% F- p* K
  40. })();
銴鋆賭誨蝣
澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨
onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉
敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准
! {  a6 |; v; i% c$ y$ N* t1 u
4.2 閮餃TinyMCE隞
& b8 o' F* s5 r6 V9 u
6 I4 W9 W9 Q5 a
嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉
蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩
  1. function register_button( $buttons ) {0 a1 H6 u% [& H% o4 g; L  w
  2.         array_push( $buttons, "|", "recentposts" );
    : Y, D: r1 I! I7 W0 H
  3.         return $buttons;; S# ?/ }) Y8 z- I& _
  4. }
銴鋆賭誨蝣
蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅8 A0 Q" d* H$ x% n
  1. function add_plugin( $plugin_array ) {8 `, G1 I/ Y9 O' f" M1 u  g+ j
  2.         $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';1 e' u" S5 S3 }4 r( Z; ~
  3.         return $plugin_array;' N! ^% J! K, S; A- b
  4. }
銴鋆賭誨蝣
銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁& [+ f( L% h0 Z+ V& y
  1. function my_recent_posts_button() { ; i2 G: C, v' R+ c
  2.         if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {
    , A# `/ n& ]; ]+ A  Z. O. y
  3.                 return;* f9 p1 T2 x$ [
  4.         }) `; u- i- {4 ~1 a( x2 L

  5. & O! _0 K6 J6 u# P( J
  6.         if ( get_user_option('rich_editing') == 'true' ) {$ w+ A& g0 V1 z! m0 b
  7.                 add_filter( 'mce_external_plugins', 'add_plugin' );. z, t6 j; P1 \0 W' P
  8.                 add_filter( 'mce_buttons', 'register_button' );* q6 Q/ U) x' t& z7 c
  9.         } ) ~: N+ `  `! o! Z8 Q- H
  10. }
銴鋆賭誨蝣
嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁
0 m9 Z" z! o- K1 P1 J+ U敺賣詨曉訖ordpress憪賣
  1. add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣
4 h, H4 T0 i+ R- Q& R+ V+ v
4.3 冽

. t0 ^4 [8 p2 y
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
1 {/ Y, y: L- v7 h. }
暺嚗敶箏閰望霈頛詨交蝡賊嚗
, F/ b  h& p6 b0 d$ i
頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
6 b1 m# @* Y% y* a% w
貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗

/ e$ E# M* z" N) v# ]
唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆
蝡: 1 2

雿輻券

祉蝛閬

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

QQ|撠暺撅||52AV璈A

GMT+8, 2026-3-28 19:13 , Processed in 0.086739 second(s), 22 queries .

蝯∠.撱

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

鋆貉憒 敹恍敺 餈銵