5
5

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の勉強5 カスタム投稿(自分用)

Last updated at Posted at 2014-05-29

カスタム投稿をプラグインなしで作る

参考

・『基礎からのWordPress』高橋のり
http://webdesignrecipes.com/wordpress-conditional-tags-and-custom-post-type/

表示するページ

・single-カスタム投稿名.php(個別表示)
・taxonomy.php(一覧表示)
→・functions.php, header.php, sidebar.php

wp.logの設定

・変数についてよくわからなくなってしまったのでfunctions.phpの一番上に

functions.php
<?php
function p($arg) {
  error_log($arg . "\n" , 3, 'WordPressのディレクトリ/log/wp.log');
}
function d($arg) {
    ob_start();
      var_dump($arg);
      $log = ob_get_contents();
        ob_end_clean();
        return rtrim($log);
}
function pd($arg) {
    p(d($arg));
}

・例
<?php pd($type1); ?>

オリジナル関数を使わずに2つのタクソノミーそれぞれ用の画像をサイドバーのメニューに表示する方法(sidebar.phpのtaxonomy.php用タグ)

・画像名をスラッグ名と同じにする

・URLに$term(=スラッグ名)
http://www.nandani.sakura.ne.jp/web_all/cms/3638/

もしくは
・single_tag_title→$term_name, get_term_byを使う
http://elearn.jp/wpman/function/get_term_by.html

sidebar.php
  /**
   * 部屋のタイプ、家具の種類(taxonomy.php)
   */
  elseif(is_tax()):

   $term_name = single_tag_title("", false);

    $type1 = get_term_by('name', $term_name, 'タクソノミー1の名前');
    //$type1 = get_term_by('slug', 'タームのスラッグ', 'タクソノミー1の名前');
    if($type1)$org_term = $type1;
    $type2 = get_term_by('name', $term_name, 'タクソノミー2の名前');
    if($type2) $org_term = $type2;

    /* 【重要】画像の名前をスラッグと同じにする! */ ?>
    <img src="<?php echo get_template_directory_uri(); ?>/images/<?php echo $org_term->slug; ?>.jpg" alt="">
5
5
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
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?