wordpressユーザーの私が、ショートコードを書いてみた。いろんなところの手を借りて書いたので出典を明記したいのだけど、まずは掲載しちゃうことにする。wp.vicunaで使用することを想定した、私好みの最新記事表示用ショートコード。http://real-jukugi.org/ で利用するのが目的。案外、PHPプログラミングをここまで本格的にやったのはこれが初めてである。でも、もっときれいにならないかな…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | function rj_postlist1( $atts , $content = null){ extract(shortcode_atts( array ( "num" => '5' , "cat" => '' , "exc" => '' , "body" => '0' , "auth" => '0' , "inline" => "h3" ), $atts )); global $post ; $tmp_post = $post ; $out = '<div id="postlist1">' ; $out .= '<style type= "text/css" > <!-- div#postlist1 div.section.entry div.textBody div.wp_social_bookmarking_light {display:none;} div#postlist1 div.section.entry div.textBody br.wp_social_bookmarking_light_clear {display:none; height: 0px;}--> </style>'; $gtpst = get_posts( 'numberposts=' . $num . '&category=' . $cat . '&exclude=' . $exc . '&orderby=modified' ); foreach ( $gtpst as $post ) : setup_postdata( $post ); $out .= '<div id="entry' .get_the_ID(). '" style="margin: 0px;">' ; $out .= '<' . $inline . ' style="margin: 0px;"><a href="' .get_permalink(). '">' .the_title( "" , "" ,false). '</a></' . $inline . '>' ; $out .= '<ul style="margin: 0 0 5px; padding: 0;">' ; if ( $auth !=0){ $out .= '<li><a href="' .get_author_posts_url(get_the_author_meta(ID)). '"><span>' .get_the_author(). '</span></a></li>' ;} $out .= '<li>' .get_the_date(). ' ' .get_the_time(). '</li>' ; $out .= '<li>' .get_the_category_list( ' | ' , '' ,false). '</li>' ; $out .= get_the_tag_list( '<li>' , ' | ' , '</li>' ); $out .= '</ul>' ; if ( $body != 0 ){ global $more ; $more = 0; $out .= '<div style="margin: 0 0 10px 0;">' ; $content = get_the_content(__( 'Continue reading' , 'vicuna' ),false); $content = apply_filters( 'the_content' , $content ); $content = str_replace ( ']]>' , ']]>' , $content ); $out .= $content ; $out .= '</div>' ; } $out .= '</div>' ; endforeach ; $out .= '</div>' ; $post = $tmp_post ; return $out ; } add_shortcode( "list1" , "rj_postlist1" ); |