52AV璈A|52AV.ONE

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

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

[銴鋆賡包
頝唾唳摰璅撅
璅銝
潸” 2016-6-9 12:40:16 | 芰閰脖 |芰憭批 撣 |摨閬 |梯璅∪
WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)5 E7 S! P0 h$ @& ~& o3 P  @4 ?
' w- y5 W5 `; h' c7 P9 J" Z
wordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐% P* G( j3 k. |6 e. G' _
隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具
1 G9 \8 p+ a6 O4 J+ J
0 r. Q; g0 Z* P% n凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :
, l4 J$ L. [3 a- j. P! `0 i
  1. [recent-posts]
銴鋆賭誨蝣
甇文 , 臭誑閮剖銝訾批嗆蝡賊 :
& m9 U1 [- u, M+ V. S) v' ?
  1. [recent-posts posts="5"]
銴鋆賭誨蝣
甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :
1 X- C- e! D# R+ e
  1. [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣

" d7 X0 e' m6 E8 @, x

8 q+ j$ l% k: M3 A9 R; ^1. 蝪∪桃凋誨蝣
( ^' H# l" r0 d: w7 L, I( C6 _' y
祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :
! i" t  d0 Q; Z2 h7 l$ e- l
  1. [recent-posts]
銴鋆賭誨蝣
閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :

# U! [9 k, ~* s$ {/ |
  • 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
  • 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
  • 閮餃賣賊蝯針ordpressaction銝- H" A4 T8 ?) g& w: U

; N. x3 y/ }+ ?
祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准
$ C/ x# g4 Q! N6 L
1.1 撱箇餈賣
' B/ S- D' Y7 H$ Y# q+ j嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡
- ]" Y; M% E- M! ^7 o
  1. function recent_posts_function() {: w# ~8 ]$ F; x- J3 {& D: j
  2.         query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));) V5 A4 R4 r; s4 F. J; R6 i
  3.         if (have_posts()) :
    ) t4 p$ p6 k0 i2 ?4 c7 X
  4.                 while (have_posts()) : the_post();
    ! K5 W1 e& n6 O0 w- g
  5.                         $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';- b6 w* X+ d5 u6 ^! _  i
  6.                 endwhile;
    ; }8 j2 G+ l4 v- L
  7.         endif;
    ( S7 a7 C- r7 z, g9 A; |# M! s' }+ M: ?2 O
  8.         wp_reset_query();1 t% S" W: `2 a# A( O0 t& w) J( J
  9.         return $return_string;# v  k9 K! p/ O: _( f
  10. }
銴鋆賭誨蝣
憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯

- h' C( t  C- R$ ^6 A7 W
/ D: @8 R) l0 V! Y

/ j7 ?" t0 h# E& y/ B& o1.2 閮餃凋誨蝣  \6 b( O3 L+ {7 g
曉剁閮幃ordpress賣豢臭凋誨蝣潘
  1. function register_shortcodes(){4 W! Z6 C) m8 r5 _5 R
  2.         add_shortcode('recent-posts', 'recent_posts_function');
    6 F: R8 g% K* {* r% Q4 M$ t' P
  3. }
銴鋆賭誨蝣
嗅冽蝡批捆銝凋蝙函凋誨蝣
% v+ Z  `: Q. k  X- Z: |6 F6 u- U
  1. [recent-posts]
銴鋆賭誨蝣
撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴
, ?) w& l3 h( A0 |' \* L& C
& n1 P/ ~* c7 f$ W5 B
% U" J2 |6 R- T( S$ v0 C: I
1.3 Hook into WordPress% a4 X& R# B/ d; g% Z* _0 c

6 b4 e2 C- ~7 [6 q箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准
; s+ W( Y7 f( c& T( y
  1. add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣
: D; x% N' A! K8 f, n- v! u- `$ B8 F
1.4 皜祈岫凋誨蝣

% y. l3 f% Y4 K6 N& }! y3 W蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :
  1. [recent-posts]
