wordpress カスタム投稿のカテゴリ・ターム記事の出力

Filed under: functions.php,wordpress — kdcs @ 16年5月17日 火曜日

カスタム投稿タイプを作成し、タクソノミー(カテゴリ)やタームを新規で追加できるようにした場合の出力方法
トップページや固定ページ内にカスタム投稿のタームによる分類で出力する

function.phpへの記述でカスタム投稿タイプを「hogehoge」タクソノミーを「hogehoge_category」タームを「example」にした場合

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

デフォルトの投稿タイプの場合、投稿タイプは「post」タクソノミーは「category」となる
以下はデフォルト投稿タイプの場合の記述

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

wordpressカスタム投稿タイプ

Filed under: functions.php,wordpress — kdcs @ 16年4月23日 土曜日

記事を作成するときに分類して管理する方が便利な時がある
デフォルトではメニューが「投稿」のみなのでタイプ分けをして記事を分類します
(続きを読む…)

サイト内検索

カテゴリー

最近の投稿

↑上に戻る