52AV璈A|52AV.ONE

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

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

[銴鋆賡包
頝唾唳摰璅撅
璅銝
潸” 2016-6-9 12:40:16 | 芰閰脖 |芰憭批 撣 |摨閬 |梯璅∪
WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)
: p& W3 Z/ ^5 P6 r  n( Z+ c' H' D9 v8 P
wordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐
+ x- j3 i5 `% [# J& m$ ~0 V. O
隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具
" K0 q! Z5 T2 A/ _3 g1 L; a
6 w6 i. T4 z9 K  V3 V凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :
( U7 m. R2 h+ R0 F
  1. [recent-posts]
銴鋆賭誨蝣
甇文 , 臭誑閮剖銝訾批嗆蝡賊 :

# g+ e, X* J9 g. j. f6 D8 x0 s& T
  1. [recent-posts posts="5"]
銴鋆賭誨蝣
甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :
/ I% `9 j) J( l# i
  1. [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣

5 T( x$ N9 x& l/ d; k

% _) f( P# M3 s. ?/ s; t2 D" s$ i1. 蝪∪桃凋誨蝣

0 m* ?. ]' ]8 W$ M( U1 Z. P# h祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :" N* N( x* S: q# M/ a
  1. [recent-posts]
銴鋆賭誨蝣
閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :
& r% ]7 L0 [! g" E7 U6 G4 K
  • 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
  • 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
  • 閮餃賣賊蝯針ordpressaction銝. T; O; |- R! C* T
# X* l6 @( }+ U- i( I( T. K& c0 J
祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准& h/ e, _6 P% q
1.1 撱箇餈賣
+ |0 F$ ]( |% e, g2 i% X' a( ]% Y嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡% ^/ I! ]+ c, k% ?, C/ r. L/ c* t
  1. function recent_posts_function() {
    / e& M' D' h9 b9 b
  2.         query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));" Z0 T; Y: q% H1 X8 N. ^. [
  3.         if (have_posts()) :
    ) {/ U4 Q9 U; d9 l
  4.                 while (have_posts()) : the_post();) X& t% j/ @  ]+ q2 y
  5.                         $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';8 w$ Z$ C8 P' J; b7 n, N
  6.                 endwhile;$ b  K: W; |0 N& s- L. M) Y8 Y
  7.         endif;6 d& W- P6 \$ N2 [6 S
  8.         wp_reset_query();) r+ o$ M: u9 S/ H6 Z
  9.         return $return_string;
    / A% Y. _/ R# ]
  10. }
銴鋆賭誨蝣
憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯

0 b/ f3 X; l) o+ p% \3 i+ g! m/ u+ l+ ?( w4 J  p
1 |# g8 ?% U; T0 x2 S
1.2 閮餃凋誨蝣5 b1 T5 V# M! R2 P
曉剁閮幃ordpress賣豢臭凋誨蝣潘
  1. function register_shortcodes(){
    6 K! F0 b: F  E+ ^$ ?4 Q2 C! }
  2.         add_shortcode('recent-posts', 'recent_posts_function');/ [. J2 ^8 Q  E$ J8 Y) D7 m7 B
  3. }
銴鋆賭誨蝣
嗅冽蝡批捆銝凋蝙函凋誨蝣
+ P- u0 o0 o3 a
  1. [recent-posts]
銴鋆賭誨蝣
撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴

2 p3 a7 m1 b5 Z+ w3 Q5 v8 {
/ K" E- u9 `: t! N1 A
7 M* g: N! K9 l' Y& }# h0 y
1.3 Hook into WordPress
2 D0 j( Y1 N8 B% S/ S/ Z/ S' y6 G% \. V. \
箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准3 j* _* L) P/ J7 X
  1. add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣

, ~' Y/ p0 p( [& }. @1.4 皜祈岫凋誨蝣
# P: x0 Z1 O# U
蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :
  1. [recent-posts]
銴鋆賭誨蝣
2. 脤凋誨蝣
2.1 凋誨蝣澆
凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻
閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼
撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 :
  1. function recent_posts_function($atts){
    # n$ R& f- f, e, v1 N' u
  2.         extract(shortcode_atts(array(
    - j, ?4 U' |- d2 V
  3.                 'posts' => 1,+ o4 x6 ]/ Y; P/ I! J6 n
  4.         ), $atts));
    - b! o  D2 h4 I# a9 ]& A, ]% V3 ]

  5. . N0 i9 M4 [  L. c4 V3 @
  6.         $return_string = '<ul>';/ @! r3 d3 m" Q: \% h+ w
  7.         query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
    7 E" }$ M" m# d* W+ o$ N: x
  8.         if (have_posts()) :
    6 V) @3 h) L2 F: w3 d
  9.                 while (have_posts()) : the_post();
    % ]6 W# W  S6 u0 _
  10.                         $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
    , V7 I% |4 i1 T8 v  H& j5 O, B
  11.                 endwhile;7 S) Y9 {- a2 _+ O8 y% v* `# j
  12.         endif;2 N$ o$ Z( O3 y' D! c+ T9 }
  13.         $return_string .= '</ul>';
    $ r5 \) h( L9 @  D7 b" y! N
  14. 6 x6 }0 `" q( }6 I8 s1 D5 Q
  15.         wp_reset_query();
    4 r0 U  G* X  X) D- k! C( J5 o
  16.         return $return_string;, L" I# E. q5 a9 ?
  17. }
