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 1 year has passed since last update.

WordPressでカテゴリごとに処理を分けるPHPコード

Last updated at Posted at 2021-04-23

WordPressでカテゴリごとにウィジェットとかの表示を切り替えたい時のコード。

まず外観のテーマエディターからheader.phpを開く。
他のファイルでも構いませんが、一番わかり易いのでheader.phpにしています。

bodyタグ付近に以下のコードを挿入。

	$cat = get_the_category();
	$cat = $cat[0];
	$cat_id = $cat->cat_ID;
	if($cat_id == 特定のカテゴリID){
        //処理
    }
    else{
	    //処理
    }

PHPを書きたくない、本番環境をいじりたくない、といった方は「Widget Options」というプラグインを用いるのもおすすめです。

こちらのプラグインではカテゴリ以外にもデバイスやログイン状態、タグ、記事タイプなど様々な指標で表示させるウィジェットや処理を切り替えることができます。

PHPのお役立ち情報

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?