52AV璈A|52AV.ONE

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

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

[銴鋆賡包
頝唾唳摰璅撅
璅銝
潸” 2016-6-9 12:40:16 | 芰閰脖 |芰憭批 撣 |摨閬 |梯璅∪
WordPress芸蝢拍凋誨蝣嘯hortcode隞亙Y蝺刻摩函凋誨蝣潭(shortcode button)
8 b1 e- A) ~8 \. Z, {* h6 r% o/ U) H, I. p1 G2 \7 `/ p1 y
wordpress Shortcode逕ordpress凋誨蝣澆賣臬肓ordpress 2.5祇憪鋡怠伐冽琿wordpress凋誨蝣澆臭誑撣豢嫣噶敹恍啣函楊頛舀蝡Y亙摰對凋誨蝣潛撣貉冽交唳蝡亙誨仿單閬閮乩頛桃Blog隢思犖嗡wordpress璅⊥踵嚗wordpress Shortcode撖衣曆pageW乩撟餌舐. o. ^3 E" n$ S: h4 g3 n/ q
隞乩 wordpress 凋誨蝣潛閰喟隞蝝孵脤具7 z! v% u# h" \5 B, f3 ^
5 F5 U: }$ @* g8 z. y
凋誨蝣潛 wordpress 雿輻券撣貊陛柴 靘憒唾憿舐內箸摰餈蝡 , 臭誑雿輻函蝔撘蝣潮隡潭潔 :# X5 K+ c- J/ N; Z9 k
  1. [recent-posts]
銴鋆賭誨蝣
甇文 , 臭誑閮剖銝訾批嗆蝡賊 :

; l: ?6 E# r6 Y9 \- n- F4 h
  1. [recent-posts posts="5"]
銴鋆賭誨蝣
甇文 , 銋臭誑箸蝡銵冽啣銝璅憿 :
9 l- x/ |9 S. x, r* b* ~
  1. [recent-posts posts="5"]Posts Heading[/recent-posts]
銴鋆賭誨蝣

) H# P! G$ }0 `) I9 {4 M4 h& l

0 j2 h  O9 k, w4 J3 C1. 蝪∪桃凋誨蝣
+ M* Z. @2 W" y6 I* A3 r
祆摮貊蝚砌典 , 撱箇銝凋誨蝣潛蝔撘蝣 :0 N5 |# P" ]9 R' C7 o  o0 }
  1. [recent-posts]
銴鋆賭誨蝣
閰脩摨撱箇舫撣貊陛桐蒂銝銝閬撣賊脤 PHP 亥 撱箇蝔憒銝 :
1 S- a# r1 p7 k9 e9 \7 y
  • 撱箇銝賣 , wordpress潛曄凋誨蝣潭澆急迨賣 ;
  • 梯身蝵桐臭蝔曹閮餃凋誨蝣 ;
  • 閮餃賣賊蝯針ordpressaction銝  _* z0 J; N. u7 O9 ^4 n" ^
3 u' p1 C5 }' L7 o3 e3 X1 f
祆摮豢蝔撘蝣澆湔交曉逆unction.php(批嗅==>憭閫==>銝駁蝺刻摩==>function.php) ,銋臭誑曉其鋡怠怠狄unction.php桃函php瑼獢銝准4 t8 J3 K( _* q' J+ c
1.1 撱箇餈賣
' S1 P% N" g' ?5 m/ j6 S; l4 Q嗥潛曄凋誨蝣潛嚗摰鋡思蝔曹餈賣貊銝畾萎誨蝣潭隞選隞交撱箇銝賣賂敺鞈摨思葉脣啁蝡
0 D$ a, g% W8 [6 b" y
  1. function recent_posts_function() {4 T; }3 }' D0 ]- d4 L1 W4 b
  2.         query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));! s! o- ^3 c, ?) j$ O" L+ n. @
  3.         if (have_posts()) :
    4 z8 a  p1 S$ o# c7 \2 k: @1 _
  4.                 while (have_posts()) : the_post();3 ^# F3 P: }# ~6 Y$ d6 @
  5.                         $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';( k" G9 a. a3 k9 e* Q
  6.                 endwhile;
    ; s0 C; m3 A- E1 c# F  D8 f' c
  7.         endif;
    4 I9 b8 {/ h: X# `! ]! r. V* h
  8.         wp_reset_query();; z% I( r/ g( D! l, ]9 Q
  9.         return $return_string;
    2 g4 h2 J$ G; _
  10. }
銴鋆賭誨蝣
憒銝蝷綽亥岷鞈摨恬脣啁蝡嚗銝西銝撣嗆亦摮銝脯澆閮餅荔餈賣訾蒂銝唬遙雿批捆嚗航銝摮銝脯
7 l/ X8 t  o" ~  `  ]
. J8 D  S2 J$ H7 T- M* M  U
+ b: e) E  Z1 g. ^. L4 f* |
1.2 閮餃凋誨蝣
4 o' `: [' e, a0 n6 w. Q1 t& o曉剁閮幃ordpress賣豢臭凋誨蝣潘
  1. function register_shortcodes(){% _( G3 ?( m+ y3 m, m3 B9 P: z
  2.         add_shortcode('recent-posts', 'recent_posts_function');
    3 K) t9 q/ v% P  C9 n+ F3 ^
  3. }
銴鋆賭誨蝣
嗅冽蝡批捆銝凋蝙函凋誨蝣" W- O& \' c/ @1 T5 s
  1. [recent-posts]
銴鋆賭誨蝣
撠芸澆 recent_posts_function() 賣賂閬蝣箔凋誨蝣潛摮臬臭嚗隞仿踹銴

& v$ K9 E* a7 C! ^0 S
7 b# U5 y, l9 D; h$ j9 X6 l7 G

) E- L2 U  m' k- I+ J8 @/ W3 l1.3 Hook into WordPress& C! @: g2 Q6 a0 d1 y2 w# l