銴鋆賭誨蝣
憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :
  1. [recent-posts posts="5"]
銴鋆賭誨蝣
園*蝷箸潛閬賢 , 典停臭誑唳餈鈭蝭蝡銵
7 r" G+ o; b& r& y0 s  z5 L3 l
2.2 凋誨蝣潔葉憓摰
臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗
  1. function recent_posts_function($atts, $content = null) {# Z6 o8 e5 x9 d4 _; P# Q
  2.         extract(shortcode_atts(array(
    1 w4 ^$ B; U& g% _, s& j9 N1 V
  3.                 'posts' => 1,
    / ~+ A# |* `, ]& X8 v/ Z
  4.         ), $atts));; P6 o# A. F+ @3 P

  5. % q4 a6 ~( P4 ]" A* A" D
  6.         $return_string = '<h3>'.$content.'</h3>';; l- X" Q) E0 W- c* z3 p
  7.         $return_string .= '<ul>';
    - |5 y' [% v4 q: X( t
  8.         query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));7 T% d* a  q/ `' H/ B
  9.         if (have_posts()) :5 a1 F0 s/ z" R5 L/ U
  10.                 while (have_posts()) : the_post();
    7 D- C! h5 ]2 d3 V- B; c; ?3 h
  11.                         $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';+ a; W7 F' R! A
  12.                 endwhile;; n( T5 o: A$ @
  13.         endif;
    7 s* F9 e8 ]; r' t
  14.         $return_string .= '</ul>';$ t% u' F. e- _9 }. C- H* P

  15. ( m. m! G( w; J2 z7 k! [( R9 B5 l
  16.         wp_reset_query();
    4 G  A& ^- A$ G4 c
  17.         return $return_string;- \/ P! j; {9 Q2 v0 O  v" V9 m- _( u  a
  18. }
銴鋆賭誨蝣
蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝
  1. [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣
支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗
- q. H2 R0 U1 R
3. 其遙雿唳寥*蝷箇凋誨蝣
3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣
閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛典臭誑撠甇斤撘蝣潭曉典撌亙
6 P3 ^& r- u+ x9 t
臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 :
  1. add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣
曉其閬靽格嫣遙雿嗡唳
, 瑟, 湧甈撠甇蝣箏圈*蝷

3 g' b; z! N' G* w0 q% p% j
璅嚗刻隢銝剖函凋誨蝣潘
  1. add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣
冽閬銝剖函凋誨蝣潘
  1. add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣
4. TinyMCE蝺刻摩典凋誨蝣潭. a- |6 p9 U/ d
, @- G7 Z! Y9 Q0 H+ |+ t
嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯
閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗
  • 箸迨撱箇銝 JS 瑼獢
  • 閮餃JS隞( k4 Z- A5 C# ]4 c3 k: C9 `
