2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

wordpress頻出コード一覧備忘録

Posted at

wordpressで使用したphpコードを備忘録として残しておく

home.php、archive.phpなど記事の一覧を表示するテンプレートファイル

メインクエリで取得したデータから投稿を1つ1つ取り出して処理するコードです。例えばお知らせ一覧のページをhome.phpで表示する場合、以下のようなコードを記述することで記事一覧を表示することができます。

<?php if ( have_posts() ) : ?>
	<ul>
		<?php while ( have_posts() ) : ?>
			<?php the_post(); ?>
			<li>
				<a href="<?php the_permalink(); ?>">
					<div class="thumbnail">
						<?php if ( has_post_thumbnail() ) { ?>
							<?php the_post_thumbnail(); ?>
						<?php } else { ?>
							<img src="<?php echo esc_url(   get_theme_file_uri('/img/noimage.jpg') ); ?>">
						<?php } ?>
					</div>
					<div class="date"><?php echo get_the_date(); ?></div>
					<?php $terms = get_the_terms($post->ID, 'category'); ?>
					<?php if ( is_array($terms) ) : ?>
						<div class="terms">
							<?php
							foreach ( $terms as $term ) {
								echo '<span>' . $term->name . '</span>';
							}
							?>
						</div>
					<?php endif; ?>
					<div class="title"><?php the_title(); ?></div>
					<div class="excerpt"><?php the_excerpt(); ?></div>
				</a>
			</li>
		<?php endwhile; ?>
	</ul>

page.php、page-XXX.phpなど固定ページを表示するテンプレートファイル

管理画面の「固定ページ」で作成した内容をテンプレートファイルで表示するコードです。


<?php if ( have_posts() ) : ?>
	<?php while ( have_posts() ) : ?>
		<?php the_post(); ?>
		<?php the_title() ?>
		<?php the_content() ?>
	<?php endwhile; ?>
<?php endif; ?>

single.php、single-XXX.phpなど記事個別ページを表示するテンプレートファイル

「デフォルトの投稿」や「カスタム投稿タイプ」で作成した記事をテンプレートファイルで表示するコードです。


<?php if ( have_posts() ) : ?>
	<?php while ( have_posts() ) : ?>
		<?php the_post(); ?>
		<h1 class="title"><?php the_title(); ?></h1>
		<div class="date"><?php echo get_the_date() ?></div>
		<?php $terms = get_the_terms($post->ID, 'category'); ?>
		<?php if ( is_array($terms) ) { ?>
			<div class="category">
				<?php foreach ( $terms as $term ) { ?>
					<a href="<?php echo get_term_link($term) ?>"><?php echo $term->name ?></a>
				<?php } ?>
			</div>
		<?php } ?>
		<div class="content"><?php the_content(); ?></div>
		<?php previous_post_link() ?>
		<?php next_post_link() ?>
	<?php endwhile; ?>
<?php endif; ?>

メインクエリの条件を変更する

コード例はメインクエリを変更して、投稿一覧ページの表示件数を個別に変更する方法です。
カスタム投稿タイプを作成して様々な種類の投稿一覧ページが必要になった時、それぞれの投稿一覧ページの表示件数を変更したい場合があります。そのような投稿一覧ページの表示件数を個別に変更したい場合などで使います。


function change_set_post($query) {
	if ( is_admin() || !$query->is_main_query() ) {
		return;
	}
	if ( $query->is_home() ) {
		$query->set('posts_per_page', '3');
		return;
	}
}
add_action('pre_get_posts', 'change_set_post');

サブクエリを使って記事一覧を表示する

メインクエリで取得できないデータをデーターベースにリクエストした上で、投稿を1つ1つ取り出して処理するコードです。

<?php
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$args = [
	'post_type' => 'post',
	'posts_per_page' => 10,
	'paged' => $paged
];
$the_query = new WP_Query($args);
?>
<?php if ( $the_query->have_posts() ): ?>
	<ul>
		<?php while ( $the_query->have_posts() ): ?>
		<?php $the_query->the_post(); ?>
			<li>
				<a href="<?php the_permalink(); ?>">
					<div class="thumbnail">
						<?php if ( has_post_thumbnail() ) { ?>
							<?php the_post_thumbnail(); ?>
						<?php } else { ?>
							<img src="<?php echo esc_url( get_theme_file_uri('/img/noimage.jpg') ); ?>">
						<?php } ?>
					</div>
					<div class="date"><?php echo get_the_date(); ?></div>
					<?php $terms = get_the_terms($post->ID, 'category'); ?>
					<?php if ( is_array($terms) ) : ?>
						<div class="terms">
							<?php
							foreach ( $terms as $term ) {
								echo '<span>' . $term->name . '</span>';
							}
							?>
						</div>
					<?php endif; ?>
					<div class="title"><?php the_title(); ?></div>
					<div class="excerpt"><?php the_excerpt(); ?></div>
				</a>
			</li>
		<?php endwhile; ?>
	</ul>
	<?php

