52AV璈A|52AV.ONE

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

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

[銴鋆賡包
頝唾唳摰璅撅
璅銝
潸” 2016-6-9 12:40:16 | 芰閰脖 |芰憭批 撣 |摨閬 |梯璅∪
WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)
$ u. r, s# R( B6 y' l
) H2 W2 I+ n/ i( g, fwordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐3 ~$ Y) B$ T. F% a
隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具6 _' G/ M/ @; j$ W5 z1 }- u6 u

- A# i8 j6 s1 h( Y凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :
4 V3 [/ @8 `. h' _) c' Y3 J
  1. [recent-posts]
銴鋆賭誨蝣
甇文 , 臭誑閮剖銝訾批嗆蝡賊 :
5 J# j; E  b0 x1 m
  1. [recent-posts posts="5"]
銴鋆賭誨蝣
甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :

% i' C' {. z" b# x& C
  1. [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣
9 L% V0 }9 n- E# F+ H5 o; Y9 M" l
$ u& T1 U( D) U& L
1. 蝪∪桃凋誨蝣
2 Z3 d. n* W" ^+ ^( @! _
祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :
6 l5 K3 Q/ _+ U( W" N$ S: L+ ]+ e9 y! o
  1. [recent-posts]
銴鋆賭誨蝣
閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :

  g2 I8 G- q, Y
  • 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
  • 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
  • 閮餃賣賊蝯針ordpressaction銝
    % w) s/ |& u' ], \" K, e
# i6 ^$ {  Q0 m" f) g
祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准
2 T0 Y1 x% ]: L2 i' H% l
1.1 撱箇餈賣
. m9 D. f; s+ g9 d  F" X: d( U" ^嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡& G: f% f- i' u
  1. function recent_posts_function() {
    - k! m% S/ T  C; }6 b7 ?( p
  2.         query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));
    & H% ^) u) K' ~* b3 P4 a' k
  3.         if (have_posts()) :
    1 A- F9 t/ B1 e; q  w$ ~, J9 }+ b
  4.                 while (have_posts()) : the_post();) k9 y4 a. R8 B/ W7 Z4 v
  5.                         $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';
    & g' e: p) P7 S8 H5 L6 B
  6.                 endwhile;
    + ~& A. d' t6 ^4 j. }, {
  7.         endif;
    - N& o# Y' N9 S7 ^: i9 V% ^" m: a
  8.         wp_reset_query();* T0 ^. U$ ~# m1 Z; ~  _- i% ~/ @
  9.         return $return_string;% q; Q: r& o7 A5 i4 D; {
  10. }
銴鋆賭誨蝣
憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯

; R2 k9 ]7 T2 J
6 j5 p% ~6 P: h
/ o+ W- W! W4 o; K
1.2 閮餃凋誨蝣2 _9 i; M* J: \& h
曉剁閮幃ordpress賣豢臭凋誨蝣潘
  1. function register_shortcodes(){
    2 G5 t9 y' S# g2 s' I
  2.         add_shortcode('recent-posts', 'recent_posts_function');
    ' l2 ~+ }! L: u
  3. }
銴鋆賭誨蝣
嗅冽蝡批捆銝凋蝙函凋誨蝣# t1 @1 k+ X( G8 |, P. w
  1. [recent-posts]
銴鋆賭誨蝣
撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴

; D+ p. b: S! P  o, }4 ?6 [6 c, i! \- U5 ]; P* b) I2 q
* x4 L7 s" P- S7 a" f: A
1.3 Hook into WordPress) i- ?! b4 s0 {9 D
6 F4 l. o  D; V' S$ n4 }
箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准
& i# v' ^0 [! \$ @5 E5 f& g: I
  1. add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣
/ q+ {$ B" B; Z
1.4 皜祈岫凋誨蝣

' m/ P/ P/ R+ w' {蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :
  1. [recent-posts]
銴鋆賭誨蝣
2. 脤凋誨蝣
2.1 凋誨蝣澆
凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻
閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼
撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 :
  1. function recent_posts_function($atts){
    . \% F/ X" b3 R4 M9 \% Y0 w
  2.         extract(shortcode_atts(array(6 Z4 J# O; f& H
  3.                 'posts' => 1,
    ; |9 K/ x& D0 E. V4 T/ \0 I: q& r
  4.         ), $atts));# s: w1 g3 c7 W& x) `2 M

  5. 9 k0 t1 x! p- m! W7 h" x1 T$ U
  6.         $return_string = '<ul>';
    ! Q9 u/ T9 {# @: g$ {$ O
  7.         query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));) ~$ Q1 h& e  y) y+ J
  8.         if (have_posts()) :
    0 w$ M2 @- t  Q2 w* f6 c$ p9 f9 e
  9.                 while (have_posts()) : the_post();* I, f. z3 q* U  o% `; ]. z3 g
  10.                         $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
    $ K% ?) F( g0 X3 ^' B3 M
  11.                 endwhile;2 s3 L, s* [5 A' H9 ~/ O# B/ E" ]
  12.         endif;
    ( v6 c4 M) y5 M- d; y, `
  13.         $return_string .= '</ul>';
    : H3 x7 i+ ]; a3 s# M/ q
  14. ! W2 ~7 B& R. |& C" e$ n+ Q' |
  15.         wp_reset_query();
    9 m- v# k: S& J& f
  16.         return $return_string;. G' u+ r* j! ]) S& C+ V2 N4 b# w
  17. }
