[wordpress] 最新の記事本文をHTMLコードのままTOPに表示させる【more付き】
moreが付いている最新記事本文をHTMLコードそのままでTOPページに表示させるには以下のように実装する
top.php
<?php
/*
Template Name: top
*/
$args = array( 'posts_per_page' => 1, 'category_name' => 'news', 'order'=> 'DESC' );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );
?>
<div class="entry-content">
<?php
global $more; // グローバル変数 $more を宣言(ループの前に)。
$more = 0; // more タグまでのみを表示するようにセット(ループの中で)。
the_content("さらに内容を見る");
?>
</div>
<?php endforeach; wp_reset_postdata();?>
<?php
$args = array( 'posts_per_page' => 1, 'category_name' => 'laptop', 'order'=> 'DESC' );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );
?>
<div class="entry-content">
<?php
global $more; // グローバル変数 $more を宣言(ループの前に)。
$more = 0; // more タグまでのみを表示するようにセット(ループの中で)。
the_content("さらに内容を見る");
?>
</div>
<?php endforeach; wp_reset_postdata();?>
今回はnewsとlaptopというカテゴリーの最新記事1つづつを表示させるようにした。
もし記事内に<!–more–>があれば、そこまでの分をHTMLそのままで表示される