52AV璈A|52AV.ONE

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

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

[銴鋆賡包
頝唾唳摰璅撅
璅銝
潸” 2016-6-9 12:40:16 | 芰閰脖 |芰憭批 撣 |摨閬 |梯璅∪
WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)
; G1 s2 v; w0 P8 P& W8 B( q7 x; }. ]+ l- ?/ T! Q
wordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐/ T1 I. ~  n* D) ]5 ?$ M! U% F
隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具4 u" U) D% Z* y) b( x
+ e  a# t5 t4 G" r
凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :
, ]! |/ L; p: t; I
  1. [recent-posts]
銴鋆賭誨蝣
甇文 , 臭誑閮剖銝訾批嗆蝡賊 :
1 T1 M5 B/ V  f# ?! L9 H3 x. H# ^
  1. [recent-posts posts="5"]
銴鋆賭誨蝣
甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :

/ L. e2 N: d% \5 H0 r7 y/ I8 u
  1. [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣

  k. w) \5 N! v# t# g

, J5 M* x$ H* r6 ~% }, M7 D1. 蝪∪桃凋誨蝣
0 B% j* G4 ?4 j0 M
祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :
2 P# j, Z( E5 _* _- v
  1. [recent-posts]
銴鋆賭誨蝣
閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :

( P% N$ @& A* v: Y% W" P' t
  • 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
  • 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
  • 閮餃賣賊蝯針ordpressaction銝% B5 v8 c$ @9 O" [
7 l" a% W5 c( V% @+ |8 |3 p/ e
祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准& ]9 f4 E& q' [5 x4 ?$ M3 H
1.1 撱箇餈賣
  _( y  T/ i: q+ y) z/ ]0 I嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡" L3 [% t+ K. _* Q
  1. function recent_posts_function() {: Q7 B" X6 r) x
  2.         query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));
    9 u/ {$ i! d! t0 f
  3.         if (have_posts()) :
    * Y  y! Y- c/ i* W3 f8 T
  4.                 while (have_posts()) : the_post();
    9 K' P" K" ]& q8 l
  5.                         $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';
    5 }4 M2 m6 r2 A5 a
  6.                 endwhile;1 C% J7 n# N' A
  7.         endif;' j* t" r7 T/ @. |* e5 Z
  8.         wp_reset_query();% }# t; p  p  G; {9 b* j- c
  9.         return $return_string;' H/ T( a2 x, ?6 d2 d' n9 O
  10. }
銴鋆賭誨蝣
憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯
" O! `0 d# \4 N! C" v( L/ U

& U) V; K6 W! y8 l7 P( |
8 W$ o0 t1 `, W0 g# n, U( f  p
1.2 閮餃凋誨蝣
3 _' V$ a% r( f! {曉剁閮幃ordpress賣豢臭凋誨蝣潘
  1. function register_shortcodes(){
    4 h  S' I' H/ M5 c8 J: `
  2.         add_shortcode('recent-posts', 'recent_posts_function');
    0 T1 Q& v% T" v" I7 w/ d
  3. }
銴鋆賭誨蝣
嗅冽蝡批捆銝凋蝙函凋誨蝣( v( Y* ^4 g1 F
  1. [recent-posts]
銴鋆賭誨蝣
撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴
) L" e$ E3 J/ E  q( \

+ _; P6 k- u5 A$ W4 \* V0 [
, z; |: K# ]+ j: O( H9 Q. M$ V" o
1.3 Hook into WordPress
3 v1 c, j2 c; \3 W, @7 X. g# f1 l4 V" }) y
箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准$ W: I2 p3 _  q5 T1 u$ ^
  1. add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣
: ]4 P) m2 A" D
1.4 皜祈岫凋誨蝣

