wordpressカスタム投稿タイプ

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

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

◆ダッシュボードメニューに任意の投稿名(カスタム投稿タイプ)を設置する
function.php
任意の名前を付ける 「example」「カスタム名」

// カスタム投稿タイプを作成する-----------------------------------------------
add_action('init', 'add_example_post_type');
function add_example_post_type() {
$params = array(
'labels' => array(
'name' => 'カスタム名',
'singular_name' => 'カスタム名',
'add_new' => '新規追加',
'add_new_item' => 'カスタム名を新規追加',
'edit_item' => 'カスタム名を編集する',
'new_item' => '新規カスタム名',
'all_items' => 'カスタム名一覧',
'view_item' => 'カスタム名を見る',
'search_items' => '検索する',
'not_found' => 'カスタム名が見つかりませんでした。',
'not_found_in_trash' => 'ゴミ箱内にカスタム名が見つかりませんでした。'
),
'public' => true,
'has_archive' => true,
'supports' => array(
'title',
'editor',
'author',
'custom-fields',
),
'taxonomies' => array('example_category','example_tag')
);
register_post_type('example', $params);
}

◆カスタム投稿タイプのカテゴリーを作成する

// カスタム投稿タイプ用のカテゴリを作成する-----------------------------------
add_action('init', 'create_example_taxonomies');
function create_example_taxonomies() {
// カテゴリを作成
$labels = array(
'name' => 'カテゴリ', //複数系のときのカテゴリ名
'singular_name' => 'カテゴリ', //単数系のときのカテゴリ名
'search_items' => 'カテゴリを検索',
'all_items' => '全てのカテゴリ',
'parent_item' => '親カテゴリ',
'parent_item_colon' => '親カテゴリ:',
'edit_item' => 'カテゴリを編集',
'update_item' => 'カテゴリを更新',
'add_new_item' => '新規カテゴリを追加',
'new_item_name' => '新規カテゴリ',
'menu_name' => 'カテゴリ' //ダッシュボードのサイドバーメニュー名
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'rewrite' => array( 'slug' => 'example_cat' )
);
register_taxonomy( 'example_category', 'example', $args );
}

◆カスタム投稿タイプのパーマリンクをカスタム仕様にする(カテゴリー名/post_id.html)

// カスタム投稿タイプのパーマリンク設定---------------------------------------
add_action('init', 'myposttype_rewrite');
function myposttype_rewrite() {
global $wp_rewrite;

$queryarg = 'post_type=example&p=';
$wp_rewrite->add_rewrite_tag('%example_id%', '([^/]+)',$queryarg);
$wp_rewrite->add_permastruct('example', '/example/%example_id%.html', false);

}
add_filter('post_type_link', 'myposttype_permalink', 1, 3);
function myposttype_permalink($post_link, $id = 0, $leavename) {
global $wp_rewrite;
$post = get_post($id);
if ( is_wp_error( $post ) )
return $post;
$newlink = $wp_rewrite->get_extra_permastruct($post->post_type);
$newlink = str_replace('%'.$post->post_type.'_id%', $post->ID, $newlink);
$newlink = home_url(user_trailingslashit($newlink));
return $newlink;
}

※14行目$post = &get_post($id)の&を外す(記述ミス?)2020.1.17

◆カスタム投稿タイプを出力する記述
例:記事タイトルにパーマリンクを設定した一覧を最大5件表示する

<ul>
<ul><!--?php $args = array( 'numberposts' =&gt; 5,                //表示(取得)する記事の数&lt;br ?--> 'post_type' =&gt; 'post-type-name' //投稿タイプの指定</ul>
</ul>
&nbsp;
<ul>
<ul>);</ul>
</ul>
&nbsp;
<ul>
<ul>$customPosts = get_posts($args);</ul>
</ul>
&nbsp;
<ul>
<ul>if($customPosts) : foreach($customPosts as $post) : setup_postdata( $post ); ?&gt;
	<li><!--?php the_title(); ?--></li>
