WordPressの続きを読むクリック後に記事を文頭から表示させるphp追記

広告
WordPressの他のページに表示される記事が全文表示では長すぎるときは、moreタグを適当に入れて文頭から任意の個所までを表示さますが、画像や空間が多い本文では、続きからだとかえって読みにくいことがあります。

moreタグでは記事の続きから表示される

moreタグを使った場合、「続きを読む」をクリックすると、記事(投稿)の続きの部分から表示される。
文頭に大事な記載がある場合や、途中からでは文脈が掴みにくい場合など、記事(投稿)の最初から表示して欲しいことがある。
そんなときにはプラグインで対処できる。
以前は「MoreLink remove」を使っていたが、現在は無くなっているようので検索すると、以下の2点が目立つ。
◆More To Top
https://wordpress.org/plugins/more-to-the-top/
This plugin hasn’t been updated in over 2 years. It may no longer be maintained or supported and may have compatibility issues when used with more recent versions of WordPress. 二年以上更新されていない‥‥。
◆Custom More Link Complete
https://wordpress.org/plugins/custom-more-link-complete/
This plugin hasn’t been updated in over 2 years. It may no longer be maintained or supported and may have compatibility issues when used with more recent versions of WordPress. こちらも二年以上更新されていない‥‥。

fanctions.phpに追記

WordPressのフォーラムで検索すると以下があった。
https://ja.forums.wordpress.org/topic/12687

fanctions.phpに
/* more-linkのハッシュ消し */
function remove_more_jump_link($link) {
  $offset = strpos($link, '#more-');
  if ($offset) {
    $end = strpos($link, '"',$offset);
  }
  if ($end) {
    $link = substr_replace($link, '', $offset, $end-$offset);
  }
  return $link;
}
add_filter('the_content_more_link', 'remove_more_jump_link');

と追加する。
なお、「/* more-linkのハッシュ消し */」部分は自分で分るように「/* 続きを読むで文頭から表示 */」などのように書き換えておくとよい。
fanctions.phpはテーマアップデートしたときに新しいファイルで上書きされる可能性もあるので、子テーマを作って使う方が良いかも知れない。
ただし、fanctions.phpを子テーマで使うとエラーが出ることもあるので、その時はプラグインでの対応となります。
広告


広告