<img src="dummy.jpg" data-src="/img/realimage.jpg" class="lazy">
<script>
document.addEventListener("DOMContentLoaded", function() {
var lazyImages = [].slice.call(document.querySelectorAll("img.lazy"));
if ("IntersectionObserver" in window) {
let lazyImageObserver = new IntersectionObserver(function(entries, observer) {
entries.forEach(function(entry) {
if (entry.isIntersecting) {
let lazyImage = entry.target;
lazyImage.src = lazyImage.dataset.src;
if (typeof lazyImage.dataset.srcset === "undefined") {
}else{
lazyImage.srcset = lazyImage.dataset.srcset;
}
lazyImage.classList.remove("lazy");
lazyImageObserver.unobserve(lazyImage);
}
});
});
lazyImages.forEach(function(lazyImage) {
lazyImageObserver.observe(lazyImage);
});
} else {
// Possibly fall back to a more compatible method here
}
});
</script>
投稿タイプのスラッグが「news」の場合
<?php // 年別アーカイブリストを表示
$year=NULL; // 年の初期化
$args = array( // クエリの作成
'post_type' => 'news', // 投稿タイプの指定
'orderby' => 'date', // 日付順で表示
'posts_per_page' => -1 // すべての投稿を表示
);
$the_query = new WP_Query($args); if($the_query->have_posts()){ // 投稿があれば表示
while ($the_query->have_posts()): $the_query->the_post(); // ループの開始
if ($year != get_the_date('Y')){ // 同じ年でなければ表示
$year = get_the_date('Y'); // 年の取得
echo '<a href="'.home_url( '/', 'https' ).'news/date/'.$year.'">'.$year.'年</a>'; // 年別アーカイブリストの表示
}
endwhile; // ループの終了
wp_reset_postdata(); // クエリのリセット
}
?>
投稿タイプのスラッグが「news」の場合
<?php
// 月別アーカイブリストを表示
$post_type = 'news'; // ポストタイプを指定
$args = array(
'post_type' => $post_type,
'posts_per_page' => -1
);
$archive_query = new WP_Query( $args );
while ( $archive_query->have_posts() ) {
$archive_query->the_post();
$archive_list[ get_the_time( 'Y/n', $post->ID ) ][] = $post->post_title;
}
wp_reset_postdata();
if($archive_list):
?>
<ul>
<?php
foreach( $archive_list as $year_month => $archive ) :
$year_month_arr = explode( '/', $year_month );
?>
<li>
<a href="<?php echo home_url( '/', 'https' ).$post_type.'/date/'.$year_month; ?>">
<?php echo $year_month_arr[0].'年'.$year_month_arr[1].'月' ?> [<?php echo count( $archive ) ?>]</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
投稿タイプ「post」では、アーカイブページは下記のような形に、
カスタム投稿タイプ「news」だと、
この時、年別アーカイブページのみ404 not foundと表示される
https://sample.com/2019?post_type=news
function customize_menus(){
global $menu;
$menu[19] = $menu[10]; //メディアの移動
unset($menu[10]);
}
add_action( 'admin_menu', 'customize_menus' );
<?php if ( function_exists( 'wp_pagenavi' ) ) wp_pagenavi(); ?>