52AV璈A|52AV.ONE

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

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

[銴鋆賡包
頝唾唳摰璅撅
璅銝
潸” 2016-6-9 12:40:16 | 芰閰脖 |芰憭批 撣 |摨閬 |梯璅∪
WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)
6 @; g0 O7 h  q$ l) V3 J3 z
8 S/ C% o' D( f) [$ jwordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐7 e- v  X: |3 M  z  A( S% A
隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具
3 X: v, V: O9 r$ f' T! U' P7 O+ A5 P: {
凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :
  X' b$ C( f# B- g/ |) ^
  1. [recent-posts]
銴鋆賭誨蝣
甇文 , 臭誑閮剖銝訾批嗆蝡賊 :

5 c* y) j1 S' _9 t8 m2 i+ }. O
  1. [recent-posts posts="5"]
銴鋆賭誨蝣
甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :
  D3 I& ]+ ]! x7 \- C
  1. [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣
6 p. A0 v' ^; O) C' n4 z2 m
# c4 `0 B& j* |- V6 ?9 q
1. 蝪∪桃凋誨蝣

. O( ^: B( D2 l; `5 t8 P祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :! J, C1 J9 F; ~/ J, F7 `' G) `
  1. [recent-posts]
銴鋆賭誨蝣
閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :

7 K8 k5 o  `- L" ~
  • 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
  • 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
  • 閮餃賣賊蝯針ordpressaction銝
    # _* j; s' o  G! @  K1 h7 `+ _. `

* E4 f& I6 Z0 ]/ ?
祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准
; Z# `$ p/ y; `
1.1 撱箇餈賣( K8 A, E, x% J( x5 \4 X, ~' e
嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡
9 `" \! V1 u; L( p
  1. function recent_posts_function() {' ?2 i5 u  T& i9 z3 |
  2.         query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));
    % g7 G$ `2 K2 t5 T; K  a5 D0 I+ j
  3.         if (have_posts()) :
    5 M4 j, Y+ {! N. R7 H+ M
  4.                 while (have_posts()) : the_post();
    % t" Y9 K5 G3 ^
  5.                         $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';5 g5 ^- p8 W8 p$ \& l2 `4 |
  6.                 endwhile;
    5 Z- \% m. N2 ]
  7.         endif;
    0 X, J2 b9 R' ^' H" F& E. _
  8.         wp_reset_query();0 ^8 u% w1 D* a* d
  9.         return $return_string;
    $ l8 w3 G$ k2 u$ V$ d3 I, j
  10. }
銴鋆賭誨蝣
憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯

# Z/ B  P, }2 V  F8 o/ B( w  n" `- g

+ v1 }* a* `. W  g+ M0 i) Y% V1.2 閮餃凋誨蝣
8 j$ M$ n4 p- ~# ^* U. f8 Z曉剁閮幃ordpress賣豢臭凋誨蝣潘
  1. function register_shortcodes(){
    - k: O8 ~& H# u# `! C, D, o) S- w
  2.         add_shortcode('recent-posts', 'recent_posts_function');7 U" E: t  {( u( r
  3. }
