フォーラム

[解決済み] ページリストの表示のタイトル・日付の順番を変更したい (43 件の投稿)

  1. igarashi5620
    メンバー
    2 years前の投稿 #

    shokun0803 さん

    度々恐れ入ります。
    お世話になります。
    下記の投稿最新記事5件表示のコードを固定ページの記事内に使用したいのですが、可能でしょうか?
    また、記事の表示件数を変更するには、どの部分を修正すればよろしいでしょうか?
    要望は最大10件表示です。

    「exec-php」のプラグインの使用で出来ることは分かりましたが、
    実際に使用すると、トップページの内容しか反映されなく、それと同じ記事が5件表示される状態になります。
    すみませんが、ご指導よろしくお願いします。

    <ul>
    <?php
    $postslist = get_posts('orderby=post_date');
    foreach ($postslist as $post) :
    setup_postdata($post);
    ?>
    <li>&raquo;<?php the_time('Y年m月d日'); ?><br /><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>
    </ul>
  2. shokun0803
    メンバー
    2 years前の投稿 #

    the_timeやthe_permalinkなどは今いる記事のデータを吐き出してしまいます。
    ページにいるということは記事ではないので最新のデータが表示されてしまうと思います。
    なので取得してループさせている$postから直接データを抜き出せば思うような表示になるのではないでしょうか。
    具体的にはget_permalink($post->ID)とかのような表記になると思います。

    あと、個人的な意見になりますが、exec-phpなどのプラグインはセキュリティを落とします。できればfunctions.phpなどにソースを記述して、フィルターなどで書き換えるような方法が良いのではないでしょうか。

  3. igarashi5620
    メンバー
    2 years前の投稿 #

    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'); ?>
    →日付が表示されない

  4. shokun0803
    メンバー
    2 years前の投稿 #

    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>&raquo;" . $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>&raquo;" . $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]を記述することでその部分を書き換えます。

    適当な作りなのでご参考までにおねがいします;)

  5. igarashi5620
    メンバー
    2 years前の投稿 #

    shokun0803 さん

    返信が遅くなってすみません。
    上記のコードで、求めていたものが表示確認出来ました。
    ありがとうございました。

    ちなみに件数については、
    if( is_page() && substr_count($contents, '[my_five_post_list]') != 0 ) {

    $new_contents = str_replace('[my_five_post_list]', $rep_contents, $contents);
    の「five」の部分を変更すればよろしいでしょうか?

  6. shokun0803
    メンバー
    2 years前の投稿 #

    おはようございます、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');といった感じでしょうか。

    簡易ショートコードの部分に関しては他のショートコードなどとかぶらなければ何に変更してもかまいません。ご自身で分かりやすいものに変更したほうが使いやすいかもしれませんね;)

  7. igarashi5620
    メンバー
    2 years前の投稿 #

    shokun0803 さん

    確認しました。
    誠にありがとうございました。

    何度も恐れ入りますが、
    固定ページについては、上記のコードの使用が可能でしょうか?
    orderby=post_date

    post_type=page
    に変更しての対応のみでよろしいでしょうか?
    よろしくお願いいたします。

  8. shokun0803
    メンバー
    2 years前の投稿 #

    固定ページについては、上記のコードの使用が可能でしょうか?

    これは何についておっしゃっていますかね。
    ・ページの中でこのコードが使えるか。(最新投稿○件の表示)
    ・ページの新規5件を表示したい。

    前者であればページの中でお好きな場所に[my_five_post_list]と記述(違うショートコードに変更していたら変更後のもので)してもらえれば動きます。普通にビジュアルエディタの状態でコピペすればOKですよ。もちろん投稿記事の中でも使えます。

    後者の場合、orderby=post_dateは「投稿日時順にソート」を意味しているのではずしてしまうと最新○件にならなくなってしまいます。post_type=pageを追記すればページの最新○件になるはずです。

  9. igarashi5620
    メンバー
    2 years前の投稿 #

    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日
    テスト2

    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');
    		$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>&raquo;" . $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');
  10. shokun0803
    メンバー
    2 years前の投稿 #

    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度で記述します。

  11. igarashi5620
    メンバー
    2 years前の投稿 #

    shokun0803 さん

    ありがとうございました。
    下記のとおり追加いたしましたが、
    さらに
    $res = get_post_meta($my_post->ID, 'new', 'true');
    を入れたのですが、表示のしかたは
    $rep_contents .= "\t<li>&raquo;" . $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>&raquo;" . $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');
  12. shokun0803
    メンバー
    2 years前の投稿 #

    . $my_title . "" . $res .

    の部分が冗長ですね。間に何も文字を置く予定がないなら

    . $my_title . $res .

    でいいと思うのですけど。他に特に問題は見当たらなさそうですよ;)

  13. igarashi5620
    メンバー
    2 years前の投稿 #

    shokun0803 さん

    ありがとうございました。
    確認いたしました。
    大変お手数をおかけいたしました。

返信

ログイン しなければ投稿できません。

About this Topic

Tags

タグ はまだありません。