📝

Custom Post Type UIでスライドショーを可変にする(多言語の場合にも対応)

2022/11/30に公開

introduction

今回紹介するのは、スライドショーを可変にするためにCustomPostTypeUIをもちいて、クライアント側が簡単にスライドショーを追加できるような実装を目標にする。あまり多くを語る気分ではないので、とりあえずやってみることにする。これは備忘録であるから、(記事の質) = f(俺のやる気)の単調増加関数であることはご了承してほしい。

事前準備

CustomPostTypeUIで新規投稿を設定する

まずはCustomPostTypeUIをインストールして、新規投稿を追加する。ちなみにインストールするのは以下のプラグインだ。

カスタムフィールドを設定して更に可変に

ACFを用いるともっと可変にできるので、是非ACFもインストールしておいてほしい、というか今回の記事はそれ有りきだ。以下のプラグインがおすすめ。

サブクエリで可変に出力する

これでサブクエリ出力ができる。

サブクエリの内容出力
<?php 
	$course_query = array(
		'post_type' =>'course_ja',
		'post_per_page' =>-1,
		'order' =>'ASC',
	);
	$course_query = new WP_Query($course_query);
	if($course_query->have_posts()):
	?>
	<?php
	while($course_query->have_posts()):
		$course_query->the_post();
		?>
		<!-- 出力する内容を記述 -->
		<p><?php echo get_field("modelcourse-title");?></p>
		<?php
	endwhile;
	wp_reset_postdata();
	?>
<?php endif;?>

orderを可変にする

orderを変える場合は、meta_keyでカスタムタクソノミーを新規作成して、そこからorderで数ソートを行うことで実現する。

order可変
<?php
	$paged = (int) get_query_var('paged');
	$args = array(
		'posts_per_page' => -1,
		'paged' => $paged,
		'meta_key' => 'year_month',
		'orderby' => 'meta_value_num',
		'order' => 'DESC',
		'post_type' => 'publication',
		'post_status' => 'publish',
		'hide_empty' => true,
		'tax_query' => array(
			array(
				'taxonomy' => 'publications_category',
				'field' => 'slug',
				'terms' => $value['term_name'],
			)
		)
	);
	$the_query = new WP_Query($args);
	if ($the_query->have_posts()) :
		$count = 1;
		while ($the_query->have_posts()) : $the_query->the_post();
	?>
			<li id="<?php the_ID(); ?>">
				<?php if (get_field('url_issue')) : ?>
					<a class="url_issue_a" href="<?php the_field('url_issue'); ?>" target="_blank">
					<?php endif; ?>
					<div class="deco_count">
						<?php echo str_pad($count, 3, 0, STR_PAD_LEFT); ?>
					</div><!-- /.deco_count -->
					<h2><?php the_title(); ?></h2>
					<div class="pub_detail">
						<p><?php the_field('author') ?></p>
						<p>
							<?php the_field('Publisher') ?>
						</p>
					</div><!-- /.pub_detail -->
					<?php if (get_field('url_issue')) : ?>
					</a>
				<?php endif; ?>
			</li>

	<?php $count++;
		endwhile;
	endif; ?>
</ul>
</div><!-- /.tab-inner -->
</div>
<?php endforeach; ?>

Discussion