52AV璈A|52AV.ONE

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

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

[銴鋆賡包
頝唾唳摰璅撅
璅銝
潸” 2016-6-9 12:40:16 | 芰閰脖 |芰憭批 撣 |摨閬 |梯璅∪
WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)8 _" a; O6 T5 W7 J* m

6 F' c' {( l" l. s* gwordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐6 [; y  g8 B- c; ^! @
隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具
. c# _7 X+ y$ y! Y8 I
: x  t" r+ a' o& e凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :
+ Q) q5 z6 }9 _
  1. [recent-posts]
銴鋆賭誨蝣
甇文 , 臭誑閮剖銝訾批嗆蝡賊 :

! ~% l8 g, h7 P- `$ A- L1 ?4 Y
  1. [recent-posts posts="5"]
銴鋆賭誨蝣
甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :
2 M7 X/ Y! c9 z
  1. [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣

/ E  f# h. r; E2 \" n% H# |
  F, z7 M% o8 Q+ @2 Q! C
1. 蝪∪桃凋誨蝣

5 l; c3 ]% ^3 a2 v祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :
, U% ?& E3 _3 I3 X+ [2 B
  1. [recent-posts]
銴鋆賭誨蝣
閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :
, O  V' |! h3 Q: R; \/ |9 Q+ c3 `
  • 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
  • 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
  • 閮餃賣賊蝯針ordpressaction銝! L# Q) K8 k9 B# W+ u

, w  q4 j$ R* I* ?% R8 ?2 F# Z. g
祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准* Y/ H9 V* w2 b
1.1 撱箇餈賣& l# A/ j, y, l3 d3 h; U
嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡
; @; Z- ^9 C( ^5 D  G6 o
  1. function recent_posts_function() {+ l" o9 O- r" L4 F- B
  2.         query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));) z, x5 i8 }. S- z( O3 D! Q
  3.         if (have_posts()) :- K: ^# B+ A) [2 [8 k# B0 N' M# p1 }
  4.                 while (have_posts()) : the_post();
    # b( A& c( @  b% T1 Y
  5.                         $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';/ j- Y$ x2 B, T
  6.                 endwhile;
      K' z! c" g6 e5 u! |
  7.         endif;; u6 ~' V8 o2 f+ \# L) {* b0 I
  8.         wp_reset_query();
    " ~9 X2 x7 O  L0 _  L+ X+ y8 _
  9.         return $return_string;9 F0 m0 E! F# H3 b! t# J
  10. }
銴鋆賭誨蝣
憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯

2 ~& O# n/ R6 X: w% S: j9 H' ]9 h

& U8 N, w, l2 J. `, z& f/ {0 U% t+ _1.2 閮餃凋誨蝣
' h7 A7 I  V+ s6 j' k曉剁閮幃ordpress賣豢臭凋誨蝣潘
  1. function register_shortcodes(){
      P/ _/ t& @1 Y$ m- A3 @9 |  d8 ~/ Y
  2.         add_shortcode('recent-posts', 'recent_posts_function');
    ' r8 B' O1 R5 v/ s
  3. }
銴鋆賭誨蝣
嗅冽蝡批捆銝凋蝙函凋誨蝣
8 F) Q8 H3 X8 t
  1. [recent-posts]
銴鋆賭誨蝣
撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴
) X" f4 _- u" v8 Z: p6 |* ]

, M! E6 |# m% n9 [/ m& t
& [" c- ~2 o1 N) A" N+ J8 a5 S
1.3 Hook into WordPress
$ ~$ i  n. b7 i8 u3 a- c% r5 O) F$ o- p: \
箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准
6 F- u& \! {, f0 i2 n0 b
  1. add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣
+ L# t1 X( b8 P( S
1.4 皜祈岫凋誨蝣
" ?# a% ~9 T1 J9 S. s
蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :
  1. [recent-posts]
銴鋆賭誨蝣
2. 脤凋誨蝣
2.1 凋誨蝣澆
凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻
閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼
撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 :
  1. function recent_posts_function($atts){
    3 ^- I  h6 l! ]% \; c
  2.         extract(shortcode_atts(array(
    6 A: ]( F- V0 y$ J7 I( [. h
  3.                 'posts' => 1,
    + q( E9 |! [2 c; e1 i
  4.         ), $atts));5 X% M. E# E$ e8 ~0 X
  5. " ]3 I' j3 Q( y; d  _0 K0 b6 M
  6.         $return_string = '<ul>';
    6 B& D: G+ |  J( a' F
  7.         query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));1 E( Y: l0 M: t, m" ]7 x
  8.         if (have_posts()) :8 v0 b. @! t! Q4 c/ [5 R
  9.                 while (have_posts()) : the_post();; ]1 M3 n! a' E$ ]
  10.                         $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';7 g% N1 m6 v5 X( G  u
  11.                 endwhile;# A$ P+ L2 q( b. @- _
  12.         endif;
    ' L" o2 ?6 Y% u) }: P. s& {
  13.         $return_string .= '</ul>';
    # a- s+ ?" O" i( ]% [
  14. " w7 u) e5 F8 N" a; G4 t- x
  15.         wp_reset_query();
    1 {! b! `7 \3 N8 x$ y; h* O# a
  16.         return $return_string;$ I' U  O( x' j$ w) l
  17. }
