52AV璈A|52AV.ONE

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

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

[銴鋆賡包
頝唾唳摰璅撅
璅銝
潸” 2016-6-9 12:40:16 | 芰閰脖 |芰憭批 撣 |摨閬 |梯璅∪
WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)" l/ Q1 K& [" G$ o' y& C
: f2 r4 s5 Y+ L) ~
wordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐
) M$ E) E( ~$ i1 C) f( }# C; u4 g
隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具
7 o4 Y/ i8 m/ p! A
& D9 G8 V" n, D4 k( Q凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :
$ c4 s" H* |7 K6 |8 @1 K
  1. [recent-posts]
銴鋆賭誨蝣
甇文 , 臭誑閮剖銝訾批嗆蝡賊 :

/ Q6 c$ h* V* o( Y. f
  1. [recent-posts posts="5"]
銴鋆賭誨蝣
甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :
! S' P7 I( j9 W$ E+ u- V
  1. [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣

: ?! G( U, N8 k( j! ]- [- a5 I

( K9 U9 R* K$ z* `7 i1. 蝪∪桃凋誨蝣
- |6 \0 U2 n6 _7 I
祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :4 F) |( Q1 j+ d; Q' x5 a
  1. [recent-posts]
銴鋆賭誨蝣
閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :

# h! k5 O9 P! |1 G% {8 B
  • 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
  • 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
  • 閮餃賣賊蝯針ordpressaction銝
    . }. M/ L- o& v

7 h) S0 W3 A- z/ z9 ]
祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准/ E/ \9 x: L4 u
1.1 撱箇餈賣# [% d4 \0 M. R: L
嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡
8 Q* a3 v# d( b
  1. function recent_posts_function() {
    7 G$ n# T4 G' {, q0 [) m  o
  2.         query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));' e' w" a" G1 m! |
  3.         if (have_posts()) :
    ) f( N; {# o# s0 u2 s6 Q- r7 x
  4.                 while (have_posts()) : the_post();' m" [+ b- j( @0 \1 a
  5.                         $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';
    8 E9 p  E# u# Y: L7 _
  6.                 endwhile;( [2 X, ~$ c( b" M4 `  o
  7.         endif;, ]) q8 G5 {  \( O9 D7 a% F' Y
  8.         wp_reset_query();
    4 v, L6 x0 f% P1 e9 F
  9.         return $return_string;4 ?2 x3 F$ R1 g3 y6 n
  10. }
銴鋆賭誨蝣
憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯

) d; u( {1 ]8 f) s& j6 I: F  K5 M7 u  t. @7 H) P3 N

) F  n$ T# A' n; l  [2 C1.2 閮餃凋誨蝣
; ~1 u8 T# q' h) m# n0 E4 T曉剁閮幃ordpress賣豢臭凋誨蝣潘
  1. function register_shortcodes(){
    0 ~. s; R, W" f5 U
  2.         add_shortcode('recent-posts', 'recent_posts_function');
    $ @& D" }6 |' {$ N8 L# }: Q4 G
  3. }
銴鋆賭誨蝣
嗅冽蝡批捆銝凋蝙函凋誨蝣
! I( ~! d3 c! h8 o& z; ~
  1. [recent-posts]
銴鋆賭誨蝣
撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴

: r3 `0 u6 O  N$ _( H% Q( D/ A  e7 }) K! {& `/ E7 C, z+ t; _3 l% g" N8 r* d
! w7 T$ U2 `6 H5 a, d
1.3 Hook into WordPress
, {) _( w2 J4 \: V8 A4 w3 U7 Y9 G( t! M$ w8 M6 C
箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准
8 g' g9 g9 S0 p4 ]; |* y
  1. add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣
% X1 t. q1 }% P8 |' P7 w
1.4 皜祈岫凋誨蝣
4 C. X1 b; U1 w3 G: J/ X2 K0 w
蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :
  1. [recent-posts]
銴鋆賭誨蝣
2. 脤凋誨蝣
2.1 凋誨蝣澆
凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻
閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼
撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 :
  1. function recent_posts_function($atts){" p8 ]5 |0 j( v8 s
  2.         extract(shortcode_atts(array(
    ! H2 _  w3 T) n6 E4 q1 d9 e/ s! D' g
  3.                 'posts' => 1,* N* f; u$ G* u! |
  4.         ), $atts));
    + U, u2 V5 O* d8 {/ V

  5. / v9 w9 J6 \7 u) }
  6.         $return_string = '<ul>';! i6 _. M  S+ D# o
  7.         query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));+ n# e: v. N6 I6 b  \5 p
  8.         if (have_posts()) :7 @8 Q' F! _/ z
  9.                 while (have_posts()) : the_post();
    - T: O" k5 Y3 y6 Z+ h, f. W3 G
  10.                         $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';7 g1 d+ W: p( B% v7 t6 a  [- w
  11.                 endwhile;
    3 v3 Y1 f, K: V* J! E
  12.         endif;
    # Z# R' \. h( t- e
  13.         $return_string .= '</ul>';" A7 I7 E* F/ o1 ]

  14. - e3 V; T1 o2 w) L$ e. f
  15.         wp_reset_query();
    ) Z1 @2 `+ Y1 [! g: S- S% ~
  16.         return $return_string;
    - q& b3 h( N$ z) M- c
  17. }
銴鋆賭誨蝣
憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :
  1. [recent-posts posts="5"]
銴鋆賭誨蝣
園*蝷箸潛閬賢 , 典停臭誑唳餈鈭蝭蝡銵

# J5 u; L" q! g8 Q
2.2 凋誨蝣潔葉憓摰
臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗
  1. function recent_posts_function($atts, $content = null) {
    % S* B4 h! ^/ M; [8 X
  2.         extract(shortcode_atts(array(2 Y& `7 v, u) w+ S: R4 k
  3.                 'posts' => 1,( }# b  M' Y# W. o0 |
  4.         ), $atts));
    7 G4 O, l* m1 ^. i0 G! u, i
  5. - _* E3 j$ Y2 k; H
  6.         $return_string = '<h3>'.$content.'</h3>';
    & ?; J% s5 ~# T; x% j
  7.         $return_string .= '<ul>';( v+ ]9 ~! C* `* S5 j& V: a
  8.         query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
    4 x4 u# V% X. U8 H  O
  9.         if (have_posts()) :
    " g( E- r+ d1 y
  10.                 while (have_posts()) : the_post();
    * L/ c6 E7 H9 W0 }
  11.                         $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';" Y4 W2 F; L  ^
  12.                 endwhile;
    8 i2 N7 M' P! `3 d4 ^' x5 \! j
  13.         endif;9 w5 |" S( |- h$ u  t5 ~
  14.         $return_string .= '</ul>';
    2 f0 t/ L; W" i' N! J* n+ P
  15. " V* A  }8 F& e/ K
  16.         wp_reset_query();
    # f9 B2 ]# f) v9 N, `1 ]
  17.         return $return_string;) Q5 e& `$ M& v/ z
  18. }
銴鋆賭誨蝣
蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝
  1. [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣
支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗
; Q  h. h4 Z( E; X; R- |& |3 ?' U
3. 其遙雿唳寥*蝷箇凋誨蝣
3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣
閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛典臭誑撠甇斤撘蝣潭曉典撌亙

9 y5 f' {0 V7 [2 E8 o  p1 m* u
臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 :
  1. add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣
曉其閬靽格嫣遙雿嗡唳
, 瑟, 湧甈撠甇蝣箏圈*蝷

! o! ?5 D' U; B" m& S/ q
璅嚗刻隢銝剖函凋誨蝣潘
  1. add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣
冽閬銝剖函凋誨蝣潘
  1. add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣
4. TinyMCE蝺刻摩典凋誨蝣潭; f& B% h8 H+ S& z
/ i/ {7 ]1 Q4 R+ _/ j
嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯
閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗
  • 箸迨撱箇銝 JS 瑼獢
  • 閮餃JS隞
    * A2 q) [% y" t  ?; `. l! o/ d- _

3 d/ `* _' F" j$ f3 ^7 K" w8 l: s
4.1 JS隞嗡撱箇
JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗
  1. (function() {
    ( q- E: [% d' T5 i/ _* T
  2.         tinymce.create('tinymce.plugins.recentposts', {
    . L* }2 k# I. g/ e! s
  3.                 init : function(ed, url) {# g5 c  f* _/ R2 f( F5 x+ ~
  4.                         ed.addButton('recentposts', {" X+ B, N! Q# S5 L! @8 b
  5.                                 title : 'Recent posts',5 z$ h0 J1 |6 |; T
  6.                                 image : url+'/recentpostsbutton.png',
    4 A/ z" W: M5 R* t
  7.                                 onclick : function() {
    ' e7 l, B, |" [4 i
  8.                                         var posts = prompt("蝡賊", "1");& o' k( e. _+ d: S+ o
  9.                                         var text = prompt("隢憛怠亙銵冽憿", "唳蝡");4 K1 @0 B/ y) i3 a
  10. 5 _0 P: a: D! g" z+ c# c
  11.                                         if (text != null && text != ''){
    3 E6 S& m% G3 p1 |9 g0 `, F
  12.                                                 if (posts != null && posts != '')! X6 j: K) e9 ]# j! [
  13.                                                 ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');
    1 C! T' Q3 r0 U  E/ r: U/ G# Z( W
  14.                                         else
    0 v' S! X  F: g% T" y: _3 u
  15.                                                 ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');: w, D3 E2 M9 H" W' }/ h
  16.                                         }  S$ g7 E1 M- T$ v
  17.                                         else{
      R, V! x) N% n+ `& C( J
  18.                                                 if (posts != null && posts != ''), p) i8 K+ p9 A" J1 z2 U
  19.                                                         ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');# p' \, l0 I8 o
  20.                                                 else
    9 i. p. h) t6 h) D. m
  21.                                                         ed.execCommand('mceInsertContent', false, '[recent-posts]');
    ( @3 y: }' m8 E" s  Y' U
  22.                                         }
    ' l' `( a, x- u4 e+ m
  23.                                 }4 L* h$ W* a' ^" }! {$ X/ J& A
  24.                         });) W5 e5 @2 u( a/ q3 g  W6 M/ S
  25.                 },
    % J0 Q- A8 f( J, r$ x: o
  26.                 createControl : function(n, cm) {
      l  K" H7 M6 A1 q6 P- A" _
  27.                         return null;( c* K" |, m; s1 B3 M/ D* ?
  28.                 },
    * _9 \/ |1 k' e5 v! t0 Z$ \# W
  29.                 getInfo : function() {6 O* O) ~8 W9 S# z
  30.                         return {' H6 C6 V3 o% H5 ~& L8 ?) }
  31.                                 longname : "Recent Posts",
    - y6 K( M: Z/ }$ a; B+ |
  32.                                 author : 'Specs',( U, C7 e+ c# B0 Q7 r% N
  33.                                 authorurl : 'http://9iphp.com',
    4 {& O% U  p" l6 J* G  s
  34.                                 infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',3 W/ e$ p# t2 q3 ]1 g2 ~, A
  35.                                 version : "1.0"
    7 N! ?. T/ x) x  D( @
  36.                         };
    7 W  I& @& z" |5 T+ P! d$ I
  37.                 }
    " C5 ^% ^7 K) W
  38.         });9 Q% j. ?# \' G4 W
  39.         tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);
    3 L% h7 r' |' H. f  J
  40. })();
