wordpressで美容系サロン・エステのWEBサイト制作
step.1
テーマに店員の投稿を設定
step.1-1
カスタム投稿とカテゴリ・タグの追加するため、function.phpに記載
//カスタム投稿
add_action('init', 'create_post_type');
function create_post_type()
{
register_post_type('clerk', [ // 投稿タイプ名の定義
'labels' => [
'name' => '店員', // 管理画面上で表示する投稿タイプ名
'singular_name' => 'clerk', // カスタム投稿の識別名
],
'public' => true, // 投稿タイプをpublicにするか
'has_archive' => false, // アーカイブ機能ON/OFF
'menu_position' => 5, // 管理画面上での配置場所
'show_in_rest' => true, // 5系から出てきた新エディタ「Gutenberg」を有効にする
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
]);
}
//カスタム投稿のタグとカテゴリーを共通に
add_action('init', function () {
register_taxonomy(
'post_tag',
[ 'post', 'clerk' ],
[
'hierarchical' => false,
'query_var' => 'tag',
'show_in_rest' => true, //管理画面にタクソノミー
]
);
register_taxonomy(
'category',
[ 'post', 'clerk' ],
[
'hierarchical' => true, // カテゴリーの場合はtrue
'query_var' => 'category_name',
'show_in_rest' => true, //管理画面にタクソノミー
]
);
});
add_action('pre_get_posts', function ($query) {
if (is_admin() && ! $query->is_main_query()) {
return;
}
if ($query->is_category() || $query->is_tag()) {
$query->set('post_type', [ 'post', 'clerk' ]);
}
});
step.1-2
カスタム投稿のテンプレートの用意
step.2
店員一覧にも本日の出勤を表示 カテゴリで店員一覧を作成 店員一覧の子カテゴリで本日の出勤カテゴリを作成 取得方法:
<?php // 記事のカテゴリー情報を取得する
$cat = get_the_category();// 取得した配列から必要な情報を変数に入れる
$cat_name = $cat[0]->cat_name; // カテゴリー名
$cat_slug = $cat[0]->category_nicename; // カテゴリースラッグ
?>
<?php //子カテゴリースラッグを取得
$cats = get_the_category();
foreach ($cats as $cat):
if ($cat->parent) {
echo $cat->slug;
}
endforeach;?>
step.3
トップページに本日出勤とランキングを表示
step.3-1
本日の出勤
<?php
// the query
$query = new WP_Query(
array(
'post_type' => 'clerk',
'posts_per_page' => 5,
'category__in' => array( 7 ),//カテゴリID
)
);
?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- pagination here -->
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php wp_reset_postdata(); ?>
step.3-2
ランキング ランキングは新しいものから表示
<?php
/* (ステップ1)データの取得 */
$query = new WP_Query(
array(
'post_type' => 'clerk',
'posts_per_page' => 4,
'category__in' => array( 1 ),//カテゴリID
)
);
?>
<?php
/* (ステップ2)データの表示 */
if ( $query->have_posts() ) : ?>
<div class="row">
<?php while ( $query->have_posts() ) : $query->the_post();?>
<!-- pagination here -->
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<span class="badge badge-primary">№<?php echo $query->current_post + 1; ?></span> <?php the_title(); ?>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php wp_reset_postdata(); ?>