LoginSignup
5
8

More than 5 years have passed since last update.

WordPressで『カスタム投稿』ページを作る方法

Last updated at Posted at 2018-04-21

『カスタム投稿タイプ』を作成する方法をメモ。
今回は「お知らせ」と「イベント」というカスタム投稿を作成します。

1、「functions.php」 に追記


<?php

//カスタム投稿タイプ

add_action( 'init', 'create_post_type' );
function create_post_type() {

//カスタム投稿タイプ1
    register_post_type( 'news', //カスタム投稿タイプ名。自由指定可。
       array(
            'label' => 'お知らせ', //WP管理画面のサイドバーに表示するカスタム投稿タイプ名。自由指定可
            'labels' => array(
            'all_items' => 'お知らせ一覧', //記事一覧ページの名前。自由指定可。
                           ),
            'menu_position' => 4, //サイドバーでの表示順序
            'public' => true,
            'has_archive' => true,
            'supports' => array('title','editor','excerpt','thumbnail','author')
        )
    );

//カスタム投稿タイプ2
    register_post_type( 'event', //カスタム投稿タイプ名。自由指定可。
       array(
            'label' => 'イベント', //WP管理画面のサイドバーに表示するカスタム投稿タイプ名。自由指定可
            'labels' => array(
            'all_items' => 'イベント一覧', //記事一覧ページの名前。自由指定可。
                           ),
            'menu_position' => 5, //サイドバーでの表示順序
            'public' => true,
            'has_archive' => true,
            'supports' => array('title','editor','excerpt','thumbnail','author')
        )
    );


//カスタム投稿タイプのカテゴリ1
  register_taxonomy(
                   'news-cat', //備考1:のちほど「archive-event.php」にも必要です
                   'news', //カテゴリのスラッグ
              array(
                   'label' => 'カテゴリ一覧', //WP管理画面のサイドバーに表示する名前。自由指定可。
                   'labels' => array(
                   'add_new_item' => '新規追加', //WP管理画面のサイドバーに表示する新規追加ボタンの名前。自由指定可。
                   ),
                   'hierarchical' => true
                   )
                   );   


//カスタム投稿タイプのカテゴリ2
  register_taxonomy(
                   'event-cat', //備考2:のちほど「archive-event.php」にも必要です
                   'event', //カテゴリのスラッグ
              array(
                   'label' => 'カテゴリ一覧', //WP管理画面のサイドバーに表示する名前。自由指定可。
                   'labels' => array(
                   'add_new_item' => '新規追加', //WP管理画面のサイドバーに表示する新規追加ボタンの名前。自由指定可。
                   ),
                   'hierarchical' => true
                   )
                   );   
?>

2、「archive-news.php」と「archive-event.php」を作成

archive-news.php

<?php if (have_posts()) : ?>
    <?php query_posts($query_string . "&showposts=20"); ?>  <!--1ページあたりの表示件数を指定-->
    <?php while (have_posts()) : the_post(); ?>

        <?php
        if ($terms = get_the_terms($post->ID, 'news-cat')) { //備考1で作成したIDを記入
            foreach ( $terms as $term ) {
                echo esc_html($term->name);
            }
            }
        ?> //カテゴリ

        <?php the_permalink(); ?> <!--記事のサムネイル-->
        <?php the_time('Y.m.d'); ?> <!--記事の投稿日時-->
        <?php the_title(); ?> <!--記事タイトル-->

    <?php endwhile; ?>

    <?php previous_posts_link(); ?>
    <?php next_posts_link(); ?>

<?php else: ?>
    <p>現在お知らせはありません。</p> <!--記事がない場合-->
<?php endif; ?>

<?php pagenavi(); ?>

archive-event.php

<?php if (have_posts()) : ?>
    <?php query_posts($query_string . "&showposts=20"); ?> <!--1ページあたりの表示件数を指定-->
    <?php while (have_posts()) : the_post(); ?>

        <?php
        if ($terms = get_the_terms($post->ID, 'event-cat')) { //備考2で作成したIDを記入
            foreach ( $terms as $term ) {
                echo esc_html($term->name);
            }
            }
        ?> //カテゴリ

        <?php the_permalink(); ?> <!--記事のサムネイル-->
        <?php the_time('Y.m.d'); ?> <!--記事の投稿日時-->
        <?php the_title(); ?> <!--記事のタイトル-->

    <?php endwhile; ?>

    <?php previous_posts_link(); ?>
    <?php next_posts_link(); ?>

<?php else: ?>
    <p>現在お知らせはありません。</p> <!--記事がない場合-->
<?php endif; ?>

<?php pagenavi(); ?>

3、 「single-news.php」と「single-event.php」 を作成

single-news.php
<?php
/**
 * Template Name:news
 * Template Post Type:news
 */
?>
<?php while (have_posts()) : the_post(); ?>
    <?php the_title(); ?> <!--記事のタイトル-->
    <?php the_time('Y.m.d'); ?> <!--記事の投稿日時-->
    <?php the_content(); ?> <!--記事の投稿内容-->

    <?php if(has_post_thumbnail()): ?>
        <?php the_permalink(); ?>" ><?php the_post_thumbnail('medium'); ?>
    <?php endif; ?> <!--記事のサムネイル-->

<?php endwhile; ?>
<?php pagenavi(); ?>

single-event.php
<?php
/**
 * Template Name:event
 * Template Post Type:event
 */
?>
<?php while (have_posts()) : the_post(); ?>
    <?php the_title(); ?> <!--記事のタイトル-->
    <?php the_time('Y.m.d'); ?> <!--記事の投稿日時-->
    <?php the_content(); ?> <!--記事の投稿内容-->

    <?php if(has_post_thumbnail()): ?>
        <?php the_permalink(); ?>" ><?php the_post_thumbnail('medium'); ?>
    <?php endif; ?> <!--記事のサムネイル-->

<?php endwhile; ?>
<?php pagenavi(); ?>

5
8
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
8