銴鋆賭誨蝣
澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨
onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉
敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准
' s3 v6 I! T' C1 a& }
4.2 閮餃TinyMCE隞5 Z! R& y+ w, B( ^

, r: V5 n1 u9 x. I2 j0 b% |+ R
嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉
蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩
  1. function register_button( $buttons ) {) C* G8 I3 B8 M2 a: c! X
  2.         array_push( $buttons, "|", "recentposts" );
    & U& o5 N% ^" Z8 B
  3.         return $buttons;
    7 j( t) Z( n( y) T" v
  4. }
銴鋆賭誨蝣
蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅
1 W$ \0 U1 P3 s' c. H  z
  1. function add_plugin( $plugin_array ) {
    " `2 N! `% G2 ]' x! [; v  n1 t
  2.         $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';3 Z9 B9 H1 t& C5 E
  3.         return $plugin_array;
    ( R  j" G3 @0 ^& B6 T
  4. }
銴鋆賭誨蝣
銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁
% R4 O* _; q% R, @
  1. function my_recent_posts_button() { $ X3 X( V, p% ], Y  N  t8 O# T+ Y
  2.         if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {
    * d0 S9 |6 E- G. _
  3.                 return;
    # V. g3 {0 \- v( `/ Q6 I
  4.         }+ N2 f- ]0 n' Q% r8 Y! n
  5. 6 a4 }6 y! A9 [: w
  6.         if ( get_user_option('rich_editing') == 'true' ) {# G" W8 w! K6 g
  7.                 add_filter( 'mce_external_plugins', 'add_plugin' );
    : t  K+ n: F0 }/ Y; P0 W' U
  8.                 add_filter( 'mce_buttons', 'register_button' );, ^2 r( s, t0 e* \* \2 g) P1 u
  9.         } & p' ?" t' M$ q9 R* I
  10. }
銴鋆賭誨蝣
嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁- l8 C( R8 a) S$ |- s+ m; p) h  A9 b
敺賣詨曉訖ordpress憪賣
  1. add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣

1 e% J4 ?% ^: [, Q: @9 o
4.3 冽
( L; I( M# E! N% }# f
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
7 i. O. j3 n2 ?9 D1 [; e
暺嚗敶箏閰望霈頛詨交蝡賊嚗
9 U# y( r, ~& z" p+ J5 S: P4 ^: _
頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗

1 p5 K4 t9 R. F
貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗

; U3 R+ `" V. M& `+ b. D
唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆
蝡: 1 2

雿輻券

祉蝛閬

撠蝝詨

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

QQ|撠暺撅||52AV璈A

GMT+8, 2025-12-5 22:26 , Processed in 0.078955 second(s), 22 queries .

蝯∠.撱

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

鋆貉憒 敹恍敺 餈銵