銴鋆賭誨蝣
憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :
  1. [recent-posts posts="5"]
銴鋆賭誨蝣
園*蝷箸潛閬賢 , 典停臭誑唳餈鈭蝭蝡銵

9 v) M: L) O0 X3 F$ V8 U- p
2.2 凋誨蝣潔葉憓摰
臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗
  1. function recent_posts_function($atts, $content = null) {
    7 P' Z0 Z7 V+ y2 K# t
  2.         extract(shortcode_atts(array(
    - }; s3 }( S- E8 l. V; F# o
  3.                 'posts' => 1,9 U+ A& l0 r0 g2 @: o% W5 q. l1 O
  4.         ), $atts));
    ) w) H7 f) R, B& j

  5. - s$ i5 W6 f/ i
  6.         $return_string = '<h3>'.$content.'</h3>';
    " G+ U) X8 @5 L' i" @
  7.         $return_string .= '<ul>';$ K" j" q2 @( H
  8.         query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));) j) S6 G8 s6 J  \
  9.         if (have_posts()) :* T. c; w6 T+ _% ^
  10.                 while (have_posts()) : the_post();% @) i: e9 ]* L: l# @# i
  11.                         $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';  U& u  ]) {, C* X& [; p. z  X
  12.                 endwhile;
    / N' |$ R: u* ^" n
  13.         endif;
    9 \( d9 t  N# l% s- o
  14.         $return_string .= '</ul>';
    ) x& Y( {3 o) G2 i* `" G7 ~
  15. 1 _2 r# Y2 q! _5 L7 f" `" t2 n7 p+ K
  16.         wp_reset_query();
    % X' ?' R& Z# h3 t
  17.         return $return_string;
    8 t1 E$ w3 l# h# g/ I: B. @, @
  18. }
銴鋆賭誨蝣
蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝
  1. [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣
支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗
) l" X6 ]+ V- Y: c0 l1 y( {: q, B* Z
3. 其遙雿唳寥*蝷箇凋誨蝣
3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣
閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛典臭誑撠甇斤撘蝣潭曉典撌亙

2 U4 g' e' ~! ~, ?4 @3 N
臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 :
  1. add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣
曉其閬靽格嫣遙雿嗡唳
, 瑟, 湧甈撠甇蝣箏圈*蝷
0 i! o! @" U% e. T3 b. n; Z. M
璅嚗刻隢銝剖函凋誨蝣潘
  1. add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣
冽閬銝剖函凋誨蝣潘
  1. add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣
4. TinyMCE蝺刻摩典凋誨蝣潭0 G9 B# X% M" ~5 [2 T3 ]
: |# C: M6 G( A. W( ?4 o
嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯
閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗
  • 箸迨撱箇銝 JS 瑼獢
  • 閮餃JS隞
    5 ^6 F  ]& C8 q& z; _