銴鋆賭誨蝣
憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :
  1. [recent-posts posts="5"]
銴鋆賭誨蝣
園*蝷箸潛閬賢 , 典停臭誑唳餈鈭蝭蝡銵
  V4 _) A3 ?* M4 G4 W7 n6 a
2.2 凋誨蝣潔葉憓摰
臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗
  1. function recent_posts_function($atts, $content = null) {4 q* z5 D; A; o( \' H
  2.         extract(shortcode_atts(array(& s9 p9 K6 [+ t& b4 ^
  3.                 'posts' => 1,
    9 D9 G6 P/ U: z( ~# q9 `
  4.         ), $atts));, m5 Z' ~" N; d- d" @
  5. * {" ?2 T2 J! c! J5 w* }) J
  6.         $return_string = '<h3>'.$content.'</h3>';
    9 t' ]- d+ X/ b* g: u0 m
  7.         $return_string .= '<ul>';) d! ]4 ]+ |" l6 c/ K, S5 ~
  8.         query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));' t& S. a+ ^% z+ Q* Y* g
  9.         if (have_posts()) :
    1 v- M0 U. x0 X' w  E, e6 M
  10.                 while (have_posts()) : the_post();/ W3 ]- N* D9 F4 X: b
  11.                         $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
    6 i* q# [  p/ f
  12.                 endwhile;
    0 w; ?( F/ l" m) G
  13.         endif;$ l) @) {. F" f
  14.         $return_string .= '</ul>';  y% t* j3 Y- I2 ^
  15.   Z/ y5 q6 }$ m1 o& t
  16.         wp_reset_query();8 R6 ]* {/ u4 q
  17.         return $return_string;- _3 k& ^8 e. s  z
  18. }