- \" L2 b! T& f! N箔賢瑁 recent_posts_function() 賣賂閬摰蝬摰 WordPress 憪文銝准! H8 h$ N0 |2 l& n1 s
  1. add_action( 'init', 'register_shortcodes');
銴鋆賭誨蝣
3 h1 j5 I3 B- w2 _8 {2 t
1.4 皜祈岫凋誨蝣
5 Z1 J& T" z! P* u* f1 R
蝪∪桃凋誨蝣澆歇閮剖 , 曉券閬皜祈岫臬西賣迤撣詨瑁 臭誑撱箇唳蝡 ( 暹 ) , 撠銝隞蝣澆唳蝡批捆雿蝵 :
  1. [recent-posts]
銴鋆賭誨蝣
2. 脤凋誨蝣
2.1 凋誨蝣澆
凋誨蝣潭舫撣豢敶 , 箏霈亙詨航摰游賬 閮剜敹閬憿舐內銝孵賊啁蝡 箔圈桃 , 閬蝯衣凋誨蝣潭摰嗡賊隞仿*蝷箸餈蝡詻
閬雿輻典拙賣 ,蝚砌臬批遣 wordpress shortcode _ atts ( ) 賢 , 摰冽嗥凋誨蝣澆惇扯砍啣惇抒其韏 , 銝血‵亙唳唾雿蝵柴 蝚砌 PHPextract() 賣 , 憿批蝢 , 摰箇凋誨蝣潔葉撅祆扼
撅餈 , 啣銝 $atts, 摰臭,敺銝剖臭誑唳閬蝡詻 亥岷鞈摨思敺摰貊桃蝡 , 銝血喳銝銵 :
  1. function recent_posts_function($atts){
    ( l2 s  ]- `! e* q7 \6 S& |
  2.         extract(shortcode_atts(array(
    , t3 E1 k$ r) u9 `0 \; Y
  3.                 'posts' => 1,3 ?' {6 ?- K* g$ ~, _
  4.         ), $atts));  K# V4 J: G2 k

  5. 5 L; L  z& P4 M5 L' b; o( u: X
  6.         $return_string = '<ul>';
    0 |8 ]/ E  k9 E" v( ~: `, a
  7.         query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
    4 J8 O' e# A# _9 [8 a
  8.         if (have_posts()) :
    3 A/ c3 ]4 N, h$ q
  9.                 while (have_posts()) : the_post();
    1 |8 B* }' Y2 v5 y% Q: G/ c6 Y. G& O
  10.                         $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
    ) w2 g2 E' d9 @% J  j/ v& o
  11.                 endwhile;. Y- ?/ f$ l" m1 }
  12.         endif;
    9 o" N$ B. e/ D" A- y$ a
  13.         $return_string .= '</ul>';
    % m( F; n1 {- e0 {
  14. ! `3 p9 {1 e+ {1 ?/ t
  15.         wp_reset_query();
    7 T7 H" N6 A, R# c0 N
  16.         return $return_string;5 y$ q  _9 O4 C' x2 U/ g+ O
  17. }
銴鋆賭誨蝣
憒雿輻刻芣摰甇日賊 , 1 撠舫閮剖潦 臭誑亙撅祆找蝙凋誨蝣潭亙游詻 雿輻券撘瑕賣 , 撠勗臭誑摰隤輻冽唳蝡賊 :
  1. [recent-posts posts="5"]
銴鋆賭誨蝣
園*蝷箸潛閬賢 , 典停臭誑唳餈鈭蝭蝡銵
# u9 {& @" D7 A4 g% \2 z
2.2 凋誨蝣潔葉憓摰
臭誑蝜潛游凋誨蝣潦啣銝鈭批捆雿箏 靘喲, 靘憒"唳蝡銵函璅憿" 亥甇斤桃 , 閬刻賣訾葉啣蝚砌$content , 銝行摰憿舐內典銵典Y銝 <h3> 璅蝪賭葉啁賣詨銝嚗
  1. function recent_posts_function($atts, $content = null) {, C+ A" M1 M- p, q* u# }$ E' Z3 d
  2.         extract(shortcode_atts(array(# h+ f1 j* ]' a, C' R( s
  3.                 'posts' => 1,
    + L! y/ U; C/ i
  4.         ), $atts));
    1 h+ `. D3 A7 Y( ?
  5. , B) t+ n# g; ~  c6 i
  6.         $return_string = '<h3>'.$content.'</h3>';
    ! q7 t/ }6 c5 e* i8 d
  7.         $return_string .= '<ul>';  e  R5 {; u+ j* R$ J
  8.         query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
    9 B) y' N$ y* o5 ^  w; m5 ?6 K
  9.         if (have_posts()) :& n; i! X% g9 J
  10.                 while (have_posts()) : the_post();/ h# g6 }8 e6 p" r5 Y$ e1 `. G8 V7 p
  11.                         $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';; _' X) z* w0 K) T1 o1 S
  12.                 endwhile;
    # t3 f) b2 u3 G. t: p' b
  13.         endif;3 f7 j$ d7 h+ P8 c  I( R2 w
  14.         $return_string .= '</ul>';- {- S6 m0 L$ C4 M

  15. ( @) p2 P1 x0 G  q" }& T
  16.         wp_reset_query();# c/ U1 s0 `# x& B; _0 @" g
  17.         return $return_string;
    / I4 y+ Z" P8 g  A& m1 D
  18. }
銴鋆賭誨蝣
蝔桃凋誨蝣潮隡潭潔HTML璅蝪踝
  1. [recent-posts posts="5"]This is the list heading[/recent-posts]
銴鋆賭誨蝣
支蝡銵典鈭銝璅憿嚗園批捆銝銝蝷箔臭璅嚗

6 t1 L) f9 r2 u: r) {. t7 R
3. 其遙雿唳寥*蝷箇凋誨蝣
3.1 典湧撠撌亙瑚葉雿輻函凋誨蝣
閮剜瘜銝 , 湧甈撠撌亙瑟臬蕭亦凋誨蝣潛典臭誑撠甇斤撘蝣潭曉典撌亙
1 a- L6 \8 U3 S; K; m
臭誑∠其銵蝔撘蝣潔券賣 , 箔雿踹湧甈撠撌亙瑟舀渡凋誨蝣 , 啣隞乩蝔撘蝣 :
  1. add_filter('widget_text', 'do_shortcode');
銴鋆賭誨蝣
曉其閬靽格嫣遙雿嗡唳
, 瑟, 湧甈撠甇蝣箏圈*蝷

- B. m' w5 N) c- O+ v5 A
璅嚗刻隢銝剖函凋誨蝣潘
  1. add_filter( 'comment_text', 'do_shortcode' );
銴鋆賭誨蝣
冽閬銝剖函凋誨蝣潘
  1. add_filter( 'the_excerpt', 'do_shortcode');
銴鋆賭誨蝣
4. TinyMCE蝺刻摩典凋誨蝣潭
$ L. s' g: \# g" A) H
, y8 ^9 w( b  O/ U4 v嗥凋誨蝣澆臭誑嫣噶箸蝡憓批捆嚗雿舫撠潭桅冽嗡隤芸航賡舀暺銴嚗撠文嗆舐嗅豢頛憭憭批貊冽嗅潮HTML隞蝣潔蒂銝嚗嗉閬雿輻函凋誨蝣潘隞敹閮雿琿隤瘜賂箏喃蝙臭撠撠隤瘜航炊嚗賢航賢湔喃啁蝯
閬閫瘙粹嚗臭誑蝯 TinyMCE蝺刻摩典銝嚗雿輻冽嗅臭誑蝪∪桃暺靘憓凋誨蝣潦撱箇拙甇仿嚗
  • 箸迨撱箇銝 JS 瑼獢
  • 閮餃JS隞
    8 C) _5 ^. ], g8 f: y" @