4 |: o6 z8 s8 x7 J* n! E9 [' 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() {" s/ K% Z- d/ g) F# a
  2.         tinymce.create('tinymce.plugins.recentposts', {  d4 f! @6 i: K  @1 m) Z  }
  3.                 init : function(ed, url) {) ^, L: v; ?  \" v
  4.                         ed.addButton('recentposts', {
    1 L: A5 l1 o( {! d. I' C7 P
  5.                                 title : 'Recent posts',1 ?! h7 h1 O4 A2 c
  6.                                 image : url+'/recentpostsbutton.png',
    ( u3 e9 S/ U: d$ t
  7.                                 onclick : function() {
    / T/ l& j/ d; e' N( |6 z) R6 g
  8.                                         var posts = prompt("蝡賊", "1");
    3 h8 k1 M* @; W6 |+ T0 q2 T
  9.                                         var text = prompt("隢憛怠亙銵冽憿", "唳蝡");  n  b  W8 v' ]7 _( W7 D# ]

  10. ( z7 w+ ^) z  R3 w$ ~3 D* C+ J+ w
  11.                                         if (text != null && text != ''){: t$ K( |  g. @
  12.                                                 if (posts != null && posts != '')# j/ j! M; F$ V0 Y+ p
  13.                                                 ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');
    . \1 ^. [, h$ M) o
  14.                                         else
    6 r( a5 ^3 [3 \& s( o% V$ \6 A! P
  15.                                                 ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');( v: w0 H( f1 l. H$ f
  16.                                         }
    ; G" S, u' [' [
  17.                                         else{
    1 [6 b; o5 h( c
  18.                                                 if (posts != null && posts != '')  L! C+ {. h& p, f
  19.                                                         ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');
    ; x7 j: c6 A5 H4 y4 V6 m
  20.                                                 else
    4 T0 w! z* A, G5 E8 r9 n3 T
  21.                                                         ed.execCommand('mceInsertContent', false, '[recent-posts]');! N. e9 w' a9 R
  22.                                         }5 k9 h5 F% ]8 Z( M
  23.                                 }# l  @1 ~' K* n) @# M+ _) c, `
  24.                         });
    5 o  p7 }! G0 y+ x0 S! }7 h
  25.                 },
    8 f" `, x7 v7 {& ^+ a6 G' M7 h
  26.                 createControl : function(n, cm) {2 y" E3 N, r! h1 U/ n0 `9 {
  27.                         return null;. T$ ^% F( A: n! s% i7 @5 V" }
  28.                 },6 o# x" J% d  K% ^$ j6 V6 N
  29.                 getInfo : function() {
    , a' w" G& i3 h9 V
  30.                         return {
    5 L- R: `( F+ Z/ |+ a
  31.                                 longname : "Recent Posts",
    : y# h' |0 x; S! b
  32.                                 author : 'Specs',+ ?& V5 p$ l) u3 g7 l
  33.                                 authorurl : 'http://9iphp.com',4 w# p( d& J& T5 |' I
  34.                                 infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',
    ) ?! ^7 W4 h5 L' t. O* U, S
  35.                                 version : "1.0"( K/ |4 P$ n" P5 `$ P! f
  36.                         };* x7 d3 M! e" m
  37.                 }! S1 M8 V: N  U. L, T
  38.         });
    4 a% @6 M0 D3 I& T$ I- L  @& U7 p" {
  39.         tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);
    0 f3 b5 {; S$ V1 ?6 M
  40. })();
銴鋆賭誨蝣
澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨
onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉
敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准

7 W+ k5 n% W3 E& H" d. m9 b% C
4.2 閮餃TinyMCE隞
4 e5 R1 e& H2 V! W2 ?6 ]" [
) n8 G/ q3 ?- }3 g" v
嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉
蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩
  1. function register_button( $buttons ) {
    5 N+ y2 @3 b  d* ^! d/ l
  2.         array_push( $buttons, "|", "recentposts" );
    4 L" o4 q6 o8 d* b+ O7 \% r
  3.         return $buttons;
    " E: c: o; s. J' t! N% r
  4. }
銴鋆賭誨蝣
蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅
! g) L. ]5 o9 m- l: f
  1. function add_plugin( $plugin_array ) {
    , N% E, {7 G- Q9 s2 w' w8 _
  2.         $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';6 T  t$ _0 Y- I) B6 Q  `
  3.         return $plugin_array;) o' ]4 K8 q, Z! M! I+ M; Q$ U0 t
  4. }
銴鋆賭誨蝣
銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁
6 i5 c/ i! r5 `6 ]) g1 b! X9 ]3 l
  1. function my_recent_posts_button() {
    ' K9 r* E* u- Q! Q. o9 G
  2.         if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {' Z+ `* E4 e8 L
  3.                 return;
    ! t/ d' K) F5 A, u5 b5 l4 [
  4.         }7 O: n. m0 h' t
  5. 9 B' E- P% s& a  Q' S
  6.         if ( get_user_option('rich_editing') == 'true' ) {+ G) [+ x$ y( k! h4 Y+ ]7 f
  7.                 add_filter( 'mce_external_plugins', 'add_plugin' );
    $ Y- Z' L- e5 ?& C+ W
  8.                 add_filter( 'mce_buttons', 'register_button' );
    3 L% u7 J! q+ |5 x% a6 i
  9.         }
    5 O$ m% F, T. H8 O8 s
  10. }
銴鋆賭誨蝣
嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁
% q& V# z( E  Z2 w: f6 n敺賣詨曉訖ordpress憪賣
  1. add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣
+ O# D. l4 C5 x7 `9 y
4.3 冽

- `; P7 v( ~1 [+ o. @
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
% R+ U2 N1 ^! `- \* c4 Z
暺嚗敶箏閰望霈頛詨交蝡賊嚗

9 K: O! f6 I6 @/ e) g* S# c
頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
8 q! ~# V1 X8 o# V. I" h2 B( g& Z
貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗

8 k% L7 m. j6 ?* u0 Q! ?
唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆
蝡: 1 2

雿輻券

祉蝛閬

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

QQ|撠暺撅||52AV璈A

GMT+8, 2026-1-31 17:46 , Processed in 0.072816 second(s), 22 queries .

蝯∠.撱

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

鋆貉憒 敹恍敺 餈銵