</ul>
</ul>
<!--?php endforeach; ?-->
<!--?php else : //記事が無い場合 ?-->
<ul>
<ul>
	<li>記事はまだありません。</li>
</ul>
</ul>
&nbsp;

◆投稿一覧(カスタム投稿)の項目にカテゴリーを追加する
※管理画面のカスタム投稿タイプの投稿一覧にはデフォルトでカテゴリーが無い

上記、カスタム投稿用カテゴリーのスラッグを入力します

function add_custom_column( $defaults ) {
$defaults['カスタム投稿のカテゴリースラッグ'] = 'カテゴリー';
return $defaults;
}
add_filter('manage_[カスタム投稿タイプ]_posts_columns', 'add_custom_column');

function add_custom_column_id($column_name, $id) {
if( $column_name == 'カスタム投稿のカテゴリースラッグ' ) {
echo get_the_term_list($id, 'カスタム投稿のカテゴリースラッグ', '', ', ');
}
}
add_action('manage_[カスタム投稿タイプ]_posts_custom_column', 'add_custom_column_id', 10, 2);

※カスタム投稿タイプ前後の[]は外す。
※カスタム投稿のカテゴリースラッグは「example_category」の部分

◆カスタム投稿の一覧表示にカテゴリーの絞り込み検索を追加する

add_action( 'restrict_manage_posts', 'add_post_taxonomy_restrict_filter' );
function add_post_taxonomy_restrict_filter() {
    global $post_type;
    if ( 'カスタム投稿タイプ' == $post_type ) {
        ?>
        <select name="color">
            <option value="">カテゴリー指定なし</option>
            <?php
            $terms = get_terms('カスタム投稿のカテゴリースラッグ');
            foreach ($terms as $term) { ?>
                <option value="<?php echo $term->slug; ?>"><?php echo $term->name; ?></option>
            <?php } ?>
        </select>
        <?php
    }
}
?> ←これはこの記事に記述するための?>ですfunctions.phpに記述するときは必要ない

◆投稿一覧の項目並び順を変更する
例:カスタム投稿タイプ「hogehoge」カスタムタクソノミー(カテゴリー)「custom_category」
カスタムフィールド「memo 備考」その他、投稿IDを使用している場合の記述
  ※この時「price 販売価格」の表示指定をしていたが、並び順に入れないことにより非表示となった

function sort_posts_columns($columns){
	$columns = array(
		'cb' => '<input type="checkbox" />',
		'title' => 'タイトル',
		'author' => '作成者',
                'custom_category' => 'カテゴリー',
		'date' => '日時',
        'memo' => '備考',
        'postid' => 'ID'
	);
	return $columns;
}
add_filter( 'manage_hogehoge_posts_columns', 'sort_posts_columns');

◆トップページのインフォメーションにカスタム投稿の「タイトル」「カテゴリー」をリンク付で挿入する
例:カスタム投稿「hogehoge」カスタムタクソノミー(カテゴリー)「hogehoge_category」5件表示
※レスポンシブデザインとしてcss「pc_inline」と「sp_inline」で切り換え

<ul>
    <?php $args = array(
        'numberposts' => 5,                //表示(取得)する記事の数
        'post_type' => 'hogehoge'    //投稿タイプの指定
    );
    $customPosts = get_posts($args);
    if($customPosts) : foreach($customPosts as $post) : setup_postdata( $post ); ?>