銴鋆賭誨蝣
蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝
  1. [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣
支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗

1 ^9 h- v$ {% S3 h- P/ a' A% h
3. 其遙雿唳寥*蝷箇凋誨蝣
3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣
閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛典臭誑撠甇斤撘蝣潭曉典撌亙

  a7 d# r0 N6 \
臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 :
  1. add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣
曉其閬靽格嫣遙雿嗡唳
, 瑟, 湧甈撠甇蝣箏圈*蝷
4 }$ U7 M/ S& \7 y
璅嚗刻隢銝剖函凋誨蝣潘
  1. add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣
冽閬銝剖函凋誨蝣潘
  1. add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣
4. TinyMCE蝺刻摩典凋誨蝣潭! o( b' w# |2 [6 i. K3 m

- D( s4 F( P4 q3 P0 P% I嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯
閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗
  • 箸迨撱箇銝 JS 瑼獢
  • 閮餃JS隞$ Q8 n* t/ c  g. F7 T/ M7 v

$ |  u3 b, [) |  X* B. 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() {  G  {2 h; e' m
  2.         tinymce.create('tinymce.plugins.recentposts', {6 \6 m* b, i# @* t2 N
  3.                 init : function(ed, url) {! h& K4 ^! B# ^+ ~/ a% R" L
  4.                         ed.addButton('recentposts', {+ Y( g* R. ~% s6 p) W: Q
  5.                                 title : 'Recent posts',
    * V: f8 T& n5 ^" v8 r% @
  6.                                 image : url+'/recentpostsbutton.png',
    & Q6 T, F) n* K4 @  R: A3 z3 P
  7.                                 onclick : function() {
    8 N! Y9 Z' H; ]5 r0 U' ?( |- |
  8.                                         var posts = prompt("蝡賊", "1");$ v9 }: h% ~  V# |1 b
  9.                                         var text = prompt("隢憛怠亙銵冽憿", "唳蝡");2 q7 O0 Y$ g2 k7 ~; P

  10. * m. S% s# H3 |6 ?- d
  11.                                         if (text != null && text != ''){
    * @* q: a& ~$ V) i- w
  12.                                                 if (posts != null && posts != '')
    2 Z: u# ?$ S# Y3 `/ H' f" }7 V& u
  13.                                                 ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');
    : F9 ]2 _5 D, y
  14.                                         else7 p: Q# ?. [  N2 Z6 k: Q1 m: ~
  15.                                                 ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');
    . ~( s& g2 U, c# l1 J, {1 i* |
  16.                                         }. i; p% G1 X  h( `( k# J, E$ j
  17.                                         else{# [& h& y$ y3 n' W, |# h" Q4 v
  18.                                                 if (posts != null && posts != '')
    0 O$ t4 i  p8 u. s, n
  19.                                                         ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');  {8 F2 h( E) K6 u0 M
  20.                                                 else4 I% o* f( i6 I1 s$ O  T% t
  21.                                                         ed.execCommand('mceInsertContent', false, '[recent-posts]');9 J4 W* I* z* v' J4 J+ U& t# z" c
  22.                                         }: |; d' q* W; x) U( W7 B/ Q" `4 X
  23.                                 }$ h; a" _& @5 T9 g9 |
  24.                         });
    " j; }" Y9 V. W$ f9 E
  25.                 },- b/ `8 Y: a% [" g
  26.                 createControl : function(n, cm) {
    . ]4 R7 r6 Q' H
  27.                         return null;' r6 X1 X: O: I4 u
  28.                 },
    ; ]1 q  |% L! a7 m& H* ~  @8 v
  29.                 getInfo : function() {
    4 x8 y2 j  ]$ a8 @
  30.                         return {
    + i/ R0 B0 Q+ G1 W4 A
  31.                                 longname : "Recent Posts",
    : Q6 X( C$ X- N4 q
  32.                                 author : 'Specs',
    9 L# m  i$ @0 K) M, S
  33.                                 authorurl : 'http://9iphp.com',
    . Z. C: u) t) U+ P
  34.                                 infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',$ V; B2 \# |+ s2 r0 l8 l' k
  35.                                 version : "1.0"% L1 J! }- x- I/ ~1 k& v3 {: Z
  36.                         };
    4 V8 A, v1 S: V! c. r; Z0 @5 A7 D
  37.                 }0 E: e' A0 q& z. v4 \7 Z7 B
  38.         });
    1 b* @; \0 X8 M) Z% F. `1 D' M6 u- r
  39.         tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);& \, w) m  |8 g- N. U5 G7 N
  40. })();
銴鋆賭誨蝣
澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨
onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉
敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准

' [5 |8 f8 j1 x& M! b' ^8 I
4.2 閮餃TinyMCE隞. J1 w5 x( {; ~9 B" L# t- ~
; C$ k1 H6 S8 f
嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉
蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩
  1. function register_button( $buttons ) {/ D0 U5 m( }; N" N) i  I# i0 }4 q
  2.         array_push( $buttons, "|", "recentposts" );$ `' n# M# p) M0 Y
  3.         return $buttons;
    . F; t- H+ V" N2 d$ V/ {
  4. }
銴鋆賭誨蝣
蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅
* {; m5 A( P8 x0 n, K' m6 p
  1. function add_plugin( $plugin_array ) {1 f2 A! M+ a7 {, P$ y5 `) r: V; d5 J
  2.         $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';  z! }8 K  B0 D" v+ ~/ J
  3.         return $plugin_array;! }5 ^+ G9 s9 K5 y
  4. }
銴鋆賭誨蝣
銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁( Q2 h, {# `9 A1 {' F: l! c
  1. function my_recent_posts_button() { . |; k" Z3 m0 g( B" P1 i, f/ n
  2.         if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {
    3 j, a# |+ \. k
  3.                 return;
    ( b! ]5 x: ~8 w7 A) S* @" f
  4.         }
    # f5 T; R9 Y- ^1 q9 S' k. b

  5. - O! n- h4 J! U$ g6 n
  6.         if ( get_user_option('rich_editing') == 'true' ) {
    + b8 W2 y0 ]& @6 s) T2 i
  7.                 add_filter( 'mce_external_plugins', 'add_plugin' );  n6 @! g0 R! ^* I0 \9 t
  8.                 add_filter( 'mce_buttons', 'register_button' );' s- U% R; ~1 J+ J/ D
  9.         } 8 J  A. i) B4 H: r9 V
  10. }
銴鋆賭誨蝣
嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁+ r% t3 [8 e* X6 @$ O3 p; P1 I
敺賣詨曉訖ordpress憪賣
  1. add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣

$ I5 H1 b9 W' V# i9 b5 T1 ?
4.3 冽
7 k+ @1 q; X7 m9 |# i1 t
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽

; J( A$ }: I) x$ N6 G6 b
暺嚗敶箏閰望霈頛詨交蝡賊嚗

" B( m8 G% N1 `. A5 h
頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗
  C: w+ }# ?) X
貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
$ }6 L, T" ^* ~0 _3 T
唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆
蝡: 1 2

雿輻券

祉蝛閬

BT蝳

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

QQ|撠暺撅||52AV璈A

GMT+8, 2024-5-30 15:10 , Processed in 0.070690 second(s), 22 queries .

蝯∠.撱

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

鋆貉憒 敹恍敺 餈銵