投稿一覧ページで、Beansみたいに本文の内容をそのまんま表示させて続きがある場合のみ「続きを読む」ボタンを設置させたいなってあれこれ骨を折っていたら、解決方法がテンプレートタグ1個だったっていう話。
なんてこったい。
結論から書くね。アイキャッチ画像を表示させたあと、これ書いて終わり。
<?php the_content('<div class="btn btn-outline-primary
btn-sm mt-4">続きを読む</div>'); ?>
the_content() は、現在の投稿の本文を出力するテンプレートタグ。
本文中に「<!–more–> クイックタグ」がある場合のみ、その続きへのリンクを勝手に設置してくれる。なんと便利な。
Beansが使ってたの、これだわ。
the_content() は引数として「続きを読む」リンクのテキストを渡すことができるので、適当に続きを読むボタンをデコって設置して終わり。あ、余白の調節も忘れずに。
改めて、content.phpの全文を書くとこんな感じ。
<?php
/**
* Post rendering content according to caller of get_template_part
*
* @package UnderStrap
*/
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
?>
<article <?php post_class(); ?> id="post-<?php the_ID(); ?>">
<header class="entry-header">
<?php
the_title(
sprintf( '<h2 class="entry-title">
<a href="%s" rel="bookmark">',
esc_url( get_permalink() ) ),
'</a></h2>'
);
?>
<?php if ( 'post' === get_post_type() ) : ?>
<div class="entry-meta">
<?php understrap_posted_on(); ?>
</div><!-- .entry-meta -->
<?php endif; ?>
</header><!-- .entry-header -->
<?php echo get_the_post_thumbnail( $post->ID, 'large' ); ?>
<!-- insert eyecatch image -->
<div class="entry-content">
<?php the_content('<div class="btn btn-outline-primary
btn-sm mt-4">続きを読む</div>'); ?>
↑これな。
</div><!-- .entry-content -->
<footer class="entry-footer">
<?php understrap_entry_footer(); ?>
</footer><!-- .entry-footer -->
</article><!-- #post-## -->
今日の苦労は一体なんだったんだ。おしまい。