52AV璈A|52AV.ONE

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

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

[銴鋆賡包
頝唾唳摰璅撅
璅銝
潸” 2016-6-9 12:40:16 | 芰閰脖 |芰憭批 撣 |摨閬 |梯璅∪
WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)) y6 J# e0 _2 i

& B* ^* z- k5 o3 a0 o# cwordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐$ s1 t. l; a; ?$ Y( Z7 V
隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具
$ U% u- n# N% B7 \5 @
2 M+ K4 N" z; [; x6 T8 Y凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :" y3 U; q9 v* F, ^. G' U
  1. [recent-posts]
銴鋆賭誨蝣
甇文 , 臭誑閮剖銝訾批嗆蝡賊 :

4 N/ l$ V& a& ?
  1. [recent-posts posts="5"]
銴鋆賭誨蝣
甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :
5 [  k( N) b2 ?4 X) v9 W# {
  1. [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣
' L5 f# l& N5 ?2 R- y* {
, Y2 `$ W8 }/ t0 [2 O
1. 蝪∪桃凋誨蝣

# f. u( H: ~+ z. W1 \祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :8 a5 ]: U7 h1 Y5 Q$ D
  1. [recent-posts]
銴鋆賭誨蝣
閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :
6 F/ {2 W2 v" l: F
  • 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
  • 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
  • 閮餃賣賊蝯針ordpressaction銝9 I9 `" R) C( G* i- q' T

) j) r9 P) }% i  m+ L- I
祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准
0 I4 B+ S3 J+ K5 e
1.1 撱箇餈賣
! Y) |' a% O% F' E) a嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡
( z& u, ]7 O# a9 S9 g( P
  1. function recent_posts_function() {
    2 V% X6 S: p6 w5 {1 Y( x" l
  2.         query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));
    4 L, m% R0 i9 Z0 F9 A* }- W
  3.         if (have_posts()) :7 f* h! M( c2 m
  4.                 while (have_posts()) : the_post();) T: a9 E  _+ E- e
  5.                         $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';
    $ X+ a$ L, H  k; H4 c
  6.                 endwhile;
    ! R, u3 q) o$ g, w
  7.         endif;
    # i6 A$ e4 V( P! y8 S7 ~
  8.         wp_reset_query();4 s1 _7 `) g; G( g
  9.         return $return_string;8 g/ _" U& s- G. ?" c
  10. }
銴鋆賭誨蝣
憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯

6 {3 E0 L3 C' f# s4 d, ]* G. d% R3 _; p5 j1 k7 ^/ i
' ]7 m9 l& K5 r7 z  b
1.2 閮餃凋誨蝣
( w6 n0 X8 ~, a3 e. L曉剁閮幃ordpress賣豢臭凋誨蝣潘
  1. function register_shortcodes(){
    - d8 M) Q% |" k. \
  2.         add_shortcode('recent-posts', 'recent_posts_function');
    ! j5 D/ E, I7 G. J% w9 \
  3. }
銴鋆賭誨蝣
嗅冽蝡批捆銝凋蝙函凋誨蝣$ l* ^$ s/ c# `+ D  h
  1. [recent-posts]
銴鋆賭誨蝣
撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴

