52AV璈A|52AV.ONE

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

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

[銴鋆賡包
頝唾唳摰璅撅
璅銝
潸” 2016-6-9 12:40:16 | 芰閰脖 |芰憭批 撣 |摨閬 |梯璅∪
WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)
' p3 W( \! S# e9 K: m2 H: O: x
9 O, ?2 ^6 d+ q1 s3 Lwordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐
3 `2 m7 d1 i  R2 a; P" d
隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具/ v5 `3 _0 S" J0 {
# K3 ?+ B5 r' j$ Y( g+ j
凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :. ?9 F5 R% p% f
  1. [recent-posts]
銴鋆賭誨蝣
甇文 , 臭誑閮剖銝訾批嗆蝡賊 :
: B6 ?  t- m' J. e3 s$ _( u. L
  1. [recent-posts posts="5"]
銴鋆賭誨蝣
甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :
6 t0 X6 N/ C: |3 Z  v6 w
  1. [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣

. y$ u( r9 N9 Z6 c

% j; [4 U/ ^8 R. d, o3 v9 u1. 蝪∪桃凋誨蝣

) [" b5 D3 }* ~' J祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :4 h# F# m  P- i* J! b) y. c
  1. [recent-posts]
銴鋆賭誨蝣
閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :

2 H# r9 U- i) G, t9 g
  • 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
  • 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
  • 閮餃賣賊蝯針ordpressaction銝7 J2 w' F' J2 q; y8 j2 x( Q( ^

* I5 m+ b% X% W1 @( \5 k
祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准
3 B" T# L/ Z0 C% M* J7 Q1 b" o" U$ |
1.1 撱箇餈賣% x$ Y0 P  U/ G
嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡
. i( h/ [* L( f1 I* x# a
  1. function recent_posts_function() {
    2 w( p2 V1 S3 e( M% k- d. t
  2.         query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));1 I" K  S7 y+ V" w' X( D) t
  3.         if (have_posts()) :
    9 h; l7 s) a4 w9 _" O
  4.                 while (have_posts()) : the_post();# M9 G) u4 K1 M( c
  5.                         $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';
    * }0 @  E# K- R- y+ U# ]
  6.                 endwhile;
    ' D/ m" _8 E, a' n' J( N
  7.         endif;& Q) c. f1 U2 H: B/ P& }
  8.         wp_reset_query();
    ' ~5 H& d0 u% F( i4 f
  9.         return $return_string;
    ( u. E/ i8 l( ]- [$ P
  10. }
銴鋆賭誨蝣
憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯
: d& q% |" n+ U* f7 \% d

. e7 j- X( U( N& C
4 d! G2 Z, w% |+ [
1.2 閮餃凋誨蝣
' q6 ~* s' @# K; d1 N$ R3 w曉剁閮幃ordpress賣豢臭凋誨蝣潘
  1. function register_shortcodes(){2 Q6 g" {2 K9 X
  2.         add_shortcode('recent-posts', 'recent_posts_function');
    + a; J$ W3 `, N7 [% i9 [7 t6 L/ d9 X$ `
  3. }
銴鋆賭誨蝣
嗅冽蝡批捆銝凋蝙函凋誨蝣4 P% l* E. [5 a2 l7 X$ A
  1. [recent-posts]
銴鋆賭誨蝣
撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴
% l) `9 b0 E# U) l$ i; Z

8 `$ j6 s) B0 w" I+ ?, z6 x) s
/ g* h! J2 e1 Y) ?* U$ \+ g3 `
1.3 Hook into WordPress9 p1 a3 P4 Q8 k7 H! u

6 v3 c2 }( E6 n9 A6 J) N箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准
' f$ {  W# r1 L& x0 @" |3 b6 H
  1. add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣

! _' r- U# n8 M+ [! s8 q  O0 _1.4 皜祈岫凋誨蝣

8 Q% F1 j, {" U蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :
  1. [recent-posts]
銴鋆賭誨蝣
2. 脤凋誨蝣
2.1 凋誨蝣澆
凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻
閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼
撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 :
  1. function recent_posts_function($atts){
    8 Y& H2 [! E5 c' z
  2.         extract(shortcode_atts(array(+ S$ L; J, `) r( n% S
  3.                 'posts' => 1,( k9 W: V; G" S7 S: m
  4.         ), $atts));
    / _1 g( e5 R3 C" y0 u$ K. a7 V5 e
  5. 7 Y( h6 H1 D1 V
  6.         $return_string = '<ul>';
    ( o  V( c0 p, x) \1 `! r
  7.         query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));( x' e. n; G1 _5 i
  8.         if (have_posts()) :
      Q- x# q9 e; m) l
  9.                 while (have_posts()) : the_post();' r5 ^) h( w6 {# `# s" p1 u: |
  10.                         $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';4 T6 t# w7 z! N3 C7 T# L$ |
  11.                 endwhile;
    2 O; [; Z( m' j* Y3 C
  12.         endif;5 _8 A- F" e) ?2 p6 J
  13.         $return_string .= '</ul>';) n( V5 b7 M3 d% n$ w
  14. 4 x1 ]) f# f+ g0 l) K0 w* V' b3 n
  15.         wp_reset_query();
    8 L1 x2 T( M3 V, k# R* ^; N, Y
  16.         return $return_string;* X/ k& s. \0 V7 c7 n1 O
  17. }
銴鋆賭誨蝣
憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :
  1. [recent-posts posts="5"]
銴鋆賭誨蝣
園*蝷箸潛閬賢 , 典停臭誑唳餈鈭蝭蝡銵
" r% j1 m! I% x
2.2 凋誨蝣潔葉憓摰
臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗
  1. function recent_posts_function($atts, $content = null) {6 d3 l; A+ _& ~# Y' L( }  i/ _
  2.         extract(shortcode_atts(array(
    ) w' m. m. I3 m  W* y
  3.                 'posts' => 1,
    & L1 N3 W0 S. J
  4.         ), $atts));- e' ]9 `7 n3 f$ T
  5. % }- }, P; }+ o
  6.         $return_string = '<h3>'.$content.'</h3>';' B, e* S" s+ X6 Y
  7.         $return_string .= '<ul>';
    - C8 a7 m% G- V: B7 Q5 S
  8.         query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));% F9 t8 r1 j! N; r9 P
  9.         if (have_posts()) :
    * T9 k/ c/ d8 `( A' z: ^
  10.                 while (have_posts()) : the_post();$ r) a- }" t0 g3 @1 T2 V5 ~. m
  11.                         $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';, Q/ E7 K* `' r1 K
  12.                 endwhile;
    2 N5 S3 P$ I0 D8 K! s3 V) O1 }
  13.         endif;, N6 B% @" m/ \) x9 h. f
  14.         $return_string .= '</ul>';
    & C/ B  e( V' J: M' f/ F6 g0 Y

  15. - }1 K/ h' J8 c& D" r! f- \
  16.         wp_reset_query();; b2 b/ E+ t# q
  17.         return $return_string;
    ' ?0 A% B0 A/ K3 W: a5 _* _% r
  18. }
銴鋆賭誨蝣
蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝
  1. [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣
支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗

, o' m3 \  i) f# ]& ]- K% E& T
3. 其遙雿唳寥*蝷箇凋誨蝣
3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣
閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛典臭誑撠甇斤撘蝣潭曉典撌亙

( A+ ]  V5 J& F
臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 :
  1. add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣
曉其閬靽格嫣遙雿嗡唳
, 瑟, 湧甈撠甇蝣箏圈*蝷

, M0 J: A+ w8 i" b
璅嚗刻隢銝剖函凋誨蝣潘
  1. add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣
冽閬銝剖函凋誨蝣潘
  1. add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣
4. TinyMCE蝺刻摩典凋誨蝣潭! e: ?; s7 ?: Z+ S

* K3 n. V- l- i, W# K& P* P1 g/ }嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯
閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗
  • 箸迨撱箇銝 JS 瑼獢
  • 閮餃JS隞
    6 S7 |; ?  P! {) s
; e2 c7 t# P  C, K9 m1 N" d
4.1 JS隞嗡撱箇
JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗
  1. (function() {
    5 \1 l: n' O- c2 M8 A/ g
  2.         tinymce.create('tinymce.plugins.recentposts', {
    # x1 A8 t8 U+ ^) O
  3.                 init : function(ed, url) {
    , k, L) j: n' _! [( U/ q% ]! c
  4.                         ed.addButton('recentposts', {* B# y! e4 U9 e0 u! @0 v8 U8 @6 H
  5.                                 title : 'Recent posts',
    9 I* N5 Z; n5 t
  6.                                 image : url+'/recentpostsbutton.png',( T; G8 G$ @0 j' R
  7.                                 onclick : function() {9 j8 V7 R- R: S. r: D
  8.                                         var posts = prompt("蝡賊", "1");
    , W9 d4 S7 Y6 |  N9 J
  9.                                         var text = prompt("隢憛怠亙銵冽憿", "唳蝡");
    6 q* ~: M, v3 u8 r
  10. 9 {+ F! V/ ^. Q% [
  11.                                         if (text != null && text != ''){
    & B- _9 p0 U# F- `
  12.                                                 if (posts != null && posts != '')% ]; \, }- }2 r9 W
  13.                                                 ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');
    , {) H3 B% k8 n
  14.                                         else
    " R0 K9 g# {/ A1 B' D; e# ]
  15.                                                 ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');% Z, Z4 E* y3 o. Z4 T' H
  16.                                         }
    . q( A' z: w5 w  Z
  17.                                         else{
    ! g: u% f$ d/ y) a; H
  18.                                                 if (posts != null && posts != '')
    : O7 g& y2 G, O5 C, O- U3 b9 x2 e
  19.                                                         ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');
    3 H+ m% l7 f/ y7 z5 r- p
  20.                                                 else" B8 t5 D- X# Z% r% E
  21.                                                         ed.execCommand('mceInsertContent', false, '[recent-posts]');$ d+ j- }4 W3 v2 }, ]7 E0 m, U7 f
  22.                                         }, D) R  Y# C" m5 n+ A/ f2 M
  23.                                 }
    % C9 P$ o9 `2 f2 x7 p6 C+ |
  24.                         });+ c) K3 x3 y3 V/ c' a9 w( ?+ x
  25.                 },8 z+ k  [' i* _1 C8 k, _
  26.                 createControl : function(n, cm) {; _( V; a+ _7 n! F1 E
  27.                         return null;
    - f/ c- d! d. w5 A' W
  28.                 },
    ' t6 y4 s/ A6 d+ M1 X$ |3 y# L
  29.                 getInfo : function() {5 i/ f$ H3 R3 P- }  S7 ^
  30.                         return {, {7 G, P& J; H1 n8 U" R
  31.                                 longname : "Recent Posts",9 C7 H7 e, I; f$ L" H
  32.                                 author : 'Specs',8 t, |% x" i6 }+ G, {5 N! @
  33.                                 authorurl : 'http://9iphp.com',1 C  |  W5 b5 v* ]
  34.                                 infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',. e" t6 x# k: `8 _; {% @
  35.                                 version : "1.0"
    0 r4 a& o) W6 N8 p
  36.                         };
    9 z9 I7 X; a0 U
  37.                 }- _! H7 V7 E$ `, n
  38.         });4 C9 z1 o/ f7 m# t
  39.         tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);  K; j3 j% e6 x$ S! x0 f. |
  40. })();
銴鋆賭誨蝣
澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨
onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉
敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准
: F7 P& y' {! O% K( J
4.2 閮餃TinyMCE隞2 P8 @- N# @" T- f2 V5 ^  D
" X- p% L1 H9 Y- r1 i
嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉
蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩
  1. function register_button( $buttons ) {
    + T9 N* s6 ~' w2 b$ {. m
  2.         array_push( $buttons, "|", "recentposts" );
    1 s5 q4 K; ]0 L4 `2 e
  3.         return $buttons;6 x" _; B; T& D% k  n7 O
  4. }
銴鋆賭誨蝣
蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅
' e. g1 M, S1 T- G1 a% E: U/ M
  1. function add_plugin( $plugin_array ) {
    ) E. d3 o3 j" [8 i1 r* d0 I
  2.         $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';
    0 L, P; h: Y2 M* _: s
  3.         return $plugin_array;% k$ w3 _# N0 Y$ K5 N
  4. }
