Re: 記事一覧の設置
永らくありがとうございました › フォーラム › 使い方全般 › 記事一覧の設置 › Re: 記事一覧の設置
ちょっとやってみました
こんな感じのプラグインでどうでしょう?
<code><?php<br /> /*<br /> Plugin Name: WP Posts List Ex<br /> Plugin URI: http://exsample.com<br /> Description: WP Posts List Ex (custom function)<br /> Author: Foo<br /> Version: 1.0<br /> Author URI: http://exsample.com<br /> */<br /> <br /> function wp_posts_list_ex($args = '') {<br /> global $wpdb, $wp_locale;<br /> <br /> $defaults = array(<br /> 'limit' => '',<br /> 'before' => '',<br /> 'after' => '',<br /> 'list_type' => 'ul', //Set value are 'ul' or 'ol'.<br /> 'ol_type' => '', //Set value are '1','a','A','i' or 'I'. Only enable if the OL tag is selectedvalue<br /> 'ol_start' => '',<br /> 'li_value_revarse' => false, //Only enable if the OL tag is selected<br /> 'show_reverse' => false,<br /> 'show_date' => false,<br /> 'date_format_txt' => '',<br /> 'echo' => true<br /> <br /> );<br /> <br /> $r = wp_parse_args( $args, $defaults );<br /> extract( $r, EXTR_SKIP );<br /> <br /> if ( '' != $limit ) {<br /> $limit = absint($limit);<br /> $limit = ' LIMIT '.$limit;<br /> }<br /> <br /> //filters<br /> $where = apply_filters( 'wp_posts_list_ex_where', "WHERE post_type = 'post' AND post_status = 'publish' AND post_date != '0000-00-00 00:00:00'", $r );<br /> $join = apply_filters( 'wp_posts_list_ex_join', '', $r );<br /> <br /> $output = '';<br /> <br /> $orderby = 'post_date DESC ';<br /> $query = "SELECT * FROM $wpdb->posts $join $where ORDER BY $orderby $limit";<br /> $key = md5($query);<br /> $cache = wp_cache_get( 'wp_posts_list_ex' , 'general');<br /> if ( !isset( $cache[ $key ] ) ) {<br /> $arcresults = $wpdb->get_results($query);<br /> $cache[ $key ] = $arcresults;<br /> wp_cache_set( 'wp_posts_list_ex', $cache, 'general' );<br /> } else {<br /> $arcresults = $cache[ $key ];<br /> }<br /> if ( $arcresults ) {<br /> $results_count = count($arcresults);<br /> if($show_reverse) {<br /> $arcresults = array_reverse($arcresults);<br /> }<br /> if ($list_type=='ul'){<br /> $output .= "<ul>n";<br /> } else {<br /> switch($ol_type){<br /> case '1':<br /> $style = "style='list-style-type: decimal'";<br /> break;<br /> case 'a':<br /> $style = "style='list-style-type: lower-alpha'";<br /> break;<br /> case 'A':<br /> $style = "style='list-style-type: upper-alpha'";<br /> break;<br /> case 'i':<br /> $style = "style='list-style-type: lower-roman'";<br /> break;<br /> case 'I':<br /> $style = "style='list-style-type: upper-roman'";<br /> break;<br /> default:<br /> $style ='';<br /> }<br /> if (!empty($ol_start) && is_numeric($ol_start)){<br /> $start = 'start='. $ol_start;<br /> } else {<br /> $start = '';<br /> }<br /> $output .= "<ol $start $style>n";<br /> }<br /> foreach ( (array) $arcresults as $arcresult ) {<br /> $url = get_permalink( $arcresult );<br /> // post date<br /> if ($show_date){<br /> if ( '' == $date_format_txt ){<br /> $the_date = ' ('.mysql2date(get_option('date_format'), $arcresult->post_date).')';<br /> } else {<br /> $the_date = ' ('.mysql2date($date_format_txt, $arcresult->post_date).')';<br /> }<br /> } else {<br /> $the_date = '';<br /> }<br /> if ( $arcresult->post_title )<br /> $text = strip_tags( apply_filters( 'the_title', $arcresult->post_title, $arcresult->ID ) );<br /> else<br /> $text = $arcresult->ID;<br /> if ($li_value_revarse){<br /> $output .= "t<li value="" . $results_count-- . "">$before<a href='$url' title='$title_text'>$text</a>$the_date$after</li>n";<br /> } else {<br /> $output .= "t<li>$before<a href='$url' title='$title_text'>$text</a>$the_date$after</li>n";<br /> }<br /> }<br /> if ($list_type=='ul'){<br /> $output .= "</ul>n";<br /> } else {<br /> $output .= "</ol>n";<br /> }<br /> <br /> }<br /> if ( $echo )<br /> echo $output;<br /> else<br /> return $output;<br /> }<br /> ?></code>
テーマ内(ループ外)で利用します。
主なオプションの説明
1. limit 表示記事数
<code><?php wp_posts_list_ex("limit=3") ?></code>
表示例
・画像なし
・画像追加
・ぼくのブログです
2. show_date 投稿日付の表示
<code><?php wp_posts_list_ex("limit=3&show_date=1") ?></code>
表示例
・画像なし (2011年12月4日)
・画像追加 (2011年12月3日)
・ぼくのブログです (2010年12月2日)
<code>3. list_type ulとolタグの選択(デフォルトul)<br /> <?php wp_posts_list_ex("limit=3&show_date=1&list_type=ol") ?></code>
表示例
1. 画像なし (2011年12月4日)
2. 画像追加 (2011年12月3日)
3. ぼくのブログです (2010年12月2日)
4. li_value_revarse li のvalue値 を反転
<code><?php wp_posts_list_ex("limit=3&show_date=1&list_type=ol&li_value_revarse=1") ?></code>
表示例
3. 画像なし (2011年12月4日)
2. 画像追加 (2011年12月3日)
1. ぼくのブログです (2010年12月2日)
5. show_reverse 抽出結果全体を逆順にする
<code><?php wp_posts_list_ex("limit=3&show_date=1&list_type=ol&show_reverse=1") ?></code>
表示例
1. ぼくのブログです (2010年12月2日)
2. 画像追加 (2011年12月3日)
3. 画像なし (2011年12月4日)
6.ol_type
デフォルトで数値
ol_type=i (小文字のローマ数字)
<code><?php wp_posts_list_ex("limit=3&show_date=1&list_type=ol&ol_type=i") ?></code>
表示例
i. 画像なし (2011年12月4日)
ii. 画像追加 (2011年12月3日)
iii. ぼくのブログです (2010年12月2日)
ol_type=I (大文字のローマ数字)
<code><?php wp_posts_list_ex("limit=3&show_date=1&list_type=ol&ol_type=I") ?></code>
表示例
I. 画像なし (2011年12月4日)
II. 画像追加 (2011年12月3日)
III. ぼくのブログです (2010年12月2日)
ol_type=a (小文字のアルファベット)
<code><?php wp_posts_list_ex("limit=3&show_date=1&list_type=ol&ol_type=a") ?><br /> ></code>
表示例
a. 画像なし (2011年12月4日)
b. 画像追加 (2011年12月3日)
c. ぼくのブログです (2010年12月2日)
ol_type=A (大文字のアルファベット)
<code><?php wp_posts_list_ex("limit=3&show_date=1&list_type=ol&ol_type=A") ?></code>
表示例
A. 画像なし (2011年12月4日)
B. 画像追加 (2011年12月3日)
C. ぼくのブログです (2010年12月2日)
7. ol_start liのvalueの開始番号
ol_start=3
3. 画像なし (2011年12月4日)
4. 画像追加 (2011年12月3日)
5. ぼくのブログです (2010年12月2日)
C. 画像なし (2011年12月4日)
D. 画像追加 (2011年12月3日)
E. ぼくのブログです (2010年12月2日)
8. その他のオプション
date_format_txt 日付フォーマット
echo echoするかPHP値として返すか (デフォルト echoする)
before liタグの前につく文字
after /liタグの後ろ、日付がある場合はその後ろにつく文字