52AV璈A|52AV.ONE

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

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

[銴鋆賡包
頝唾唳摰璅撅
璅銝
潸” 2016-6-9 12:40:16 | 芰閰脖 |芰憭批 撣 |摨閬 |梯璅∪
WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)
) F8 d* x! r# D8 A5 U
2 o5 d& j0 d9 S5 owordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐& P8 n- c2 T4 J/ ~6 }2 Z
隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具
% i! G- y9 r; k
7 C: W, e$ q! u2 C: h! u1 {( X凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :2 I0 x% ?  W& ?. c, B1 Q- A1 Q
  1. [recent-posts]
銴鋆賭誨蝣
甇文 , 臭誑閮剖銝訾批嗆蝡賊 :

: J) [, `  z7 k3 x
  1. [recent-posts posts="5"]
銴鋆賭誨蝣
甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :

- @8 p' V6 u  @
  1. [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣

3 s0 B6 b" ^6 F' V6 u

3 r) l. L. q+ n$ L% i1. 蝪∪桃凋誨蝣

: e' }: v7 w8 c6 H, Y+ Q祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :1 [5 Z* u6 }8 c( x, X; q: s9 C( I
  1. [recent-posts]
銴鋆賭誨蝣
閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :

% G: {( B2 V3 ^6 C+ ?
  • 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
  • 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
  • 閮餃賣賊蝯針ordpressaction銝4 u; u" i- b& G  Q* ~( M" \

+ R! v; h" }- @- M" h  V# i
祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准* M- b$ Q. c% H- J" p0 U
1.1 撱箇餈賣7 z) j$ ]5 z5 p6 ^, V& }# K; c9 W3 K; I
嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡' F/ Q; _5 q- k4 E) ~; d
  1. function recent_posts_function() {' m2 ]/ _( ]2 q0 n3 y* d  Z2 i# u8 j
  2.         query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));
    * m( B/ _* b7 k4 U; U
  3.         if (have_posts()) :0 s$ r4 V2 L2 w3 y) L8 W
  4.                 while (have_posts()) : the_post();
    4 S+ g& a* V  s. S1 R" G6 {/ v
  5.                         $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';
    ! x& q& `4 g" P: E5 I& A
  6.                 endwhile;
      r0 z+ M! d; n( x( W# Q& b
  7.         endif;
    % A' o1 Z! w# ~+ C( A
  8.         wp_reset_query();1 Z8 I# I+ L3 b, q
  9.         return $return_string;
    $ X3 G- J9 x" G# o# l  e7 t
  10. }
銴鋆賭誨蝣
憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯

" ?- E. M  m( H# x9 L1 ?9 u' u* D" D+ e

7 t( H, K; {6 u# h5 Z' ]& j1.2 閮餃凋誨蝣0 E6 P* t; H8 f
曉剁閮幃ordpress賣豢臭凋誨蝣潘
  1. function register_shortcodes(){, `3 N4 o) i% U+ n& u) |
  2.         add_shortcode('recent-posts', 'recent_posts_function');9 J& T' b8 S/ r$ {8 _$ p
  3. }
銴鋆賭誨蝣
嗅冽蝡批捆銝凋蝙函凋誨蝣
6 p7 `0 R( C  f0 c; b/ r* f
  1. [recent-posts]
銴鋆賭誨蝣
撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴

* F2 A  s( d: _- p4 o; m" h. O  g  E

$ p( f7 `: @% a7 _6 ~1.3 Hook into WordPress
# ^( K* E4 @* m* }  f- Q; y: v
箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准
$ f* D/ }5 g& [) @. V4 f2 x% y
  1. add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣
4 x3 r7 k% z+ n2 L" l# y! p; j
1.4 皜祈岫凋誨蝣
8 @: \$ m, {* g( K
蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :
  1. [recent-posts]
銴鋆賭誨蝣
2. 脤凋誨蝣
2.1 凋誨蝣澆
凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻
閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼
撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 :
  1. function recent_posts_function($atts){9 j2 d- ^" H8 r8 W% R
  2.         extract(shortcode_atts(array(
    $ @+ }5 ]% r! M: E
  3.                 'posts' => 1,
    8 t2 V/ {# f; k$ l3 ?; a
  4.         ), $atts));
    8 \3 E" x+ {3 a9 u% f, r6 U
  5. ; ?7 A$ a/ m2 _7 ^5 `* D. u0 P
  6.         $return_string = '<ul>';
      U, L2 S' w! M$ k1 }; o
  7.         query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));- T7 t# z2 a. k. V
  8.         if (have_posts()) :& w4 Q  t; {* c: u
  9.                 while (have_posts()) : the_post();* f. {( f; Z- j3 {# U! ^
  10.                         $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';% w! E' t; W, e- R( s# I
  11.                 endwhile;
    2 i# X8 m5 m% X
  12.         endif;
    9 Z& f3 Q$ c6 V7 i& h
  13.         $return_string .= '</ul>';
    " [. Q) F) m- }+ g! K& l* R

  14. : x- h$ k8 [8 Q
  15.         wp_reset_query();
    9 d1 w3 H0 j7 [3 `7 A
  16.         return $return_string;
    , x# X0 Q7 p) t4 A0 [8 Y
  17. }
銴鋆賭誨蝣
憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :
  1. [recent-posts posts="5"]
銴鋆賭誨蝣
園*蝷箸潛閬賢 , 典停臭誑唳餈鈭蝭蝡銵

! V2 s4 X# u* A; Q' o
2.2 凋誨蝣潔葉憓摰
臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗
  1. function recent_posts_function($atts, $content = null) {
    - v! I9 z5 @" ?: d3 P
  2.         extract(shortcode_atts(array(
    2 E( a, W" v  j# J
  3.                 'posts' => 1," f0 p/ t' }* V, R
  4.         ), $atts));
    . `9 E, @5 s+ J% ~# S. m4 N
  5. ( ^( C& [  z2 {; A0 n
  6.         $return_string = '<h3>'.$content.'</h3>';; a6 N6 b1 _% a" P, C& l. n
  7.         $return_string .= '<ul>';0 I- r/ @- j$ i! z* _
  8.         query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));; T( u7 O7 g. I2 d1 R
  9.         if (have_posts()) :
    0 H- @5 ~; k" Q3 T* U
  10.                 while (have_posts()) : the_post();% n8 |6 `* H2 k1 W
  11.                         $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
    5 w/ a/ s& Z8 a1 {1 }
  12.                 endwhile;
    * }8 S( L& o$ m0 W3 F; \
  13.         endif;" z0 Q. d# N" L$ M- S; l$ e8 N
  14.         $return_string .= '</ul>';
    . M7 I8 w9 |0 c6 ^" x" B6 h3 D) e" L

  15. . h% G. O1 M) L; A4 s$ s
  16.         wp_reset_query();2 m7 ^5 {+ m8 h3 m" [1 `. t
  17.         return $return_string;- B" Z0 T; H  d0 f9 O+ w4 O  u
  18. }
銴鋆賭誨蝣
蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝
  1. [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣
支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗
+ I- w% H4 g! j0 ?7 z
3. 其遙雿唳寥*蝷箇凋誨蝣
3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣
閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛典臭誑撠甇斤撘蝣潭曉典撌亙

9 x/ d' P* A5 p0 B* I$ O: X
臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 :
  1. add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣
曉其閬靽格嫣遙雿嗡唳
, 瑟, 湧甈撠甇蝣箏圈*蝷

( \; a* g' v% H$ B  ^$ X' r# n( E
璅嚗刻隢銝剖函凋誨蝣潘
  1. add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣
冽閬銝剖函凋誨蝣潘
  1. add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣
4. TinyMCE蝺刻摩典凋誨蝣潭/ P2 ?  U( B0 m; ]2 ?7 D

2 W& h: K; J$ ?' C. c  f/ A5 P嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯
閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗
  • 箸迨撱箇銝 JS 瑼獢
  • 閮餃JS隞
    " [: Y# L0 u+ X% t* Z; ?
# e) r* V5 _3 T' 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() {
    7 g* o) b; d5 w' O- \
  2.         tinymce.create('tinymce.plugins.recentposts', {
    + h3 _' M: Z/ j
  3.                 init : function(ed, url) {' ?9 r% [2 @6 o
  4.                         ed.addButton('recentposts', {( A6 I# ^1 h2 h9 l. ~  E' [& ^
  5.                                 title : 'Recent posts',# ]+ N$ X" z: |5 v1 P% o
  6.                                 image : url+'/recentpostsbutton.png',3 T$ a4 t2 z9 ~" d" p
  7.                                 onclick : function() {
    0 B* R2 }7 X( s; f' O
  8.                                         var posts = prompt("蝡賊", "1");
    # ^( ~; N/ `+ L1 ?
  9.                                         var text = prompt("隢憛怠亙銵冽憿", "唳蝡");6 {& v! K+ D* A) n7 \, A  b( k

  10. ' k' Q$ z5 g% ]1 [
  11.                                         if (text != null && text != ''){: r$ F$ L% g$ Y  a4 D9 j
  12.                                                 if (posts != null && posts != '')
    % q8 Z" e3 D; j* l2 i4 _
  13.                                                 ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');
    / n( P2 G/ j  h! u+ U' k. v- _7 w0 a1 W+ w
  14.                                         else
    , \. N5 \5 g: P8 p2 C" d; U& N( v. a: S
  15.                                                 ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');
    # k' r1 V5 j. _8 V
  16.                                         }; |% t& T' n2 T; g% I
  17.                                         else{
    $ [  l; ~* ^8 F
  18.                                                 if (posts != null && posts != '')
    ; P" H* R3 u4 R# {) t
  19.                                                         ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');
    ' A/ H7 W$ H" {# ~: E1 u# {, ]
  20.                                                 else
    " E" E! x, H9 M# v* ^
  21.                                                         ed.execCommand('mceInsertContent', false, '[recent-posts]');7 J- n! \, V$ i2 q5 h6 z
  22.                                         }
    . L% U9 N9 M0 z5 N6 _, t4 d
  23.                                 }
    7 W" T; ^8 Y" w- q" T& F
  24.                         });/ D" d6 J# U- p- N4 F5 C
  25.                 },0 t: q+ D+ S5 @& L
  26.                 createControl : function(n, cm) {5 T% \6 ]0 B2 W
  27.                         return null;% P! R( k, j) O9 |9 ^1 _
  28.                 },
    & [1 M5 ], M$ h1 p! V
  29.                 getInfo : function() {
    : J% _; |. N2 b& `) e2 l
  30.                         return {. r2 o) O7 C* |: Y( C3 O
  31.                                 longname : "Recent Posts",, F# ^1 O9 a5 ?/ ]& v' j* Z
  32.                                 author : 'Specs',
    - k& v& E! d+ V0 c
  33.                                 authorurl : 'http://9iphp.com',
    " u, `- U1 W% K( Z
  34.                                 infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',
    # k0 L; Z6 q, O. |. m; Y) B
  35.                                 version : "1.0"- q' U! s% T" ]
  36.                         };7 X* `3 Q: N/ _4 @
  37.                 }) e, l* Y. y! @& y
  38.         });+ J! d! [! O5 l' V' n4 h
  39.         tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);
    # k# A/ ~8 r9 y/ L% @4 I2 c% I3 L
  40. })();
銴鋆賭誨蝣
澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨
onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉
敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准
$ Y$ a" W$ D1 {( {+ s: x
4.2 閮餃TinyMCE隞
( q4 K$ _- {  J( g  q

- z0 t  }6 x% H& z' y% z
嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉
蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩
  1. function register_button( $buttons ) {
    7 N5 l% \, U- e
  2.         array_push( $buttons, "|", "recentposts" );  w# Z! g! _$ e
  3.         return $buttons;
    4 ?. `9 J! R* T
  4. }
銴鋆賭誨蝣
蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅2 ^+ Z4 P, q- F5 Y0 V
  1. function add_plugin( $plugin_array ) {
    ( J5 s' D/ [. h0 ?
  2.         $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';- ~3 p+ o1 n; G3 T: y( \
  3.         return $plugin_array;
    ! F5 O' @# H. F( R" b' }, A+ ]
  4. }
銴鋆賭誨蝣
銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁% i% g+ a2 R! T. J* C; e6 O/ Q- n
  1. function my_recent_posts_button() { 2 u% i( }9 @/ E7 K
  2.         if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {
    & A6 b  q. f2 N1 v
  3.                 return;
    - T3 v% A" U8 b# l
  4.         }
    + T% J  p- ^/ M, t/ \. {( \
  5. % F) B" i1 e3 k$ T
  6.         if ( get_user_option('rich_editing') == 'true' ) {) o0 T5 P7 }9 ^  K
  7.                 add_filter( 'mce_external_plugins', 'add_plugin' );9 |( z* _1 l" w. J3 h
  8.                 add_filter( 'mce_buttons', 'register_button' );
    8 ]. t1 `. h) a
  9.         }
    ; Y5 }( n) x9 @0 L5 j' \: _3 o
  10. }
銴鋆賭誨蝣
嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁; B4 v0 T+ b% U$ z9 K2 O
敺賣詨曉訖ordpress憪賣
  1. add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣

' `7 ]/ p% ]% o4 z; P) ~
4.3 冽

9 Y7 [" l' s0 E1 P9 [( N; C
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽

* e0 Q: t1 {" b2 Q+ K3 K
暺嚗敶箏閰望霈頛詨交蝡賊嚗

6 p2 K  z8 X8 b$ N5 t4 G: ^" ]
頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗

( _8 h" _7 g8 T& m* r  l- @
貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗

# `$ I0 v; b& D* V/ w: F- v
唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆
蝡: 1 2

雿輻券

祉蝛閬

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

QQ|撠暺撅||52AV璈A

GMT+8, 2026-1-30 23:58 , Processed in 0.073112 second(s), 22 queries .

蝯∠.撱

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

鋆貉憒 敹恍敺 餈銵