! C$ z3 R6 o6 Y8 h! f  J& h, E0 K
4.1 JS隞嗡撱箇
JS隞嗆舫 TinyMCE API 靘閮餃 TinyMCE 隞嗥其蜓憿 js 桅銝(/wp-content/themes/雿銝駁蝔/js/)撱箇 recent-posts.js 嚗銝西票脣颱Y隞蝣潘閬瑼(/wp-content/themes/雿銝駁蝔/js/recentpostsbutton.png)銋曉典銝桅嚗嚗
  1. (function() {
    ; m' Y* l7 m  Q# I7 Z. C. A
  2.         tinymce.create('tinymce.plugins.recentposts', {
    2 ]& d7 V2 ~9 Y
  3.                 init : function(ed, url) {9 B* k3 R3 e* I; B% b$ y& Q
  4.                         ed.addButton('recentposts', {
    : @. a2 v8 y, ~9 W( ~1 y; j2 e
  5.                                 title : 'Recent posts',
    6 H7 l7 v3 _3 j1 K8 E* s; S
  6.                                 image : url+'/recentpostsbutton.png',% H* ]5 _6 X. m' S
  7.                                 onclick : function() {
    ( N/ m. l) i2 T! b1 D7 q$ u& `4 j
  8.                                         var posts = prompt("蝡賊", "1");6 o" h7 g% b# ?. ^% U! l. t  D/ b
  9.                                         var text = prompt("隢憛怠亙銵冽憿", "唳蝡");
    8 u' {' x( a! H# C6 j$ c+ {# v

  10. ; L. F) h7 p* e) Y. _
  11.                                         if (text != null && text != ''){
    3 t- g7 h& f, J, G; _2 N8 Y! L/ C% R- e
  12.                                                 if (posts != null && posts != '')9 j) u4 O7 _4 z
  13.                                                 ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]'+text+'[/recent-posts]');
    5 h& n, S/ {5 K( C4 \
  14.                                         else" |- p' c5 L- W1 z) ]
  15.                                                 ed.execCommand('mceInsertContent', false, '[recent-posts]'+text+'[/recent-posts]');
    / Y1 k  I" G( o
  16.                                         }
    / r4 @( Y) J( v2 F
  17.                                         else{
    + T) }" L. f5 F/ F
  18.                                                 if (posts != null && posts != '')
    0 e" P1 g5 O) }* n& i  T' M
  19.                                                         ed.execCommand('mceInsertContent', false, '[recent-posts posts="'+posts+'"]');
    % j4 o+ q7 t1 ^+ |- G# q
  20.                                                 else6 Z8 J7 W3 `& G7 T; O( ?
  21.                                                         ed.execCommand('mceInsertContent', false, '[recent-posts]');
    2 M! m% u7 i% n, ]9 }+ ?3 I
  22.                                         }4 {6 M5 R) [, x. R' f
  23.                                 }+ ^5 M  y$ w" ?" ^" h, b
  24.                         });4 @0 Y9 ~4 F9 Z- V. w3 P
  25.                 },
    * [. b; ?# S/ ]" z) z. H
  26.                 createControl : function(n, cm) {; j% t8 ~0 d6 @/ _! P: E. e& j
  27.                         return null;' S, U: Q- h. U
  28.                 },0 _5 i3 Q  u) K( o7 N% s- [9 r
  29.                 getInfo : function() {0 G2 T* C; s& [2 L! b/ `. l6 |9 M8 K
  30.                         return {
    $ D+ B- R4 f6 F* s! ]: b
  31.                                 longname : "Recent Posts",2 i0 M. d+ v  w9 Q6 Q/ Z
  32.                                 author : 'Specs',; Z  e$ P7 R* |+ ^
  33.                                 authorurl : 'http://9iphp.com',
    : V8 `" o' D5 Z& c) Y. y# j
  34.                                 infourl : 'http://9iphp.com/opensystem/wordpress/1094.html',0 b2 Y! j0 u  P8 z
  35.                                 version : "1.0"2 ?2 p- S0 ?2 r3 ~# l. C! M7 r
  36.                         };$ k) V7 r% H! U* L8 B1 [2 m
  37.                 }
    7 |' R/ Z2 O( Z9 p
  38.         });
    ) E7 ^/ |$ C% M; W8 M' Y! N2 V
  39.         tinymce.PluginManager.add('recentposts', tinymce.plugins.recentposts);
    ) L, V1 P6 p9 V/ |! y( k2 e7 L
  40. })();
