やりたかったこと
- トップページに "お知らせ" のリストを表示させたい
- "お知らせ" はカスタム投稿タイプ (スラッグは "news")
- 「先頭に固定表示」の記事はリストの頭に表示
- 「先頭に固定表示」の記事は、より目立つようスタイリング
- お知らせする記事が0件ならエリア自体を出力させない
コード
<?php
$list_cnt = -1; //表示させたい件数。全表示は -1
$stickyArticleArray = get_option( 'sticky_posts' );
// if ( ! empty( $stickyArticleArray ) ) { $list_cnt -= count( $stickyArticleArray ) }; // 件数指定の場合、先頭固定記事の件数を引く
$the_query = new WP_Query( array(
'post_type' => array( 'news' ),
'posts_per_page' => $list_cnt,
'post_status' => 'publish'
) );
?>
<?php if ( $the_query->have_posts() ) : ?>
<aside class="frontpage-news media__body">
<h2 class="media__title">お知らせ</h2>
<ul class="no-listmarker">
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li class="list-item<?php if( is_sticky() ) { ?> sticked-item<?php } ?>">
<time class="frontpage-news__date"><?php the_time('Y年m月d日');?></time>
<?php if( empty( $post->post_content ) ) : ?>
<?php the_title(); ?>
<?php else : ?>
<a href="<?php the_permalink();?>"><?php the_title(); ?></a>
<?php endif; ?>
</li>
<?php endwhile; ?>
</ul>
<p class="alignright mt-0"><small><a href="<?php echo home_url(); ?>/archives/news">» 過去のお知らせ</a></small></p>
</aside>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
メモ
「先頭に固定表示」にチェックを付けた記事、付けていない記事を、それぞれ get_posts() で出力しようと試みたがややこしくなったので WP_Query() での実装とした。
<?php // ※ 仕上げてないので正常に機能しない
// "先頭に固定表示" にチェックが入っている記事 (https://yogawa.com/post-3711)
$stickyArticleArray = get_option( 'sticky_posts' );
if ( isset( $stickyArticleArray ) ) {
$stickyArticleArrayStr = implode( ',', $stickyArticleArray );
// "先頭に固定表示" にチェックが入っている記事
$sticked_news_posts = get_posts( array(
'post_type' => 'news',
'include' => $stickyArticleArrayStr,
// 'post__in' => get_option( 'sticky_posts' ), // get_posts では効かない
'posts_per_page' => -1
));
} else {
$sticked_news_posts = NULL;
}
// "先頭に固定表示" にチェックが入っていない記事
$no_sticked_news_posts = get_posts( array(
'post_type' => 'news',
'exclude' =>$stickyArticleArrayStr,
// 'post__not_in' => get_option( 'sticky_posts' ), // get_posts では効かない
'posts_per_page' => -1
));
?>
<?php if( ! empty( $sticked_news_posts ) || ! empty( $no_sticked_news_posts ) ) : ?>
<aside class="frontpage-news media__body">
<h2 class="media__title">お知らせ</h2>
<ul class="no-listmarker">
<?php // "先頭に固定表示" にチェックが入っている記事
if ( isset( $stickyArticleArray ) ) {
foreach ( $sticked_news_posts as $post ) : setup_postdata( $post ); ?>
<li class="list-item sticked-item">
<time class="frontpage-news__date"><?php the_time('Y年m月d日');?></time>
<?php if( empty( $post->post_content ) ) : ?>
<?php the_title(); ?>
<?php else : ?>
<a href="<?php the_permalink();?>"><?php the_title(); ?></a>
<?php endif; ?>
</li>
<?php
endforeach;
}
// "先頭に固定表示" にチェックが入っていない記事
foreach ( $no_sticked_news_posts as $post ) : setup_postdata( $post ); ?>
<li class="list-item">
<time class="frontpage-news__date"><?php the_time('Y年m月d日');?></time>
<?php if( empty( $post->post_content ) ) : ?>
<?php the_title(); ?>
<?php else : ?>
<a href="<?php the_permalink();?>"><?php the_title(); ?></a>
<?php endif; ?>
</li>
<?php
endforeach;
wp_reset_postdata();
?>
</ul>
<p class="alignright mt-0"><small><a href="<?php echo home_url(); ?>/archives/news">» 過去のお知らせ</a></small></p>
</aside>
<?php endif ?>