wordpress カスタム投稿のタームをカスタムフィールドテンプレートを使用して画像を一覧表示 2012年9月6日
Tag: | | |

 カスタム投稿のタームの画像一覧をカスタムフィールドテンプレートを使用して、表示したい場合

1.taxonomy-分類名.phpで出力るす場合

<div class="img-box">
<ul>
<?php
$args = array(
'post_type' => 'topics',
'taxonomy' => 'info',
'term' => 'event',
'numberposts' => '-1',
);
$my_posts = get_posts($args);
foreach ( $my_posts as $post ) {
setup_postdata($post); ?>

<?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="118px" height="<?php echo $h; ?>" />
</li>
<?php
}
?>
</ul>
</div>

page.phpで出力るす場合

<?php
$args = array(
    'post_type' => 'topics',
    'posts_per_page' => 50,
    'tax_query' => array( /* カスタム分類 */
            array(
                'taxonomy' => 'info',
                'field' => 'slug',
                'terms' => 'event',
            )
        )
    );
?>
<?php $loop = new WP_Query($args); ?>
<div class="jutaku-box">
<ul>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?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><div class="jutaku-img-box"><a href="<?php the_permalink() ?>"><img src="<?php echo $url; ?>" alt="<?php echo $alt; ?>" width="118px" height="<?php echo $h; ?>" /></a></div>
</li>
<?php endwhile; ?>
</ul>

2.pageなどでタームをループさせて一覧を表示したい場合

<?php $tax_posts = get_posts('post_type=カスタムタイプ&taxonomy=カスタム分類&term=ターム&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; ?>

3.複数のタームをループさせて一覧を表示したい場合

<?php
    $args=array(
        'tax_query' => array(
            array(
                'taxonomy' => 'newsoll', //タクソノミーを指定
                'field' => 'slug', //ターム名をスラッグで指定する
                'terms' => array( 'event','report','information' ) //表示したいタームをスラッグで指定
            ),
        ),
        'post_type' => 'news', //カスタム投稿名
        'posts_per_page'=> 5 //表示件数(-1で全ての記事を表示)
    );
 ?>
<?php query_posts( $args ); ?>
<?php if(have_posts()): ?>
<?php while(have_posts()):the_post(); ?>
 
ページが存在する場合の指定
 
<?php endwhile; else: ?>
 
ページが存在しない場合の指定
 
<?php endif; ?>
<?php wp_reset_query(); ?>

New

Aechive

Tag

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