0
0

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 で検索結果の body クラスからページ関連のクラスを取り除く

Posted at

検索結果の body には、何故か検索にひっかかったページに関連するクラスも出力されます。検索結果に他のページのスタイルがあたってしまったりして不便なので、フィルターを使って取り除きます。

functions.php
function remove_page_class_from_search($classes, $class) {
  if (is_search()) {
    foreach ($classes as $i => $class) {
      if (strpos($class, 'page-') === 0 || strpos($class, 'parent-page-') === 0) {
        unset($classes[$i]);
      }
    }
    $classes = array_values($classes);
  }
  return $classes;
}
add_filter('body_class', 'remove_page_class_from_search');
0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?