& [% E3 a% m8 S. e- m" M  @
+ y! V! r7 S" R

# V. A# j0 _- k1.3 Hook into WordPress6 J& Q' n$ @, v" V
% w2 O0 v0 v" y8 y" `, Y
箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准" g8 t0 E5 F2 V' R- N2 x( ?, u
  1. add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣

. l. O& U2 i+ [1.4 皜祈岫凋誨蝣

: L) @" H3 R1 F# u' @" u. D蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :
  1. [recent-posts]
銴鋆賭誨蝣
2. 脤凋誨蝣
2.1 凋誨蝣澆
凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻
閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼
撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 :
  1. function recent_posts_function($atts){( T/ Q7 `% l6 _: O; S8 b
  2.         extract(shortcode_atts(array(, {: o9 Z/ p2 M- J
  3.                 'posts' => 1,
    7 {  r; [3 T# G
  4.         ), $atts));% W; z( L" Q( G6 j3 y. J* p

  5. , Z+ o6 D3 }2 d7 k3 ^- }2 l' U
  6.         $return_string = '<ul>';& G% y; m  ?+ U* X/ y/ b7 ?1 A
  7.         query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));# B+ D: z$ A3 O: w/ f
  8.         if (have_posts()) :- H( a5 O6 K# _0 @
  9.                 while (have_posts()) : the_post();; h4 h8 ^1 V" N
  10.                         $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
    ' S8 M' Q8 J( J. y* ]% D# f
  11.                 endwhile;
    4 K' N- O& t9 K) S
  12.         endif;
    ' o, u4 y  M1 G  F5 b0 h) Q: q4 p
  13.         $return_string .= '</ul>';
    4 f) h; K; C, d1 r( c1 T* L2 K
  14. 4 {; ~5 G* X6 Y- c4 o7 I. q
  15.         wp_reset_query();; \# b# h" T' L! R7 g: I; P
  16.         return $return_string;# q1 D+ w! p$ u
  17. }
銴鋆賭誨蝣
憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :
  1. [recent-posts posts="5"]
銴鋆賭誨蝣
園*蝷箸潛閬賢 , 典停臭誑唳餈鈭蝭蝡銵

- C) B  C) s3 P9 Z. m
2.2 凋誨蝣潔葉憓摰
臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗
  1. function recent_posts_function($atts, $content = null) {* h7 A/ {0 s' O) @! G# v
  2.         extract(shortcode_atts(array(
    1 n. Y9 [" p5 o' G
  3.                 'posts' => 1,
    $ x+ }6 k5 b, S( b
  4.         ), $atts));
    1 O# |( s( J* f4 C% T8 k, ^

  5. 3 y$ N( B: }1 T, D  k7 T* T
  6.         $return_string = '<h3>'.$content.'</h3>';- [" d! B6 z# ~  f
  7.         $return_string .= '<ul>';. L& D8 w$ U2 Z* t! {% y2 X
  8.         query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
    % g; z) }3 ?/ `1 P" V
  9.         if (have_posts()) :/ q: e% r2 G7 K1 A( a# H
  10.                 while (have_posts()) : the_post();; h. ?' S9 L% s
  11.                         $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
    $ Q( \# x9 ~* ?: G
  12.                 endwhile;4 D6 h+ p, s$ F
  13.         endif;; A- j4 E' _8 q7 a* v
  14.         $return_string .= '</ul>';3 L" h9 }3 ^+ d$ r* H; Z
  15. - g1 a: }& C+ b' T- K7 c, ~; {
  16.         wp_reset_query();
      A3 `* ^$ b! l
  17.         return $return_string;
    / @& Z8 ?. I; ~! A/ I
  18. }
銴鋆賭誨蝣
蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝
  1. [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣
支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗
% v  j1 i% g3 g& Q& n  |' Q. G& m
3. 其遙雿唳寥*蝷箇凋誨蝣
3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣
閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛典臭誑撠甇斤撘蝣潭曉典撌亙

. U6 I) w( ]) v( D$ p9 x2 h' z
臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 :
  1. add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣
曉其閬靽格嫣遙雿嗡唳
, 瑟, 湧甈撠甇蝣箏圈*蝷

8 P8 D3 w- A; Y
璅嚗刻隢銝剖函凋誨蝣潘
  1. add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣
冽閬銝剖函凋誨蝣潘
  1. add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣
4. TinyMCE蝺刻摩典凋誨蝣潭
3 P* _. Y, j, y% M( V. z& B8 M( w$ E1 C. L! k/ a7 s6 r
嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯
閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗
  • 箸迨撱箇銝 JS 瑼獢
  • 閮餃JS隞
      A6 U, T0 a( x) }& E2 M% r0 D( h
) l9 u( l. l- L  Y. x, K$ s% w
4.1 JS隞嗡撱箇
JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗
  1. (function() {
    , Q0 ?9 i; d+ E) z; V$ q
  2.         tinymce.create('tinymce.plugins.recentposts', {' P8 m) C5 a, j( Q% `" a  w3 [! S  L" k
  3.                 init : function(ed, url) {6 @* H1 J8 ^- C. q' k! f
  4.                         ed.addButton('recentposts', {
    ' l: p' V5 \1 K& W) q+ T5 D- n
  5.                                 title : 'Recent posts'," g" j, S  U0 b/ K4 F
  6.                                 image : url+'/recentpostsbutton.png',
    + R7 `) j5 y, [. ~( p; f+ ?' d/ K( N
  7.                                 onclick : function() {* y: h+ v% N6 ?  Q$ n  R4 {
  8.                                         var posts = prompt("蝡賊", "1");) n  l/ _9 b! e: X* u
  9.                                         var text = prompt("隢憛怠亙銵冽憿", "唳蝡");
    0 _0 V2 k: s, X9 i) s0 i& x
  10. , q  d/ V2 N& t$ w* r
  11.                                         if (text != null && text != ''){8 R& @0 k" S% w* p# _+ o# s, J
  12.                                                 if (posts != null && posts != '')% x0 D9 D2 {1 S! a; ?+ _5 B4 |- u  k
  13.                                                 ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');9 q3 P6 A3 L/ R  k& W+ l( o
  14.                                         else
    8 d$ m* c# b/ M' i5 K
  15.                                                 ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');
    ( _, I4 F4 D# T+ X$ Z
  16.                                         }/ S9 p% P& |9 ^) \9 u, k1 ]& _
  17.                                         else{
    6 b& J2 d% i' _" Z# A4 F8 H
  18.                                                 if (posts != null && posts != '')
    : d) U2 n  [3 u2 ~
  19.                                                         ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');, X" ~  \( D! S& X! v  M( @
  20.                                                 else7 R, C* L6 V" }
  21.                                                         ed.execCommand('mceInsertContent', false, '[recent-posts]');
    8 P0 t  |' A8 V6 n9 D9 M/ }
  22.                                         }
    - o5 S$ j; F5 \' ^( a. |
  23.                                 }, T3 j2 n& w0 u! o
  24.                         });
    5 u) C1 d, A7 n; J
  25.                 },
    7 g- u6 c9 I+ v. p
  26.                 createControl : function(n, cm) {
    6 H+ V$ x4 l5 T
  27.                         return null;
    ) ?  ], m2 N+ Y) T3 F' h1 z: y( {
  28.                 },4 j4 n* H' \1 a" S( W8 d
  29.                 getInfo : function() {
    # i' s7 o1 f& w* w$ T1 d$ h* G( s' L
  30.                         return {3 {- Y1 ?  Q/ b( o, q! o; M
  31.                                 longname : "Recent Posts",7 B8 k3 }: Q5 r. y7 Z( B! M
  32.                                 author : 'Specs',
    : M& Y' G, c3 S: k$ u
  33.                                 authorurl : 'http://9iphp.com',5 ^. U, l4 a8 O- R
  34.                                 infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',1 M% |7 C" B; W! i) E% r
  35.                                 version : "1.0"
    ! z# ]+ I5 u' _, @% y" Z/ M! l- [
  36.                         };
    0 x1 k* b& e8 r' x2 n/ W% L  _
  37.                 }
    ! H' {& U7 M  K2 m2 D% r
  38.         });
    ! y% E. V0 f1 }% z9 _/ p8 f
  39.         tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);, \  x/ d) i( R8 P; f- o# r
  40. })();
銴鋆賭誨蝣
澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨
onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉
敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准
% S$ D9 |9 ?' g
4.2 閮餃TinyMCE隞- \8 I0 O4 ?1 X1 p% ^
$ d  i, g. p) P2 S
嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉
蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩
  1. function register_button( $buttons ) {( }0 P+ m3 I- t2 i+ P
  2.         array_push( $buttons, "|", "recentposts" );' M: n. f: q& W! g+ `
  3.         return $buttons;
    7 K6 ]  _" d9 h+ R6 c! j9 _
  4. }
銴鋆賭誨蝣
蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅% |  [5 y5 ]3 O/ ~) C. t" E
  1. function add_plugin( $plugin_array ) {
    $ r  o3 a- L. y  t1 J
  2.         $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';
    1 ]; k' s& q# m# I/ C- r% c
  3.         return $plugin_array;/ R' c6 [# q4 ]% H/ E6 o
  4. }
銴鋆賭誨蝣
銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁
+ g3 L  _2 Y" m
  1. function my_recent_posts_button() {
    - [7 G. }% S, f
  2.         if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {
    8 b2 w, ^/ l& B0 y: i- k
  3.                 return;
    ! ?- u, A7 ?1 H6 E9 z3 c6 S
  4.         }# a( o  d, X' x4 `% }6 J$ n( d

  5. ' o/ d1 V, |; q# r% Q; z0 b/ ?
  6.         if ( get_user_option('rich_editing') == 'true' ) {
    8 a- c- q( K4 a% u$ G
  7.                 add_filter( 'mce_external_plugins', 'add_plugin' );
    5 [2 \- a' Q/ I3 @1 l
  8.                 add_filter( 'mce_buttons', 'register_button' );
    ) X; V8 M. e# O# o* u8 o
  9.         } 2 P/ F. h- h1 f, K
  10. }
銴鋆賭誨蝣
嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁4 g# G3 i6 d. Q5 f$ u9 k$ S2 N
敺賣詨曉訖ordpress憪賣
  1. add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣

3 j' T7 I5 Y  t$ }6 d
4.3 冽
) n# T! n) U. Z: l5 a6 i* ]
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
( Y# \* I$ z+ D& k9 V2 G% h  q; D
暺嚗敶箏閰望霈頛詨交蝡賊嚗
6 E# L" O4 `- c4 v& t
頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗

9 M" ]5 r) {0 B+ p
貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗

) F) b6 s+ \0 c' B
唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆
蝡: 1 2

雿輻券

祉蝛閬

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

QQ|撠暺撅||52AV璈A

GMT+8, 2026-2-9 06:32 , Processed in 0.076132 second(s), 22 queries .

蝯∠.撱

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

鋆貉憒 敹恍敺 餈銵