0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

wordpress 抜粋表示した時 more(続きを読む)が表示されないので自己解決した話

Posted at

症状

最新記事をカテゴリ毎にトップページに並べているサイトで記事単体があまりにも1画面表示を覆ってしまうので、read more(続きを読む)タグ(Wordpress標準)を使って解決仕様と試みた所全く表示されないので、焦った結果自己解決したという内容です。

解決方法

普通にループ内でmoreを出力してあげればよかった

xxx.php
//ループ前にかならずこいつを宣言する
global $more;
$more = false;

$list = get_posts(array(
'category_name' => 'categoryname'
'posts_per_page' => 1 //最新記事のみがほしいので1、最新記事から2つほしければ2
))
foreach ($list as $post){
setup_postdata($post);
echo the_content("続きを読む"); //他の表記が良ければココに記載
}
wp_reset_postdata();

//TPOに応じて
$more = true;

今まで(未解決のとき)

表記と改行が少ないのと、ループ文の記載が無いので好きだったが、moreが表示されないので・・・

xxx.php
$list = get_posts('category_name=categoryname&posts_per_page=1');
$content = apply_filters('the_content',$list[0]->post_content);
echo $content;

wordpress codexにも普通に記載があった。。。

しっかり読まないとダメですね。
リンク

##説明
現在の投稿の本文を出力します。 このテンプレートタグはループの中で使わなければなりません。

本文中に クイックタグがある場合、先頭からそのクイックタグまでの内容のみを抜粋として表示します。 ただし単一投稿ページ(パーマリンクで投稿を特定したページ)では、抜粋のみでなく本文をすべて表示します。 the_content() テンプレートタグは の表示方法を決めるパラメータを受け取り、投稿の全文を「続けて読む」ためのリンクを表示する、という設計になっています。

## について:

クイックタグの "more" の前に空白を入れてはいけません。つまり は正常に動作しません! クイックタグは single.php などの単一投稿を表示するテンプレートでは動作せず無視されます。

詳しくは 「続きを読む」のカスタマイズ をお読みください。

以上です。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?