- G$ \( G" F% q1 y8 a蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :
  1. [recent-posts]
銴鋆賭誨蝣
2. 脤凋誨蝣
2.1 凋誨蝣澆
凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻
閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼
撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 :
  1. function recent_posts_function($atts){0 |9 O* M0 e; s. N+ Z
  2.         extract(shortcode_atts(array(
    & {4 i7 e- S  g5 O# a
  3.                 'posts' => 1,
    ' x# x3 d$ r# T- H+ T
  4.         ), $atts));
      q4 C* z9 e: @7 i+ p) t
  5. " n9 G( I/ `$ ~8 |) |8 A- E
  6.         $return_string = '<ul>';
    8 J  D6 K. Q6 L" k
  7.         query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));# ]- `" B* A5 z1 i4 `6 _) {' ^
  8.         if (have_posts()) :
    % B. K* X& z8 X
  9.                 while (have_posts()) : the_post();' F/ p/ e% m* ~3 l
  10.                         $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';; ~' G, E, U  F* W
  11.                 endwhile;( y( R" o% Z8 A5 W' U( d) ?
  12.         endif;1 e' R; j7 ^, u# G+ s6 Y" Z
  13.         $return_string .= '</ul>';
    ' z+ L( D+ u) ^1 p. @8 D; e$ y" |

  14. ! q  \3 c! _2 Y8 r
  15.         wp_reset_query();+ r: [) Q! s6 h8 c& g; M
  16.         return $return_string;
    % I. J: l9 c) ^6 h$ a: [# T  {
  17. }
銴鋆賭誨蝣
憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :
  1. [recent-posts posts="5"]
銴鋆賭誨蝣
園*蝷箸潛閬賢 , 典停臭誑唳餈鈭蝭蝡銵
4 P" D/ Z1 D: b$ l
2.2 凋誨蝣潔葉憓摰
臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗
  1. function recent_posts_function($atts, $content = null) {2 a% O4 j& ~0 G0 `7 F- ?
  2.         extract(shortcode_atts(array(, [' f& o& c( F& p6 l1 a
  3.                 'posts' => 1,
    : L& o. U( f, o3 t  c
  4.         ), $atts));
    , N# D. }6 G% n" H6 A

  5. $ A+ b8 ?: I6 Z2 @* n
  6.         $return_string = '<h3>'.$content.'</h3>';3 y1 i$ k; s/ W, B* v% Z9 H/ M* K& D
  7.         $return_string .= '<ul>';/ d& D! E8 W) J" B7 \: O/ p5 G7 S
  8.         query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));% [" Q2 H* n! l  t
  9.         if (have_posts()) :
    + [* R# [3 d; P1 W7 b
  10.                 while (have_posts()) : the_post();
    - Y1 R# Z7 x& G$ \0 s
  11.                         $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
    ) t/ J! I, J! K! n, X/ E9 N1 Z
  12.                 endwhile;
    1 V$ Q+ k+ q& t
  13.         endif;, Q4 o: T7 h5 g3 \, f! g2 H3 i
  14.         $return_string .= '</ul>';
    ) A2 D3 I/ d% g

  15. 4 r  @5 m8 L7 P( q) C2 d) Z0 C7 B
  16.         wp_reset_query();4 J/ E0 _1 q. h$ [: ]; f  ?+ a
  17.         return $return_string;
    $ i% L* e3 u8 s
  18. }
銴鋆賭誨蝣
蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝
  1. [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣
支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗
) u% Z; a5 C9 z& \2 g( W
3. 其遙雿唳寥*蝷箇凋誨蝣
3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣
閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛典臭誑撠甇斤撘蝣潭曉典撌亙
3 Y9 n# \3 Y' T% h
臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 :
  1. add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣
曉其閬靽格嫣遙雿嗡唳
, 瑟, 湧甈撠甇蝣箏圈*蝷

" ^/ [5 g+ \) @5 Q4 E$ O
璅嚗刻隢銝剖函凋誨蝣潘
  1. add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣
冽閬銝剖函凋誨蝣潘
  1. add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣
4. TinyMCE蝺刻摩典凋誨蝣潭" i6 q! S# e- @
. l& m* n. s' f0 y8 ?4 T1 e/ ~
嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯
閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗
  • 箸迨撱箇銝 JS 瑼獢
  • 閮餃JS隞
    2 |/ `- d) D, H' ~8 q" p, y
* a8 n  f) X4 B* P7 X
4.1 JS隞嗡撱箇
JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗
  1. (function() {
      M5 s; ~) n  q$ u' D
  2.         tinymce.create('tinymce.plugins.recentposts', {
    $ d7 f8 S: g: C4 d9 Q, z
  3.                 init : function(ed, url) {
    . Z; y( t& g4 \- b( y
  4.                         ed.addButton('recentposts', {) W1 J& W. k" V+ H" T; F% B
  5.                                 title : 'Recent posts',9 K4 O. X( i7 Q  m
  6.                                 image : url+'/recentpostsbutton.png',
    , U8 b9 `! N& ?# E5 l" s( U! K4 e
  7.                                 onclick : function() {
    " D7 b7 B% V: {+ ?2 W( I3 m
  8.                                         var posts = prompt("蝡賊", "1");
    % V+ p2 E1 D3 ]) a# `( w
  9.                                         var text = prompt("隢憛怠亙銵冽憿", "唳蝡");
    ( O, K0 X$ I( @$ b: N# t
  10. * m7 }' C7 P& j7 k) x
  11.                                         if (text != null && text != ''){# {) e$ k" W( n' t  @3 i1 L' ~* r
  12.                                                 if (posts != null && posts != '')7 Z/ L4 q, N& M: M
  13.                                                 ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');9 ~2 a& I2 }1 N6 G
  14.                                         else8 ?( X6 f# K. _6 k
  15.                                                 ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');( W- E6 d8 e# Y8 p
  16.                                         }
    3 Y, N; m* [! ]& t8 O9 @2 u- F, H, a
  17.                                         else{
    , r3 G) e# ]5 Z4 ]4 u. i1 K9 I
  18.                                                 if (posts != null && posts != '')8 ~/ r$ |' `$ G, v& M! I# R3 w, D
  19.                                                         ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');! h- W3 c4 o6 D! Q
  20.                                                 else7 d: d1 z) n2 t* V
  21.                                                         ed.execCommand('mceInsertContent', false, '[recent-posts]');* v# D: j2 f, t, X" D
  22.                                         }
    4 \- H: p5 {2 o2 P9 z. b5 K
  23.                                 }
    % {9 f% y5 w5 f" z
  24.                         });
    9 W6 G' a% f% |; E0 O; R9 F
  25.                 },+ B$ ^2 G+ i! f, K5 P( c& {
  26.                 createControl : function(n, cm) {- L/ D* D6 Z: C) Y  ^
  27.                         return null;
    6 @- I0 F. r7 E1 W5 ]! g, p
  28.                 },! j' o5 n' {1 K( i
  29.                 getInfo : function() {
    2 \( M- B6 y( B+ T* ]5 p
  30.                         return {
    3 u0 R" R0 D8 \
  31.                                 longname : "Recent Posts",
    6 g1 L+ F" E$ ]. `
  32.                                 author : 'Specs',+ w" c, E7 Q$ \
  33.                                 authorurl : 'http://9iphp.com',
    # ^. q% X6 n& r. N! {) O
  34.                                 infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',
    * R% R+ L8 X; A- r: U9 `: ?! r
  35.                                 version : "1.0"
    ; G3 h9 n/ y3 v  n. z
  36.                         };) x  y, N3 G; B" C0 l1 f2 C7 M
  37.                 }
    3 d; [. G) M2 c7 ~) _2 E
  38.         });
    " O5 ]; f! M0 U( P6 P" p% J5 r' D
  39.         tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);
    , _' u, w6 Y" a) {7 U9 I
  40. })();
銴鋆賭誨蝣
澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨
onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉
敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准
6 ^) y. B' [) n) H. A! H0 V
4.2 閮餃TinyMCE隞
3 c% R7 x: Y: z! S/ c. \# @

8 B; f# }" F# Y5 e
嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉
蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩
  1. function register_button( $buttons ) {0 P, V$ n8 u' V5 W2 {& U9 x
  2.         array_push( $buttons, "|", "recentposts" );
    6 w+ r! |& C: y
  3.         return $buttons;
    % @; l3 t  ]: O/ J& R, J
  4. }
銴鋆賭誨蝣
蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅; l7 x  v# w* l$ a
  1. function add_plugin( $plugin_array ) {
    4 L8 n" x4 i; B' q& Q
  2.         $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';2 J5 Y0 S/ a6 ^4 Q; w8 ^
  3.         return $plugin_array;
    , h3 A% }3 k+ A/ c; n
  4. }
銴鋆賭誨蝣
銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁# r- g$ M$ ]8 ~, Q% d. Y, ]. ~
  1. function my_recent_posts_button() {
    $ l# U* H5 a- M! b! Q
  2.         if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {2 e( t7 C% L& M( K+ J* R
  3.                 return;
    % W( y6 s1 w3 q) l$ b
  4.         }
    ' L) z& [) C. |7 U" h; y
  5. : r- s+ B" i- }$ U; R$ [
  6.         if ( get_user_option('rich_editing') == 'true' ) {8 g6 [/ U0 S" t! ]9 q
  7.                 add_filter( 'mce_external_plugins', 'add_plugin' );
    0 U9 K" e6 J1 C. T% c1 X" b$ V
  8.                 add_filter( 'mce_buttons', 'register_button' );& G2 k- a! Y/ Q/ N9 X& G* r
  9.         }   v0 s9 O6 w) K+ S1 `
  10. }
銴鋆賭誨蝣
嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁
' w- M9 {$ Y& \0 V1 {* E- D9 e( f敺賣詨曉訖ordpress憪賣
  1. add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣
( _1 M) }0 @' Y) R' L( N0 N9 y4 M
4.3 冽
' v: x* p: b8 y( x- ?
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽

4 d+ D7 z! t4 o) Y; e6 r- W* u& o
暺嚗敶箏閰望霈頛詨交蝡賊嚗

0 f0 B7 ]" O! f6 A1 a5 s; c& k
頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
8 j4 n. p  h) ^
貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗

( A5 b3 s( }4 y, I5 E: V+ N
唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆
蝡: 1 2

雿輻券

祉蝛閬

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

QQ|撠暺撅||52AV璈A

GMT+8, 2025-12-7 01:14 , Processed in 0.077985 second(s), 22 queries .

蝯∠.撱

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

鋆貉憒 敹恍敺 餈銵