WordPressによるホームページ制作のCB-Web(シービーウェブ)

Memos 制作メモ

CB-Webホーム > 制作メモ > WordPress カスタム投稿タイプの追加手順

WordPress カスタム投稿タイプの追加手順2016.08.08更新

1 functions.phpに記述を追加する
2 管理画面にカスタム投稿のメニューが追加される(投稿できるようになる)
3 テンプレートファイルを整える

functions.php

function add_custom(
                    $cat_name, 
                    $cat_label,
                    $cat_slug,
                    $cat_show,
                    $post_name,
                    $post_label,
                    $post_position,
                    $post_slug ){
    register_taxonomy($cat_name,
        array($post_name),
        array(
            'label' => $cat_label,
            'show_ui' => $cat_show,
            'hierarchical' => true,
            'rewrite' => array('slug' => $cat_slug.'/category', 'with_front' => false)
    ));
    register_post_type($post_name, array(
        'label' => $post_label,
        'menu_position' => $post_position,
        'public' => true,
        'supports' => array('title', 'editor', 'thumbnail', 'custom-fields'),
        'has_archive' => true,
        'rewrite' => array('slug' => $post_slug, 'with_front' => false)
    ));
}
function add_custom_hogehoge(){
    add_custom(
               'cat_hogehoge',
               'ほげほげカテゴリ',
               'hogehoge',
               true,
               'post_hogehoge',
               'ほげほげ',
               5,
               'hogehoge');
}
add_action('init', 'add_custom_hogehoge');

テンプレートファイルの調整について

以下のテンプレートファイルを、必要に応じて修正・作成する。

taxonomy-cat_hogehoge.php
taxonomy.php
archive-post_hogehoge.php
archive.php
single-post_hogehoge.php
single.php
index.php

× CLOSE