<li><a class="newmarkList" title="<?php the_time('Y年m月d日 H時i分s秒 に更新'); ?>" href="<?php the_permalink(); ?>"><span class="infoData"><?php the_time('Y年m月d日'); ?> <span class="font10 sp_inline">【<?php if ($terms = get_the_terms($post->ID, 'hogehoge_category')) { foreach ( $terms as $term ) { echo esc_html($term->name)  ; }} ?>】</span></span><?php  the_title(); ?><span class="font10 pc_inline">【<?php if ($terms = get_the_terms($post->ID, 'hogehoge_category')) { foreach ( $terms as $term ) { echo esc_html($term->name)  ; }} ?>】</span></a></li>
    <?php endforeach; ?>
    <?php else : //記事が無い場合 ?>
        <li><p>記事はまだありません。</p></li>
    <?php endif;
    wp_reset_postdata(); //クエリのリセット ?>
</ul>

◆トップページのインフォメーションにカスタム投稿の特定のカテゴリー(ターム)のみ表示させる

<ul>
    <?php $args = array(
        'post_type' => 'hogehoge',    //投稿タイプの指定
        'taxonomy' => 'hogehoge_category', //functions.phpで設定したタクソノミー
        'term' => 'hoge_term', //記事分類用に作成したカテゴリー名(スラッグ)
        'numberposts' => 5                //表示(取得)する記事の数
    );
    $customPosts = get_posts($args);
    if($customPosts) : foreach($customPosts as $post) : setup_postdata( $post ); ?>
<li><a class="newmarkList" title="<?php the_time('Y年m月d日 H時i分s秒 に更新'); ?>" href="<?php the_permalink(); ?>"><span class="infoData"><?php the_time('Y年m月d日'); ?> <span class="font10 sp_inline">【<?php if ($terms = get_the_terms($post->ID, 'hogehoge_category')) { foreach ( $terms as $term ) { echo esc_html($term->name)  ; }} ?>】</span></span><?php  the_title(); ?><span class="font10 pc_inline">【<?php if ($terms = get_the_terms($post->ID, 'hogehoge_category')) { foreach ( $terms as $term ) { echo esc_html($term->name)  ; }} ?>】</span></a></li>
    <?php endforeach; ?>
    <?php else : //記事が無い場合 ?>
        <li><p>記事はまだありません。</p></li>
    <?php endif;
    wp_reset_postdata(); //クエリのリセット ?>
</ul>

※カスタム投稿タイプではなく、通常の投稿(’post_type’ => ‘post’)の場合、taxonomyはcategory(’taxonomy’ => ‘category’)になる

☆2018年12月15日に追記
※カスタム投稿タイプを複数設定し、それぞれカスタム投稿の一覧にカテゴリーを表示させたり並び順を変更する場合は関数名を変える
【カテゴリーを表示させる場合】
ひとつ目のカスタム投稿タイプでは「add_custom_column」次(2番目)は「add_custom_column2」のように末尾に数字を付けて関数名を変える

//カスタム投稿タイプ「hogehoge」のコラム一覧にカテゴリーを表示させる------------------------☆
function add_custom_column2( $defaults ) {
$defaults['hogehoge_category'] = 'カテゴリー';
return $defaults;
}
add_filter('manage_hogehoge_posts_columns', 'add_custom_column2');

function add_custom_column_id2($column_name, $id) {
if( $column_name == 'hogehoge_category' ) {
echo get_the_term_list($id, 'hogehoge_category', '', ', ');
}
}
add_action('manage_hogehoge_posts_custom_column', 'add_custom_column_id2', 10, 2);

【一覧の並び順を変える】
ひとつ目のカスタム投稿タイプでは「sort_posts_column」次は「sort_posts_column2」のように末尾に数字を付けて関数名を変える

//カスタム投稿「hogehoge」一覧の並び順を変更する--------------------------------------------☆
function sort_posts_columns2($columns){
	$columns = array(
		'cb' => '<input type="checkbox">',
		'title' => 'タイトル',
                'hogehoge_category' => 'カテゴリー',
		'author' => '作成者',
		'date' => '日時',
	);
	return $columns;
}
add_filter( 'manage_hogehoge_posts_columns', 'sort_posts_columns2');

サイト内検索

カテゴリー

最近の投稿

« |wordpressカスタム投稿タイプ| »
↑上に戻る