銴鋆賭誨蝣
2. 脤凋誨蝣
2.1 凋誨蝣澆
凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻
閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼
撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 :
  1. function recent_posts_function($atts){
    5 }! c* M6 K$ h9 p( Z) [  o* J
  2.         extract(shortcode_atts(array(
    # m0 x  Y7 g- B! U9 X
  3.                 'posts' => 1,2 C# U& ^, c) ~1 T2 i2 k) W2 z2 I
  4.         ), $atts));
    3 c; H8 g) c: V+ `  Y7 q
  5. , Y6 \) y& q* }2 K
  6.         $return_string = '<ul>';; P9 z2 X( d& @7 q. a% Q4 ~  B& V$ i
  7.         query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
    8 M5 w5 w" Z) j% ~" P
  8.         if (have_posts()) :  b: b9 l2 d! R0 h% W5 D7 y
  9.                 while (have_posts()) : the_post();6 X! E! P1 z! q0 j6 [
  10.                         $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
    0 s$ u( S0 u3 |) }: b
  11.                 endwhile;( n8 E! B. t, ?$ _' ]/ T
  12.         endif;# K. B0 b7 D( j( N% G2 Q4 D+ U
  13.         $return_string .= '</ul>';
    - D* c$ q) O) c. Y( K
  14. $ A, x4 B: R& o7 i8 F8 K
  15.         wp_reset_query();2 t# B: A% Y& c4 G
  16.         return $return_string;
    $ U* w$ I) s5 P- @/ g( n9 p8 h
  17. }
銴鋆賭誨蝣
憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :
  1. [recent-posts posts="5"]
銴鋆賭誨蝣
園*蝷箸潛閬賢 , 典停臭誑唳餈鈭蝭蝡銵

/ N5 n/ o* ~. W8 F- t- |7 K
2.2 凋誨蝣潔葉憓摰
臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗
  1. function recent_posts_function($atts, $content = null) {: X) w9 a* `- @8 F1 u
  2.         extract(shortcode_atts(array(0 a7 P/ D' E3 Z* M7 U
  3.                 'posts' => 1,& d# Z% L$ D; Q
  4.         ), $atts));
    ; l5 b& ?, R# ?
  5. 7 D! G* {9 f- z' J& w/ [
  6.         $return_string = '<h3>'.$content.'</h3>';
    % j' J  w  Z1 O9 p
  7.         $return_string .= '<ul>';
    - A- G4 G6 b7 L! n
  8.         query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
    8 r- N1 _& _0 H
  9.         if (have_posts()) :5 ~4 i) J& j! j
  10.                 while (have_posts()) : the_post();
    5 m# w5 o5 H& G/ e1 s, |3 d: P. \
  11.                         $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
    7 z0 x- p/ W; Z
  12.                 endwhile;
    - x3 i# q" y3 X# A6 q* t
  13.         endif;
    ) f  }  A6 |7 |9 s+ C. ?' S- [  T* ^
  14.         $return_string .= '</ul>';8 X& x; K5 R) [  Y: U- a

  15. & t7 a: ^! G# }! M9 X8 v
  16.         wp_reset_query();
    8 C8 V" W% |& ^; K) V
  17.         return $return_string;/ B2 @- y0 [8 t( D5 Q
  18. }
銴鋆賭誨蝣
蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝
  1. [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣
支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗

5 _% S1 N( k% Q, m4 O
3. 其遙雿唳寥*蝷箇凋誨蝣
3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣
閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛典臭誑撠甇斤撘蝣潭曉典撌亙

1 \$ d6 X, ~3 h; }
臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 :
  1. add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣
曉其閬靽格嫣遙雿嗡唳
, 瑟, 湧甈撠甇蝣箏圈*蝷
' T5 I7 U& b) w$ w5 v4 n) ?' C
璅嚗刻隢銝剖函凋誨蝣潘
  1. add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣
冽閬銝剖函凋誨蝣潘
  1. add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣
4. TinyMCE蝺刻摩典凋誨蝣潭1 r6 C3 a3 M# S) t  N3 R. A" X) A

% w. W5 L. x* o4 \2 _嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯
閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗
  • 箸迨撱箇銝 JS 瑼獢
  • 閮餃JS隞' M; X  q6 W+ X% h

. d, ~! H( z# f& N  B: I7 u/ E
4.1 JS隞嗡撱箇
JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗
  1. (function() {+ }) A' `8 b2 U' A
  2.         tinymce.create('tinymce.plugins.recentposts', {( D5 P" c: @4 l8 A; I
  3.                 init : function(ed, url) {
    % h4 p6 ?' P; u& E+ |
  4.                         ed.addButton('recentposts', {/ o9 ]' }- @3 }7 w+ P3 i+ E
  5.                                 title : 'Recent posts',
    2 N0 D& L- S: z. s4 h+ A1 s9 B2 s5 B
  6.                                 image : url+'/recentpostsbutton.png',
      j) T8 G  |, r9 P) k
  7.                                 onclick : function() {8 I5 [2 b' o* O7 W! W4 w  V5 C# N
  8.                                         var posts = prompt("蝡賊", "1");8 u$ W4 S! f* W( Z8 p2 o/ s! |
  9.                                         var text = prompt("隢憛怠亙銵冽憿", "唳蝡");; x8 b; {& B) G. ]( x1 j( ~

  10. + l4 i8 Z. V/ ~& P
  11.                                         if (text != null && text != ''){8 g4 g; o8 h( C$ C) L( G' v- V
  12.                                                 if (posts != null && posts != '')
    # ]8 E4 s% X4 n' D
  13.                                                 ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');
    ( O% ~7 t- z# p$ E$ d' Z
  14.                                         else$ a7 ?) `: y7 q
  15.                                                 ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');: C' i! F$ x* c. M' z# T
  16.                                         }  J' V4 g9 d2 c5 @% f! b  N" @! M
  17.                                         else{! e. o" b2 [' c/ I: C1 a9 z& ?
  18.                                                 if (posts != null && posts != '')
    * ~, [4 m2 s2 _
  19.                                                         ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');
    * u6 i9 U; G9 L6 X# Y& u) X5 z3 f
  20.                                                 else
    : O' S9 K* C. ^* g/ O5 }  X
  21.                                                         ed.execCommand('mceInsertContent', false, '[recent-posts]');: v7 F8 s8 r% a
  22.                                         }
    : `3 Y" f1 J' i: B% M+ N
  23.                                 }
    8 [# ~9 G6 z9 A& ?1 e  V) G
  24.                         });
    ( a3 F0 i4 J6 O2 l
  25.                 },) o* I* ]0 u! N% m  v
  26.                 createControl : function(n, cm) {
    4 f: V5 {! D3 i# M
  27.                         return null;
    - {( v( W3 P$ K$ ^" c! [7 u  d
  28.                 },
    : p4 v6 s% N, F; z
  29.                 getInfo : function() {
    3 c/ ?4 C% |4 ~7 v' W" W. X0 j
  30.                         return {" Y9 r% n8 R. M! D
  31.                                 longname : "Recent Posts",; R! }  w5 w! I8 V# v
  32.                                 author : 'Specs',! k4 ?, B9 Z/ d' C
  33.                                 authorurl : 'http://9iphp.com',
    0 \, f7 d9 S% D( ~8 }7 q$ k
  34.                                 infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',/ o0 S' n# ^/ \- S, Z& i
  35.                                 version : "1.0"3 I9 C. h( h2 D9 b: x
  36.                         };: P5 t0 m) O6 y# i
  37.                 }( p* H+ }0 R' u# E5 k# f) I+ B$ [/ i
  38.         });2 @# A% f- B1 y
  39.         tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);7 }) T& W6 V  G1 D! u
  40. })();
銴鋆賭誨蝣
澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨
onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉
敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准

3 j3 w) {3 f# ~# g2 \* x$ w& N
4.2 閮餃TinyMCE隞) d8 T6 b; q$ J8 M- ~, }  K9 e& n

: O/ v! d. ]' L% j0 E4 |. H
嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉
蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩
  1. function register_button( $buttons ) {! A" n& Y2 L. }2 _$ e
  2.         array_push( $buttons, "|", "recentposts" );& n' p$ W9 Z9 A* @' U
  3.         return $buttons;
    0 k0 E! ]# M3 h4 Q! o8 T
  4. }
銴鋆賭誨蝣
蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅
  e0 u( o' v) `: Y- }
  1. function add_plugin( $plugin_array ) {9 x3 c' ?3 {- Q; d0 c8 }
  2.         $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';1 k! f& J# n# ]6 O
  3.         return $plugin_array;
    , N: d* c. }  l* ]6 l# N7 e0 C0 [
  4. }
