例えばお知らせというカスタムタイプの中に
イベントと見学会と言うカスタム分類を作成した場合
イベントの記事下に同じイベントに属する記事を表示したい場合
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>
‘taxonomy’ => ‘event’, // カスタム分類’event’ を指定します。
イベントというカスタム分類の中に
モデルハウスと展示会と言うタームを作成した場合
モデルハウスの記事下に同じモデルハウスに属する記事を表示したい場合
1.Custom Field Templateを使用して画像を出力したい場合(パターン1)
<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>
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; ?>
<?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(); ?>