$ x' t* R* l. ]; O1 M" b
4.1 JS隞嗡撱箇
JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗
  1. (function() {4 C1 R% x3 B7 M5 Q
  2.         tinymce.create('tinymce.plugins.recentposts', {
    / x4 E/ t8 Q" k& m* c4 k) d1 l
  3.                 init : function(ed, url) {
    . Q$ t/ D, k- u% v" c$ ~
  4.                         ed.addButton('recentposts', {
    7 P* z% j- V! G" X, d9 I$ X
  5.                                 title : 'Recent posts',; t7 N2 u4 a, c
  6.                                 image : url+'/recentpostsbutton.png',
    ( ?( r' E4 i0 c! c/ h
  7.                                 onclick : function() {- h" s' z% ~8 k$ M$ \& R' m
  8.                                         var posts = prompt("蝡賊", "1");( T2 T5 q+ X. b
  9.                                         var text = prompt("隢憛怠亙銵冽憿", "唳蝡");+ F% |( g; U/ z( S) u" C) a5 Y

  10. : Z% c) P1 b( X' [4 P& i( E3 D$ a
  11.                                         if (text != null && text != ''){
    3 W1 s% @! d* t
  12.                                                 if (posts != null && posts != '')
    % Y, }1 m% }! G% G7 K3 X& L$ D2 g5 K  a; z
  13.                                                 ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');) U* I$ B# W2 p8 N6 j3 b- @
  14.                                         else* b! @# N$ ]) W, m6 O* t0 t
  15.                                                 ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');0 P4 J! }0 ]( t' m, z
  16.                                         }
    4 t. x0 _# I  Y" P/ M( ?
  17.                                         else{1 W/ I! \1 ]. b4 q! a
  18.                                                 if (posts != null && posts != '')+ \3 U/ _. s7 X% V  A, q1 \' C5 c
  19.                                                         ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');8 a$ U) a; s; c3 r
  20.                                                 else: f3 D" U, ]% B
  21.                                                         ed.execCommand('mceInsertContent', false, '[recent-posts]');7 e- d/ @5 p- V8 ]
  22.                                         }
    ! W% q2 d# b4 q! [; \
  23.                                 }
    . ^* V2 a2 C; |) g9 T8 a3 N) G
  24.                         });- w, x. f" ?# i
  25.                 },
    1 N$ @4 T( }# j8 K8 w( \
  26.                 createControl : function(n, cm) {& L' w9 r) m2 O+ H, v
  27.                         return null;/ ~7 ?3 g6 v) M1 x5 ]+ R/ k
  28.                 },' e1 X9 h0 M3 {* Q! p  l2 k
  29.                 getInfo : function() {% q; N& N0 k# ?/ C. b
  30.                         return {
    1 r" y, s" [4 G5 G  T
  31.                                 longname : "Recent Posts",
    % B* o9 `6 \/ k! d
  32.                                 author : 'Specs',2 J8 g  f0 H! q; p. E! m8 [  K5 w
  33.                                 authorurl : 'http://9iphp.com',
    0 F* U9 l" s) Z( p3 ^
  34.                                 infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',7 M' U( u1 x# ^2 C: m
  35.                                 version : "1.0"
    . I1 e9 C5 H& Q# S% Z; S; V
  36.                         };
    8 q% \$ M; N- r" ^8 I1 o
  37.                 }
    ) @0 k/ `) i, g" @: b4 h/ _
  38.         });
    ' j# e! R, Z0 B6 F* r
  39.         tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);/ e1 p) y( \& r5 c8 P
  40. })();
銴鋆賭誨蝣
澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨
onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉
敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准

1 \9 P" _4 A( h1 n/ g
4.2 閮餃TinyMCE隞
, w7 s2 K  b( z' U

* E' @- f' _- k$ C* i& i+ Z
嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉
蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩
  1. function register_button( $buttons ) {$ a! r" Y4 b* K0 F/ I% T& S
  2.         array_push( $buttons, "|", "recentposts" );
    " v" C8 A$ n' y0 ~- I( |: `
  3.         return $buttons;
    4 }" u3 F) z5 W- O3 G3 |
  4. }
銴鋆賭誨蝣
蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅9 ~$ A  \1 s8 a5 `9 w, H' P
  1. function add_plugin( $plugin_array ) {
    0 p* k. ^: y7 S. @
  2.         $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';
    4 [: G4 k# D) ^: r( w; W; t+ t
  3.         return $plugin_array;6 u7 k( m0 _) h0 u( B" l
  4. }
銴鋆賭誨蝣
銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁
1 {$ d) p4 ~* k# r% ?1 W  G  h
  1. function my_recent_posts_button() {
    " r* n/ Z  G9 E" I. W9 o
  2.         if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {/ L( M4 v' `% d' Q
  3.                 return;
    " J6 F5 B; {! F8 L. Y
  4.         }
    5 V% O, a! N- u
  5. - a( @4 q6 Z8 \" W
  6.         if ( get_user_option('rich_editing') == 'true' ) {! d. i, D" x5 x) p, P3 \% q8 [
  7.                 add_filter( 'mce_external_plugins', 'add_plugin' );
    # [5 g% z) |. `6 X9 k$ k
  8.                 add_filter( 'mce_buttons', 'register_button' );; L! a4 L! {0 t* J" b( z* F+ C) n
  9.         }
    + F7 T! ]! v& i' W" b, q
  10. }
銴鋆賭誨蝣
嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁
8 \& c$ Z8 V# M* I" L" j敺賣詨曉訖ordpress憪賣
  1. add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣

- U" C1 F$ x$ u; m% j5 `! m" E8 N
4.3 冽

- L( x3 S. N5 }9 C
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽

4 O3 x4 _; E; r. w+ W3 Z  ~
暺嚗敶箏閰望霈頛詨交蝡賊嚗

  t  S: k+ r' _' x- I/ s) _. }
頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
& _3 f0 r$ t8 d- i
貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗

  \# x/ V+ _, f, u7 `; D
唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆
蝡: 1 2

雿輻券

祉蝛閬

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

QQ|撠暺撅||52AV璈A

GMT+8, 2026-3-2 23:38 , Processed in 0.086156 second(s), 22 queries .

蝯∠.撱

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

鋆貉憒 敹恍敺 餈銵