2016/11/08
固定ページ Wordpress
備忘録です。
参考サイト
[WordPress] どうしても固定ページテンプレートでページ送り付きのアーカイブが作りたいのよと言う場合
【参考サイトのタグ】page-***.phpに追加記述
<?php
/**
* Template Name: Archive Page */
?>
<?php
$paged = (int) get_query_var('paged');
$args = array(
'posts_per_page' => 3,
'paged' => $paged,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish'
);
$the_query = new WP_Query($args);
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
get_template_part( 'content', get_post_format() );
endwhile;
else:
get_template_part( 'content', 'none' );
endif;
if ($the_query->max_num_pages > 1) {
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => 'page/%#%/',
'current' => max(1, $paged),
'total' => $the_query->max_num_pages
));
}
wp_reset_postdata();
?>
カスタム投稿だったので、上記をこのようにしました。
【例】カスタム投稿testのsportsタームを表示
<?php
/**
* Template Name: Archive Page */
?>
/*ここにヘッダー、パンくずなどを追加*/
<?php
$paged = (int) get_query_var('paged');
$args = array(
'posts_per_page' => 3,
'paged' => $paged,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'test', //カスタム投稿のtest
'tax_query' => array(
array(
'taxonomy' => 'test_cate',//カスタム投稿のタクソノミー
'field' => 'slug',
'terms' => 'sports',//カスタム投稿のターム
),
),
'post_status' => 'publish'
);
$the_query = new WP_Query($args);
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
/*ここに表示したい内容を記述*/
<p><?php the_title(); ?></p>
<p><?php the_content(); ?>
<?php endwhile; ?>
<?php endif; ?>
<div class"testpage">
<?php if ($the_query->max_num_pages > 1) {
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => 'page/%#%/',
'current' => max(1, $paged),
'total' => $the_query->max_num_pages
));
}
wp_reset_postdata();
?>
</div>
/*ここにフッターなどを追記*/
2026/11/06
AI
2026/11/05
Wordpress
ホームページ制作
2026/11/04
Mac
2026/11/03
AI
2026/11/02
JQuery
css