カスタム投稿タイプを登録する

カスタム投稿タイプを登録するコードです。デフォルトの投稿タイプ以外に投稿機能を増やしたい時に使います。
ここでは、worksという制作実績を管理するためのカスタム投稿タイプを登録する例です。


function cpt_register_works() {
	$args = [
		'label' => '制作実績',
		'labels' => [
			'singular_name' => '制作実績',
			'edit_item' => '制作実績を編集',
			'add_new_item' => '新規制作実績を追加'
		],
 	'public' => true,
 	'show_in_rest' => true,
 	'has_archive' => true,
 	'delete_with_user' => false,
 	'exclude_from_search' => false,
 	'hierarchical' => false,
 	'query_var' => true,
 	'menu_position' => 5,
 	'supports' => [
			'title',
			'editor',
			'thumbnail',
			'custom-fields',
			'revisions',
			'author',
			'excerpt'
		],
	];
	register_post_type('works', $args);
}
add_action('init', 'cpt_register_works');

カスタムタクソノミーを登録する

カスタムタクソノミーを登録するコードです。カスタム投稿タイプにタクソノミーを追加したり、デフォルトの投稿タイプに元からある「カテゴリー」「タグ」以外にタクソノミーを追加したい時に使います。
ここでは、worksという制作実績を管理するためのカスタム投稿タイプにタクソノミーを登録する例です。


function tax_register_workstype() {
	$args = [
 	'label' => 'タイプ',
 	'labels' => [
 		'singular_name' => 'タイプ',
 		'edit_item' => 'タイプを編集',
 		'add_new_item' => '新規タイプを追加'
 	],
 	'show_admin_column' => true,
 	'hierarchical' => true,
 	'query_var' => true,
 	'show_in_rest' => true
	];
	register_taxonomy('works-type', 'works', $args);
}
add_action('init', 'tax_register_workstype');

ターム一覧を表示するコード1

特定のタクソノミーに紐づくタームの一覧を表示するためのコードです。
サイドバーやフッター等で、特定のタクソノミーに紐づくタームの一覧を表示するためのコードです。


<?php
$args = [
  'taxonomy' => 'category'
];
$terms = get_terms($args);
?>
<?php if ( !empty($terms) ) : ?>
  <ul>
    <?php
    foreach ( $terms as $term ) :
      echo '<li><a href="' . get_term_link($term) . '">' . $term->name . '(' . $term->count . ')</a></li>';
    endforeach;
    ?>
  </ul>
<?php endif; ?>

<?php //現在表示中のターム名を表示(ターム別一覧ページに記述)
single_term_title('現在の選択:');
?>

ターム一覧を一覧ページで表示するためのコード

アーカイブページ(記事一覧ページ)で特定のタクソノミーに紐づくタームの一覧を表示するためのコードです。
ターム別の記事一覧ページで、現在選択されているタームを明示するための処理を含みます。


<?php
if ( is_home() ) {
	$current_slug = '';
} else {
	$current_slug = get_queried_object()->slug;
}
$all_class = is_home() ? '_current' : '';
?>
<ul>
	<li class="<?php echo $all_class; ?>"><a href="<?php echo esc_url( home_url('/news/') ); ?>">すべて</a></li>
	<?php
	$args = [
		'taxonomy' => 'category',
	];
	$terms = get_terms($args);
	foreach ( $terms as $term ) :
		$current = $current_slug === $term->slug ? '_current' : '';
		echo '<li class="' . $current . '">';
		echo '<a href="' . get_term_link($term) . '">' . $term->name . '</a>';
		echo '</li>';
	endforeach;
	?>
</ul>

CSSとJavaScriptをfunctions.phpで読み込む

CSSとJavaScriptをfunctions.phpで読み込むためのコードです。


function my_theme_scripts() {
	wp_enqueue_style('my-style', get_theme_file_uri('/css/style.css'), [], '1.0');
	wp_enqueue_script('my-script', get_theme_file_uri('/js/script.js'), ['jquery'], '1.0', true);
}
add_action('wp_enqueue_scripts', 'my_theme_scripts');

ここら辺のコードをよく使用したので備忘録としてまとめておきます。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?