2
2

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で表示をちょっとだけ早くする

Last updated at Posted at 2014-02-10

WordPressのテーマファイル内で同じ関数を何回も使用している場合は、
テーマファイルの先頭で値を変数に入れてしまうことで表示をやや高速化することができます。

たとえば、home_urlやbloginfo('template_url')などが該当するかもしれません。

以下では$infoという連想配列を作成して使用した場合です。
連想配列のキーを元の関数に近づけておくことで、読み替えの違和感も少なくなると思います。

index.php
<?php
$info = array(
    'home_url' => home_url('/'),
    'template_directory_uri' => get_template_directory_uri(),
    'template_url' => get_bloginfo('template_url'),
    'stylesheet_url' => get_bloginfo('stylesheet_url'),
);
?>
<!doctype html>
<html lang="ja">
(省略)
<a href="<?php echo $info['home_url']; ?>">ホームへ</a>

キャッシュプラグイン等に比べると効果は少ないですが、
同じ関数の呼び出しが多いなぁ、と感じているなら試してみると良いと思います。

2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?