wordpress カスタム投稿の関連記事一覧を表示 2012年9月4日
Tag: | | |

single.phpに同じカスタム投稿の記事一覧を表示する方法

1.カスタム分類

例えばお知らせというカスタムタイプの中に
イベントと見学会と言うカスタム分類を作成した場合
イベントの記事下に同じイベントに属する記事を表示したい場合

964-2

2.Custom Field Templateを使用して画像を出力したい場合

<ul>
<?php
$args = array(
	'post_type' => 'topics',
	'post__not_in' =>  array($post->ID),
	'orderby' => 'rand',
	'posts_per_page' => 4,
	'tax_query' => array( /* カスタム分類 */
		'relation' => 'OR',
			array(
				'taxonomy' => 'event',
				'field' => 'slug',
				'terms' => $recipelist_slug,
			)
		)
	);
?>
<?php
$attach_id = get_post_meta($post->ID,"メイン画像",true);
$image_info = wp_get_attachment_image_src( $attach_id  , 'full' );
list( $url, $w, $h) = $image_info;
$h = intval(200 * ( $h / $w ));
$alt = get_post_meta($attach_id , '_wp_attachment_image_alt', true);
?>
<li><a href="<?php the_permalink() ?>"><img src="<?php echo $url; ?>" alt="<?php echo $alt; ?>" width="88px" /></a></li>

</ul>
‘post_type’ => ‘products’ // 投稿タイプ ‘products’ を指定します。
‘post__not_in’ => array($post->ID) // 今表示されている投稿を除きます。
‘orderby’ => ‘rand’ // 表示するものをランダムにします。
‘posts_per_page’ => 6 // 表示される最大の数です。

‘taxonomy’ => ‘event’, // カスタム分類’event’ を指定します。

2.カスタム分類-ターム

イベントというカスタム分類の中に
モデルハウスと展示会と言うタームを作成した場合
モデルハウスの記事下に同じモデルハウスに属する記事を表示したい場合

964-4

1.Custom Field Templateを使用して画像を出力したい場合(パターン1)

964-5
<ul>
<?php
$args = array(
'post_type' => 'topics',
'taxonomy' => 'event',
'term' => 'model',
'numberposts' => '-1',
);
$my_posts = get_posts($args);
foreach ( $my_posts as $post ) {
setup_postdata($post); ?>
<li><?php
$attach_id = get_post_meta($post->ID,"メイン画像",true);
$image_info = wp_get_attachment_image_src( $attach_id  , 'full' );
list( $url, $w, $h) = $image_info;
$h = intval(200 * ( $h / $w ));
$alt = get_post_meta($attach_id , '_wp_attachment_image_alt', true);
?>
<li><a href="<?php the_permalink() ?>"><img src="<?php echo $url; ?>" alt="<?php echo $alt; ?>" width="88px" /></a></li></li>
<?php
}
?>
</ul>
‘post_type’ => ‘topics’, //カスタムタイプを指定
‘taxonomy’ => ‘event’, //カスタム分類を指定
‘term’ => ‘model’, // タームを指定

2.カスタム分類-ターム(パターン2)
1.Custom Field Templateを使用して画像を出力したい場合

<?php $tax_posts = get_posts('post_type=topics&taxonomy=info&term=event&posts_per_page=3'); if($tax_posts): ?>
<?php
$attach_id = get_post_meta($post->ID,"メイン画像",true);
$image_info = wp_get_attachment_image_src( $attach_id  , 'full' );
list( $url, $w, $h) = $image_info;
$h = intval(200 * ( $h / $w ));
$alt = get_post_meta($attach_id , '_wp_attachment_image_alt', true);
?>
<li><a href="<?php the_permalink() ?>"><img src="<?php echo $url; ?>" alt="<?php echo $alt; ?>" width="88px" /></a></li>
 <?php endforeach; ?>

3.タイトルのみ出力したい場合
964-3

<?php $tax_posts = get_posts('post_type=qa&taxonomy=interior&term=furniture&posts_per_page=3'); if($tax_posts): ?>
	<ul>
		<?php foreach($tax_posts as $tax_post): ?>
		<li><a href="<?php echo get_permalink($tax_post->ID); ?>"><?php echo esc_html($tax_post->post_title); ?></a></li>
		<?php endforeach; ?>
	</ul>
<?php endif; ?>

4.カスタム分類-ターム(パターン3)
トピックスという投稿タイプにイベントというカスタム分類を作成し、
イベントの分類の中にモデルハウスと展示会と言うタームを作成した場合、single.phpの記事下に同じタームに属する記事を自動判別しリンク付きタイトルリストを表示したい。

<?php
$term = array_shift(get_the_terms($post->ID, 'event'));
?>
<?php $tax_posts = get_posts('post_type=topics&taxonomy=event&term='.esc_html($term->slug));  if($tax_posts): ?>
<ul>
	<?php foreach($tax_posts as $tax_post): ?>

	<li><a href="<?php echo get_permalink($tax_post->ID); ?>"><?php echo esc_html($tax_post->post_title); ?></li>
	<?php endforeach; ?>
	</ul>
<?php endif; ?>

5.カスタム投稿タイプの記事をランダムで表示したい(パターン4)

<?php
$loop = new WP_Query( array( 'post_type' => 'カスタム投稿タイプ名', 'posts_per_page' => 表示させる件数, 'orderby' =>rand ) );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<ul>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
</ul>
<?php endwhile;wp_reset_query(); ?>

New

Aechive

Tag

css Facebook facebookカスタマイズ Facebookページ font functions.php google NEWマーク RSS single.php webツール WEBデザインツール Wordpress アーカイブ カスタム カスタムフィールド カスタムフィールドテンプレート カスタム投稿 カテゴリー カレンダー サイドバー ターム テキスト テンプレートタグ プラグイン ヘッダ ページ ポップアップ メニュー 並び替え 並べ替え 人気 便利 分岐 印刷 年別アーカイブ 投稿 投稿記事 改行 文字数 文法チェック 最新記事 月別アーカイブ 特定カテゴリー 画像