web-dev-qa-db-ja.com

特定のページに特定の投稿を表示するためのプラグインはありますか?

ホームページには 'news'と 'events'のカテゴリーからの最新の4つの投稿を表示したいのですが、イベントページにはWPの通常のページ区切り形式ですべての投稿を 'events'カテゴリーに表示したいですたくさんあります。

この種のルールベースのポスト生成を処理するプラグインを知っていますか?そうでなければ、これを回避するための最善の方法は何でしょうか?

3
Andy Cheeseman

あなたはたぶん、この種のことのためにWordpressの組み込みのクエリの1つを使うことを考えて、あなた自身でいくつかのカスタムループを作成するべきです。おそらくあなたのためにこれを行うことができるそこにプラグインがありますが、一般的な原則として可能な限りサードパーティのスクリプトへの依存を減らすことをお勧めします。

上で説明したことを実行するには、おそらく次のようなループが必要になります(これをホームページのテンプレートにドロップします)。

// This is where we set up the parameters for your custom loop.  In the example below you would want to swap out the category ID numbers with the IDs for your News and Events cateogries
<?php $my_query = new WP_Query( 'cat=2,6&post_per_page=4' );?>

// The Loop
<?php if($my_query->have_posts()) : while ( $my_query->have_posts() ) : $my_query->the_post(); ?>

//Add template tags and other stuff here that you want to show up for each post

<?php endwhile; else: ?>
    <p>Sorry - No posts to display.</p>
<?php endif; wp_reset_query();?>

もう一方のループを実現するには、上とほぼ同じことが必要ですが、最初の行を少し変更する必要があります。これでうまくいくはずです(これをあなたのカスタムページテンプレートに入れてください):

//This adds in the pagination that you require.  
//Once again, you will need to modify the category ID to match the ID of the one you want to display.  
//You can also Tweak the other parameters to suit your requirements
<?php $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
query_posts( array( 'cat' => '12', 'posts_per_page' => 10, 'orderby' => 'date', 'order' => 'DESC', 'paged' => $paged ) ); ?>

WP-Queryの詳細はこちらにあります。

http://codex.wordpress.org/Class_Reference/WP_Query

お役に立てれば!

1
FourStacks