TIPS NOTE(カスタム投稿)のしくみ
子テーマの functions.php に以下のコードを記載すると、専用のカスタム投稿タイプを作成できます。
URLは /tips/、サイト上の表示名は「TIPS NOTE」です。専用カテゴリー・専用タグも作成され、タグ一覧の表示やアーカイブページのタイトル・説明文にも対応しています。
URLや表示名などを変更する場合は、コード内の該当箇所を適宜変更してください。内部名を変更する場合は、関連するコード内の指定も合わせて変更する必要があります。
新サイトに導入するときの注意
- Astraの子テーマの
functions.phpに記載する。 - 「設定」→「パーマリンク」を開き、変更せずに「変更を保存」する。
- タグ一覧を表示したい場所には
[tips_tags]を記載する。
functions.php を編集する前に
functions.phpは、WordPressの動作に関わる重要なファイルです。記述を間違えると、Webサイトが表示されなくなったり、管理画面にログインできなくなったりする場合があります。
編集する前に、必ず現在のfunctions.phpの内容をバックアップしておきましょう。
また、コードを追加する際は、既存のコードを誤って削除したり変更したりしないよう注意してください。
コードを追加した後にエラーが発生した場合は、追加したコードを確認し、元の状態に戻してください。
functions.phpの編集は、内容を確認したうえで慎重に行ってください。
/**
* TIPS NOTE
* カスタム投稿タイプ・専用カテゴリー・専用タグ
* アーカイブタイトル・説明文・タグ一覧
*/
function register_tips_post_type() {
/*
* カスタム投稿タイプ
*/
register_post_type( 'tips', array(
'labels' => array(
'name' => 'TIPS NOTE',
'singular_name' => 'TIPS NOTE',
'add_new' => '新規追加',
'add_new_item' => 'TIPS NOTEを追加',
'edit_item' => 'TIPS NOTEを編集',
'new_item' => '新しいTIPS NOTE',
'view_item' => 'TIPS NOTEを表示',
'search_items' => 'TIPS NOTEを検索',
'not_found' => 'TIPS NOTEが見つかりません',
'not_found_in_trash' => 'ゴミ箱にTIPS NOTEはありません',
'all_items' => 'TIPS NOTE一覧',
'menu_name' => 'TIPS NOTE',
),
'public' => true,
'show_ui' => true,
'show_in_rest' => true,
'has_archive' => true,
// アーカイブURL:/tips/
'rewrite' => array(
'slug' => 'tips',
),
'menu_icon' => 'dashicons-admin-post',
'supports' => array(
'title',
'editor',
'thumbnail',
'excerpt',
'revisions',
),
'taxonomies' => array(
'tips_category',
'tips_tag',
),
) );
/*
* 専用カテゴリー
*/
register_taxonomy( 'tips_category', 'tips', array(
'labels' => array(
'name' => 'TIPSカテゴリー',
'singular_name' => 'TIPSカテゴリー',
'search_items' => 'カテゴリーを検索',
'all_items' => 'カテゴリー一覧',
'edit_item' => 'カテゴリーを編集',
'update_item' => 'カテゴリーを更新',
'add_new_item' => 'カテゴリーを追加',
'new_item_name' => '新しいカテゴリー名',
'menu_name' => 'カテゴリー',
),
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_rest' => true,
'hierarchical' => true,
'show_tagcloud' => false,
) );
/*
* 専用タグ
*/
register_taxonomy( 'tips_tag', 'tips', array(
'labels' => array(
'name' => 'TIPSタグ',
'singular_name' => 'TIPSタグ',
'search_items' => 'タグを検索',
'all_items' => 'タグ一覧',
'edit_item' => 'タグを編集',
'update_item' => 'タグを更新',
'add_new_item' => 'タグを追加',
'new_item_name' => '新しいタグ名',
'separate_items_with_commas' => 'タグをコンマで区切る',
'add_or_remove_items' => 'タグを追加または削除',
'menu_name' => 'タグ',
),
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_rest' => true,
'hierarchical' => false,
// 通常のタグクラウドには表示しない
'show_tagcloud' => false,
) );
}
add_action( 'init', 'register_tips_post_type' );
/*
* 専用タグ一覧ショートコード
* 使用方法:[tips_tags]
*/
function tips_tag_list() {
$terms = get_terms( array(
'taxonomy' => 'tips_tag',
'hide_empty' => true,
) );
if ( is_wp_error( $terms ) || empty( $terms ) ) {
return '';
}
$output = '<div class="tips-tag-list">';
foreach ( $terms as $term ) {
$url = get_term_link( $term );
if ( is_wp_error( $url ) ) {
continue;
}
$output .= '<a href="' . esc_url( $url ) . '">'
. esc_html( $term->name )
. '</a>';
}
$output .= '</div>';
return $output;
}
add_shortcode( 'tips_tags', 'tips_tag_list' );
/*
* TIPS NOTE アーカイブタイトル
*/
function tips_archive_title( $title ) {
if ( is_post_type_archive( 'tips' ) ) {
return 'TIPS NOTE';
}
return $title;
}
add_filter( 'get_the_archive_title', 'tips_archive_title' );
/*
* TIPS NOTE アーカイブ説明文
*/
function tips_archive_description( $description ) {
if ( is_post_type_archive( 'tips' ) ) {
return 'ここに説明文を記載';
}
return $description;
}
add_filter( 'get_the_archive_description', 'tips_archive_description' );
Tips 記事検索