ページリストの表示のタイトル・日付の順番を変更したい
-
お世話になります。
新規でページを作成した場合、下記のタグを入れることにより、
リスト表示が可能ですが、HTMLに出力した場合、順番を(1)日付(2)タイトルの順に表示したいのですが、どのように変更すればよいかが不明です。
どなたかご指導をお願いします。<?php wp_list_pages('title_li=&exclude=3,6,10,12,56& sort_column=post_date&show_date=created'); ?>
〇現在
・タイトル
2009年8月2日〇変更後
・2009年8月2日
タイトル
-
the_timeやthe_permalinkなどは今いる記事のデータを吐き出してしまいます。
ページにいるということは記事ではないので最新のデータが表示されてしまうと思います。
なので取得してループさせている$postから直接データを抜き出せば思うような表示になるのではないでしょうか。
具体的にはget_permalink($post->ID)
とかのような表記になると思います。あと、個人的な意見になりますが、exec-phpなどのプラグインはセキュリティを落とします。できればfunctions.phpなどにソースを記述して、フィルターなどで書き換えるような方法が良いのではないでしょうか。
shokun0803 さん
すみません、この部分がどうして良いかがわかりません。
ページにいるということは記事ではないので最新のデータが表示されてしまうと思います。
なので取得してループさせている$postから直接データを抜き出せば思うような表示になるのではないでしょうか。下記のページや
<?php wp_get_archives('type=postbypost&limit=5'); ?>
を参考にしましたが、思うような表示にはなりませんでした。すみませんが、もう少しヒントを下さい。
http://ja.forums.wordpress.org/topic/1009?replies=3
→表示されない
<?php wp_get_archives('type=postbypost&limit=5'); ?>
→日付が表示されないforeach ($postslist as $my_post) : setup_postdata($my_post); $my_year = substr($my_post->post_date, 0, 4); $my_month = substr($my_post->post_date, 5, 2); $my_day = substr($my_post->post_date, 8, 2); $my_time = date('Y年m月d日', mktime(0,0,0,$my_month,$my_day,$my_year)); $my_permalink = get_permalink($my_post->ID); $my_title = $my_post->post_title; $rep_contents .= "\t<li>»" . $my_time . "<br /><a href=\"" . $my_permalink . "\">" . $my_title . "</a></li>\n"; endforeach;
必用なpostデータを取得してforeachまではできているので、foreachして取り出したpostのデータを表示することが重要です。
現状のソースですとthe_title()
などのようにその場で表示しようとしています。
取り出したpostデータから情報を抽出するには$my_post->post_title
などのようにします。そのまま表示してもいいですが、上記ソースでは一度別の変数に格納しています。これをfunctions.phpに記述するとなると、例えば、
function my_five_post_list($contents) { if( is_page() && substr_count($contents, '[my_five_post_list]') != 0 ) { $rep_contents = "\n<ul>\n"; $postslist = get_posts('orderby=post_date'); foreach ($postslist as $my_post) : setup_postdata($my_post); $my_year = substr($my_post->post_date, 0, 4); $my_month = substr($my_post->post_date, 5, 2); $my_day = substr($my_post->post_date, 8, 2); $my_time = date('Y年m月d日', mktime(0,0,0,$my_month,$my_day,$my_year)); $my_permalink = get_permalink($my_post->ID); $my_title = $my_post->post_title; $rep_contents .= "\t<li>»" . $my_time . "<br /><a href=\"" . $my_permalink . "\">" . $my_title . "</a></li>\n"; endforeach; $rep_contents .= "</ul>\n"; $new_contents = str_replace('[my_five_post_list]', $rep_contents, $contents); } else { $new_contents = $contents; } return $new_contents; } add_filter('the_content', 'my_five_post_list');
などのようになるでしょうか。
ページの表示したい任意の場所に[my_five_post_list]
を記述することでその部分を書き換えます。適当な作りなのでご参考までにおねがいします;)
shokun0803 さん
返信が遅くなってすみません。
上記のコードで、求めていたものが表示確認出来ました。
ありがとうございました。ちなみに件数については、
if( is_page() && substr_count($contents, '[my_five_post_list]') != 0 ) {
と
$new_contents = str_replace('[my_five_post_list]', $rep_contents, $contents);
の「five」の部分を変更すればよろしいでしょうか?おはようございます、igarashi5620さん。
[my_five_post_list]についてはページなどでphpのコードを使用せずにコードを挿入する為に*任意*につけた名前です。実際にはページのこの部分を探して、あれば「置き換え」します。簡易ショートコードといったところでしょうか。
で、件数についてですが、
$postslist = get_posts('orderby=post_date');
の部分が決定しています。get_postsについては日本語Codexをみるとよく分かりますよ。get_postsはデフォルトで5件の投稿を取得します。ですので何も指定していない現在のコードだと5件表示になっているのですね。
ここにnumberposts=10と指定してあげれば10件表示にできるはずです。具体的には$postslist = get_posts('orderby=post_date&numberposts=10');
といった感じでしょうか。簡易ショートコードの部分に関しては他のショートコードなどとかぶらなければ何に変更してもかまいません。ご自身で分かりやすいものに変更したほうが使いやすいかもしれませんね;)
shokun0803 さん
確認しました。
誠にありがとうございました。何度も恐れ入りますが、
固定ページについては、上記のコードの使用が可能でしょうか?
orderby=post_date
を
post_type=page
に変更しての対応のみでよろしいでしょうか?
よろしくお願いいたします。固定ページについては、上記のコードの使用が可能でしょうか?
これは何についておっしゃっていますかね。
・ページの中でこのコードが使えるか。(最新投稿○件の表示)
・ページの新規5件を表示したい。前者であればページの中でお好きな場所に
[my_five_post_list]
と記述(違うショートコードに変更していたら変更後のもので)してもらえれば動きます。普通にビジュアルエディタの状態でコピペすればOKですよ。もちろん投稿記事の中でも使えます。後者の場合、orderby=post_dateは「投稿日時順にソート」を意味しているのではずしてしまうと最新○件にならなくなってしまいます。
post_type=page
を追記すればページの最新○件になるはずです。shokun0803 さん
すみません後者です。
下記コードを
$postslist = get_posts('post_type=page&exclude=125,131');
$postslist = get_posts('orderby=post_date');
の下に追加してテストしたところの表示になりましたが、
コードの記述のしかたはこれでよろしいでしょうか?
ご指導お願いします。
* 2009年12月11日
テスト6
* 2009年12月11日
テスト5
* 2009年12月11日
テスト4
* 2009年12月11日
テスト3
* 2009年12月11日
テスト2function my_five_post_list($contents) { if( is_page() && substr_count($contents, '[my_five_post_list]') != 0 ) { $rep_contents = "\n<ul>\n"; $postslist = get_posts('orderby=post_date'); $postslist = get_posts('post_type=page&exclude=125,131'); foreach ($postslist as $my_post) : setup_postdata($my_post); $my_year = substr($my_post->post_date, 0, 4); $my_month = substr($my_post->post_date, 5, 2); $my_day = substr($my_post->post_date, 8, 2); $my_time = date('Y年m月d日', mktime(0,0,0,$my_month,$my_day,$my_year)); $my_permalink = get_permalink($my_post->ID); $my_title = $my_post->post_title; $rep_contents .= "\t<li>»" . $my_time . "<br /><a href=\"" . $my_permalink . "\">" . $my_title . "</a></li>\n"; endforeach; $rep_contents .= "</ul>\n"; $new_contents = str_replace('[my_five_post_list]', $rep_contents, $contents); } else { $new_contents = $contents; } return $new_contents; } add_filter('the_content', 'my_five_post_list');
get_posts
は1つだけ記述しましょう。
上記の記述ですと、2度get_posts
しています。これだと1回目のget_posts
は2回目のget_posts
に上書きされて消えてしまいます。$postslist = get_posts('orderby=post_date&post_type=page&exclude=125,131');
のように1度で記述します。
shokun0803 さん
ありがとうございました。
下記のとおり追加いたしましたが、
さらに
$res = get_post_meta($my_post->ID, 'new', 'true');
を入れたのですが、表示のしかたは
$rep_contents .= "\t<li>»" . $my_time . " <a href=\"" . $my_permalink . "\">" . $my_title . "" . $res . "</a></li>\n";
でよろしいですか?
” . $res . “の部分が不安です。。
一応動いておりますが。。
よろしくお願いします。function my_five_post_list($contents) { if( is_page() && substr_count($contents, '[my_five_post_list]') != 0 ) { $rep_contents = "\n<ul>\n"; $postslist = get_posts('orderby=post_date&post_type=page&exclude=5,29,31,36,38,40'); foreach ($postslist as $my_post) : setup_postdata($my_post); $res = get_post_meta($my_post->ID, 'new', 'true'); $my_year = substr($my_post->post_date, 0, 4); $my_month = substr($my_post->post_date, 5, 2); $my_day = substr($my_post->post_date, 8, 2); $my_time = date('Y年m月d日', mktime(0,0,0,$my_month,$my_day,$my_year)); $my_permalink = get_permalink($my_post->ID); $my_title = $my_post->post_title; $rep_contents .= "\t<li>»" . $my_time . " <a href=\"" . $my_permalink . "\">" . $my_title . "" . $res . "</a></li>\n"; endforeach; $rep_contents .= "</ul>\n"; $new_contents = str_replace('[my_five_post_list]', $rep_contents, $contents); } else { $new_contents = $contents; } return $new_contents; } add_filter('the_content', 'my_five_post_list');
. $my_title . "" . $res .
の部分が冗長ですね。間に何も文字を置く予定がないなら
. $my_title . $res .
でいいと思うのですけど。他に特に問題は見当たらなさそうですよ;)
shokun0803 さん
ありがとうございました。
確認いたしました。
大変お手数をおかけいたしました。
- トピック「ページリストの表示のタイトル・日付の順番を変更したい」には新たに返信することはできません。