銴鋆賭誨蝣
澆 tinymce.create() 撱箇鈭銝隞嗚隞蝣潔葉閬 init() 賣賂摰蝢拐銝摮嚗銝icon隞嗅 onclick() 鈭隞嗉蝔摨
onclick() 賣貊抵嚗靘摨敶箏拙撠閰望霈冽嗉撓蝡賊銵冽憿拙賂嗅寞冽嗉撓伐凋誨蝣潭亙啁楊頛臬其葉
敺嚗TinyMCE隞園 add() 賣詨 PluginManager曉冽撌脩游鈭 [recent-posts] 凋誨蝣澆訖ordpress銝准
0 Q) N5 J' w& W* O
4.2 閮餃TinyMCE隞
4 S8 W! G% _9 i  Z9 M3 q- i
" o! i) u) Z7 d
嗆典撱箇JS 瑼 , 曉券閬閮餃摰凋誨蝣潭 甇斗撱箇鈭拙賣訾蒂摰蝜怎喲拍嗥 wordpress 瞈曉其葉
蝚砌怠 register_button() 賣豢凋誨蝣澆唳銝哨函暹唳銋憓脩
  1. function register_button( $buttons ) {, K; J: t2 o  l8 u
  2.         array_push( $buttons, "|", "recentposts" );
    + T1 b+ [: R% i2 \) v* T- b; R
  3.         return $buttons;) X! Q. f+ n7 L1 V& t8 J. E
  4. }