銴鋆賭誨蝣
嗅冽蝡批捆銝凋蝙函凋誨蝣
& w3 k9 P; H( k2 V0 W1 r2 S
  1. [recent-posts]
銴鋆賭誨蝣
撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴

0 {4 P+ ?/ G# t# A9 f' Z# L9 {; q# G% m1 l: T

1 V: O$ o2 k$ f9 `1.3 Hook into WordPress" F2 ~! T7 A; C, e* z
9 h7 g5 [5 |# _! \8 V2 i0 z( J6 d
箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准9 d! I9 X) |+ F5 H5 j( |
  1. add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣
2 V/ u: \3 B! V1 l, A1 A
1.4 皜祈岫凋誨蝣
! Q0 k) J# M+ r5 \5 [$ ?" s2 y
蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :
  1. [recent-posts]
銴鋆賭誨蝣
2. 脤凋誨蝣
2.1 凋誨蝣澆
凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻
閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼
撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 :
  1. function recent_posts_function($atts){
    * `5 R0 d1 G8 t/ C3 W
  2.         extract(shortcode_atts(array(0 ~. Q+ ~, P2 V3 p
  3.                 'posts' => 1,
    1 d, l. ~& ]* r- A% M
  4.         ), $atts));6 P: N3 ?4 A+ K
  5. ; P. Z+ {# }3 t* M' _( n
  6.         $return_string = '<ul>';$ Z* W3 T4 g" B# Q8 C) z
  7.         query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
    0 \8 w" y0 ~3 q
  8.         if (have_posts()) :% B- q/ `& n5 _1 I6 g* N
  9.                 while (have_posts()) : the_post();( H" |* U; _* d9 u2 _- O7 V* E
  10.                         $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';* e8 q% Y) e( L" A) S
  11.                 endwhile;
    3 f9 k8 v7 J8 X4 T5 m9 L
  12.         endif;
    ( A, U, X1 ]/ q
  13.         $return_string .= '</ul>';9 ~4 X; Y( ]/ J3 ~! p0 _3 m: O

  14. 0 t* D- n/ n' G' F) V
  15.         wp_reset_query();
    # W* z3 a* E% k
  16.         return $return_string;
    * K) ?* U6 v3 q/ G
  17. }
銴鋆賭誨蝣
憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :
  1. [recent-posts posts="5"]
銴鋆賭誨蝣
園*蝷箸潛閬賢 , 典停臭誑唳餈鈭蝭蝡銵
4 {' I6 _" U8 m4 X5 B4 ]. A
2.2 凋誨蝣潔葉憓摰
臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗
  1. function recent_posts_function($atts, $content = null) {
    2 v$ \3 B6 B, N2 r2 p. U' c2 J. k
  2.         extract(shortcode_atts(array(
    + A( \6 O3 k  r
  3.                 'posts' => 1,
    3 A3 w+ j8 q; e8 h' n
  4.         ), $atts));/ c2 v, j1 g+ a
  5. 1 ?# K! ~: X0 C/ B/ }
  6.         $return_string = '<h3>'.$content.'</h3>';
    : B1 C( ~. _0 S' J+ W& b
  7.         $return_string .= '<ul>';) X9 ]6 z. i! V6 Y9 p  s
  8.         query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));. s( k" @0 x7 ~
  9.         if (have_posts()) :4 e: e7 W7 |9 {6 R$ I* z' ]; Y' N
  10.                 while (have_posts()) : the_post();
    , @( j* ^" J% X( w: G
  11.                         $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
    : x- ~& C8 ~- h% f) }8 f6 B
  12.                 endwhile;3 T, f4 T* P- e, r3 J
  13.         endif;. e1 P: d4 |9 s" z) X! o1 F
  14.         $return_string .= '</ul>';
    ) Q7 Z6 s" _$ i

  15. / f  ^3 \! H3 }
  16.         wp_reset_query();
    . l, I+ ~- Y# y6 f/ E1 Q2 E6 C
  17.         return $return_string;
    ' p; i8 v, O' y% _% k) `9 c
  18. }
銴鋆賭誨蝣
蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝
  1. [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣
支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗

, @! s! m. h% q. o. g
3. 其遙雿唳寥*蝷箇凋誨蝣
3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣
閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛典臭誑撠甇斤撘蝣潭曉典撌亙
4 t0 T, [; I2 [1 r9 R, m
臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 :
  1. add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣
曉其閬靽格嫣遙雿嗡唳
, 瑟, 湧甈撠甇蝣箏圈*蝷
* B6 c) f/ z: ?% B
璅嚗刻隢銝剖函凋誨蝣潘
  1. add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣
冽閬銝剖函凋誨蝣潘
  1. add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣
4. TinyMCE蝺刻摩典凋誨蝣潭8 h$ F3 f+ z" L4 ^8 Y
0 z6 Z4 v+ o8 [5 [( F! @, I
嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯
閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗
  • 箸迨撱箇銝 JS 瑼獢
  • 閮餃JS隞
    : E7 W1 N$ C. r7 y$ j2 T- o4 H9 _, z
* X9 O1 O7 A& z& u7 a1 Z
4.1 JS隞嗡撱箇
JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗
  1. (function() {& `4 ^; Y9 Z3 t/ |/ q: P. j
  2.         tinymce.create('tinymce.plugins.recentposts', {
      a; a) Q, q& S' h0 t/ B4 @% P8 f0 e
  3.                 init : function(ed, url) {
    6 T! ]! w# [1 e; K6 D  z& x8 j; D0 m8 P
  4.                         ed.addButton('recentposts', {0 M9 j6 C$ c. `; v& M. G# @9 ]: z
  5.                                 title : 'Recent posts',
    1 \- X+ D& r3 y. H; g/ T9 p
  6.                                 image : url+'/recentpostsbutton.png',4 S5 y+ [: h. H* U
  7.                                 onclick : function() {
    7 J, [2 X$ |5 H
  8.                                         var posts = prompt("蝡賊", "1");
    ; X2 x- B. N5 q$ w9 s
  9.                                         var text = prompt("隢憛怠亙銵冽憿", "唳蝡");7 u: C! E! {; I6 T: _

  10. : s2 U4 j* V* @5 m0 B- T$ e
  11.                                         if (text != null && text != ''){
    - v, P- S! u* I2 b& i1 b0 s
  12.                                                 if (posts != null && posts != '')
    " M. F! V  a& q! t% U; ~0 e
  13.                                                 ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');
    3 C( M3 Z( K! {+ b; X
  14.                                         else6 }8 c0 w7 v. @
  15.                                                 ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');% |! i& V2 m3 h1 |/ ~0 W: A
  16.                                         }
    & ^: c; _0 q7 G1 |2 C" ~. F
  17.                                         else{
    + V: Q# J3 w/ m: j/ W: r0 O
  18.                                                 if (posts != null && posts != '')
    ' Y' I1 X! u0 ]5 ^) O& o6 O# w
  19.                                                         ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');, k5 _/ N  ?9 H# {" D
  20.                                                 else3 t9 i0 W5 U, Z* C* N
  21.                                                         ed.execCommand('mceInsertContent', false, '[recent-posts]');
    " ?( R4 c& x3 v
  22.                                         }- x" `1 E# ?6 a) i
  23.                                 }
    ' s; B/ I% T+ k+ ^& _2 U1 P8 h8 A7 S
  24.                         });. Y# [" s! f8 |/ O( X
  25.                 }," L- U9 X' L" M
  26.                 createControl : function(n, cm) {8 r: H4 {, [. f
  27.                         return null;, T; r/ h2 T" j$ K# U7 }( `
  28.                 },6 b/ C% F. R3 k/ [
  29.                 getInfo : function() {
    8 J/ w0 H1 i1 v  G9 @. u6 T' C; V
  30.                         return {
    ' V; \' ?5 s0 T0 d: H4 \
  31.                                 longname : "Recent Posts",# H; U9 f) K% o! H' L& ]
  32.                                 author : 'Specs',2 j+ u1 r3 S' u# u! |
  33.                                 authorurl : 'http://9iphp.com',
    . k0 B. L4 T6 P1 [! R  y2 w+ o
  34.                                 infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',$ `  N1 Z, A5 r0 K) F% a
  35.                                 version : "1.0"7 ~& d0 @) r# B6 i
  36.                         };" G$ Y2 ~) {( Q1 O/ c
  37.                 }/ l, C2 M4 {8 ~( X# a
  38.         });5 `- _6 y) k7 e) b/ J
  39.         tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);
    ) @( r5 ]$ h0 C# }
  40. })();
銴鋆賭誨蝣
澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨
onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉
敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准

' C) E: C, S7 m9 c0 Q- E
4.2 閮餃TinyMCE隞
% `; _) B8 y+ C

& Z' Y3 w8 e: A" j. v7 f
嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉
蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩
  1. function register_button( $buttons ) {
    $ g4 b4 m2 V  o1 w. \
  2.         array_push( $buttons, "|", "recentposts" );
    ! S0 S2 i: w8 E% c. U
  3.         return $buttons;
      I9 ^! j) W, C, w; F
  4. }
銴鋆賭誨蝣
蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅8 g& m# N$ M1 j1 W  U1 b
  1. function add_plugin( $plugin_array ) {
    % _, x. x" I: q
  2.         $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';
    4 ]9 d. Q- \* ^, T* b/ W, _# ^' n& O: V
  3.         return $plugin_array;
    : t% s  }: z( A0 L3 [3 I
  4. }
銴鋆賭誨蝣
銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁
: l( p4 Y) b7 ?" E
  1. function my_recent_posts_button() {
    ! C/ D9 G* Z+ I3 m$ i4 F6 G
  2.         if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {) P# n# M/ q5 c* j; ^% p
  3.                 return;% d9 y) s* z8 V3 L" z
  4.         }  [! ]* f- R( L3 `* A3 Q$ j
  5. 7 D& G# W& ^! l0 r6 {& r
  6.         if ( get_user_option('rich_editing') == 'true' ) {; n' L+ Z, p* W6 `4 I% v% S
  7.                 add_filter( 'mce_external_plugins', 'add_plugin' );! s3 E, k5 P" D. k2 F. E, j, p! Y
  8.                 add_filter( 'mce_buttons', 'register_button' );# D' z5 p/ G! R" g; M
  9.         } 4 @. {8 \7 _! T1 e( B  q$ Y
  10. }
銴鋆賭誨蝣
嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁; x5 `6 G# w3 H; `4 Z6 u/ s+ g
敺賣詨曉訖ordpress憪賣
  1. add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣

# |9 ~2 f6 [- W9 i7 W+ K
4.3 冽
, @" K0 e* ^" O
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽

! l* _6 D! I; [2 ]* a
暺嚗敶箏閰望霈頛詨交蝡賊嚗

$ v. X: B3 T/ E, R1 A; X2 C1 U
頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
% F- h5 z! x; v# R  T8 Y6 v3 i
貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗

/ o& ?  C; ?" Z/ ]
唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆
蝡: 1 2

雿輻券

祉蝛閬

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

QQ|撠暺撅||52AV璈A

GMT+8, 2026-3-29 19:59 , Processed in 0.085163 second(s), 20 queries .

蝯∠.撱

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

鋆貉憒 敹恍敺 餈銵