銴鋆賭誨蝣
銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁
/ ?2 a8 D' l2 ^. }0 t
  1. function my_recent_posts_button() {
    * l+ o+ x2 g. [8 Q2 {9 R1 J( d, Z
  2.         if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {; g, i0 Z5 O0 x& k$ {" e
  3.                 return;, r9 K% N) R' T, W
  4.         }
    2 x# J* j3 j+ i& `6 G) w

  5. : K6 h( G! ?3 ], d, L2 t
  6.         if ( get_user_option('rich_editing') == 'true' ) {
    * i5 U" Z# o6 C6 `) V& i0 \
  7.                 add_filter( 'mce_external_plugins', 'add_plugin' );
    : m  ^7 Q* c$ N: O( [, K  U/ w
  8.                 add_filter( 'mce_buttons', 'register_button' );$ l' A  E. {( c: e
  9.         } 2 l/ \* Z- t3 Y' j
  10. }
銴鋆賭誨蝣
嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁7 i& o) O8 i3 b. W) N5 c! z
敺賣詨曉訖ordpress憪賣
  1. add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣

" g& u0 e" Y5 d; {/ `2 T
4.3 冽

& ?) `1 t) X+ D. n& O
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽

3 Z( d* I9 |2 s( J) Q& E
暺嚗敶箏閰望霈頛詨交蝡賊嚗
0 [$ D2 w. i' _& t* B; d5 M! z
頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗

  ^* v- g; k% y* @0 V9 X3 Y6 i. U' U
貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
( K: q# }8 I5 J6 x. S1 P
唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆
蝡: 1 2

雿輻券

祉蝛閬

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

QQ|撠暺撅||52AV璈A

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

蝯∠.撱

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

鋆貉憒 敹恍敺 餈銵