銴鋆賭誨蝣
蝚砌賣 add_plugin() 摰JS隞嗥頝臬隞嗅' d6 A5 @- |6 G* j: b& M
  1. function add_plugin( $plugin_array ) {
    - q( ?2 q2 [- D  z
  2.         $plugin_array['recentposts'] = get_template_directory_uri() . '/js/recent-posts.js';$ g; Y4 P; Y6 l' j4 i0 L
  3.         return $plugin_array;( o6 U  R+ T. h* P
  4. }
銴鋆賭誨蝣
銝銝甇交臬怠W拙賣貊瞈曉具register_button() 賣賊蝯 mce_buttons 瞈曉剁函楊頛臬刻交隞嗥瑁嚗 add_plugin() 賣賊蝯 mce_external_plugins 瞈曉剁嗉交瑁
$ N$ ]# y9 \% R: Q
  1. function my_recent_posts_button() {
    , a  S- x9 M% z
  2.         if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {; ]* I' I) V" E( x' x, Y
  3.                 return;, e. M& n/ u4 b) \1 B0 n# k! X9 y9 r' \
  4.         }% a* M( {  s4 Z
  5. 4 S, w# H# p2 _# w" N9 l
  6.         if ( get_user_option('rich_editing') == 'true' ) {7 t* z  B+ P' N4 a& `
  7.                 add_filter( 'mce_external_plugins', 'add_plugin' );$ K% y% Y2 f3 J  p& p! o3 u( k
  8.                 add_filter( 'mce_buttons', 'register_button' );
      n9 Q1 X! v  V( P0 n
  9.         } 2 f9 l8 |5 }$ E
  10. }
銴鋆賭誨蝣
嗥冽嗆撠蝡航蝺刻摩其葉蝺刻摩甈嚗Y賣訾瑁1 s- v! T$ N. H, h: z
敺賣詨曉訖ordpress憪賣
  1. add_action('init', 'my_recent_posts_button');
銴鋆賭誨蝣

5 g2 x4 ^  l8 o8 r1 M, E
4.3 冽
5 P6 h2 }' J5 H) ~' J
箔皜祈岫蝔撘蝣潭臬行迤蝣綽銝蝭啁蝡嚗銝摰蝢拍臭臬歇蝬箇曉函洵銝銵怠偏鈭嚗憒銝蝷綽

! {/ [! M4 i# Y4 b& c3 w) c
暺嚗敶箏閰望霈頛詨交蝡賊嚗

3 w& Z; Z8 J; x6 U6 l0 t& J& }
頛詨亙暺蝣箏敺嚗敶箇洵鈭撠閰望嚗霈頛詨亙銵函璅憿嚗

7 R: a7 D0 X  B
貊蝛箇閰梧銝箇曉冽敺隞蝣潔葉敺嚗凋誨蝣潭啣啁楊頛臬其葉嚗
9 _/ X  R4 t  {0 r( ^5 U
唳迨撠勗之鈭唾憓嗡賜凋誨蝣澆臭誑銝Y摮嚗憒瘜株ˊ銝銝嚗訾縑臭誑鋆賭箇泵雿瘙凋誨蝣澆
蝡: 1 2

雿輻券

祉蝛閬

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

QQ|撠暺撅||52AV璈A

GMT+8, 2026-3-30 14:18 , Processed in 0.095548 second(s), 22 queries .

蝯∠.撱

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

鋆貉憒 敹恍敺 餈銵