銴鋆賭誨蝣
銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁
+ M& Q/ R  Z4 p* s. @% i2 Q
  1. function my_recent_posts_button() { , \" m6 E: j: C' G3 r/ ?
  2.         if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {
    0 g: u5 x: C- e* T6 O! H5 C
  3.                 return;9 U: v8 G* k$ c/ {: V
  4.         }' D2 }# U7 v0 j4 \4 M. ]& z
  5. & C/ d* z  d- A* Y0 A" z  }
  6.         if ( get_user_option('rich_editing') == 'true' ) {8 G) ]" f' k; ]" `8 z
  7.                 add_filter( 'mce_external_plugins', 'add_plugin' );
    1 x! I0 B$ D$ e2 S5 `( h
  8.                 add_filter( 'mce_buttons', 'register_button' );
    3 l; H7 X# `( `2 L! ]% A6 w& Y
  9.         }
    ; C* e% {. A0 `0 E, {
  10. }
銴鋆賭誨蝣
嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁
" N2 c& S/ f1 w( s: t9 s敺賣詨曉訖ordpress憪賣
  1. add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣
& |0 ?! L# W" K  t$ [
4.3 冽

9 w+ I8 Z# i# D  e7 A! I- U
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽
( k1 d5 f- j0 y5 P. O9 C$ n9 n" W
暺嚗敶箏閰望霈頛詨交蝡賊嚗

* z, B& Q: S; I% f/ y" _9 P" x
頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗

9 a+ N3 t5 j6 r0 L$ _, i
貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗

" f6 B6 O( ~& u2 v/ v
唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆
蝡: 1 2

雿輻券

祉蝛閬

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

QQ|撠暺撅||52AV璈A

GMT+8, 2026-3-17 20:12 , Processed in 0.084510 second(s), 22 queries .

蝯∠.撱

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

鋆貉憒 敹恍敺 餈銵