messedupcodes
messedupcodes
messed up codes
20 posts
codes, codes, codes ! why are they messed up everytime??
Don't wanna be here? Send us removal request.
messedupcodes · 8 years ago
Text
wp pagenavi で2ページ目が404になる
条件
1.カスタム投稿タイプを使っている 2.カスタム投稿タイプではポストと同じカテゴリーが使える 3.カテゴリーはネストされていて、孫カテゴリーまで存在する 4.category.phpで記事一覧を出す。 5.category.phpにはページネーションをつける
最初にやっていたこと
孫カテゴリーのアーカイブにいるときに、親カテゴリーか、そのさらに上の階層のカテゴリーを見てポストタイプを変えたかったので、WP_Queryを使って一覧を吐き出していた。
すると、ページネーションの2ページ目で404が発生。
paged => $paged がないからかと思ったけども、違う。
結論から言うと、アーカイブのテンプレートでWP_Queryで一覧を出してしまうと、ページネーションがきかないらしい。
ということでメインループで一覧を吐き出すべくpre_get_postの設定をする。
その前に、そもそもメインループ回したら、どんな一覧が表示できるのよ?と思って、if(have_posts())...を書いたら、何も表示されない。 なんで表示されないのかと悩むこと小一時間。 試しにポストの記事にカテゴリーを付けてリリースしたら、そちらは一覧に表示される。 つまり、普通の投稿タイプはちゃんと表示されて、カスタム投稿タイプにあてているカテゴリーは一覧に表示されないことがわかった。 pre_get_postでpost_typeをちゃんと指定しないといけない。
でもそれやると、今度は普通の投稿タイプの時にカテゴリー一覧が表示されない。
ということで行き着いたコードがこちら
function custom_query($query){  if(is_admin() || ! $query->is_main_query())    return;  if( $query->is_category()){    $cat = get_queried_object();    if($cat->parent != 0){      $cat = get_category($cat->parent);      $type = $cat->slug;      if($type == 'hoge'){        $post_type = 'hoge';      }else{        $post_type = 'hage';      }      $query->set('post_type', $post_type);      $query->set('posts_per_page', '20');      $query->set('meta_key', 'yomi');      $query->set('meta_value', '');      $query->set('orderby', 'meta_value');      $query->set('order', 'asc');      return;    }else{      $query->set('posts_per_page', '20');      $query->set('meta_key', 'yomi');      $query->set('meta_value', '');      $query->set('orderby', 'meta_value');      $query->set('order', 'asc');      return;    }  } } add_action('pre_get_posts', 'custom_query');
カスタム投稿タイプだろうが、普通の投稿タイプだろうが カテゴリーアーカイブでは、カテゴリに属するものがメインループで表示されるだろうと思い込んでいたんだよな…
0 notes
messedupcodes · 8 years ago
Text
Wordpress 入れ子構造のカテゴリー出力。
   <?php      $args = array(        'parent' => 2,        //'parent_of' => 4,        'order_by' => 'id',        'hide_empty' => 0      );      $categories = get_categories($args);      //取得したカテゴリへの各種処理      echo '<ul class="acordion">';      foreach($categories as $val){        //カテゴリのリンクURLを取得        $cat_link = get_category_link($val->cat_ID);        //カスタムフィールドでアイコン取得する用のIDを取得        $post_id = 'category_' . $val -> cat_ID;        //アイコンのソース取得        $attachment_id=get_field('category_icon_small',$post_id);        $icon_src = wp_get_attachment_image($attachment_id,'full');        //親カテゴリのリスト出力        echo '<li class="parent">';        //子カテゴリのIDを配列で取得。配列の長さを変数に格納        $child_cat_num = count(get_term_children($val->cat_ID,'category'));        //子カテゴリがあった場合の処理        if($child_cat_num > 0){          echo '<span class="trigger none-submenu">' . $val -> name . '</span>';        }else{          echo '<a href="' . $cat_link . '" class="parent button">' . $val -> name . '</a>';        }
       //print_r($child_cat_num);
       //子カテゴリが存在する場合        if($child_cat_num > 0){          echo '<ul class="child-gp submenu">';          //子カテゴリの一覧取得条件          $category_children_args = array('child_of'=>$val->cat_ID, 'hide_empty' => 0);          //print_r($category_children_args);          //子カテゴリの一覧取得          $category_children = get_categories($category_children_args);          //print_r($category_children);          //子カテゴリの数だけリスト出力          foreach($category_children as $child_val){            $cat_link = get_category_link($child_val -> cat_ID);            echo '<li class="child">';            //孫カテゴリのIDを配列で取得            $grandchild_cat_num = count(get_term_children($child_val->cat_ID,'category'));
           if($grandchild_cat_num > 0){              echo '<span class="trigger none-submenu">' . $child_val -> name . '</span>';            }else{              echo '<a href="' . $cat_link . '" class="child button"><p>' . $child_val -> name . '</p></a>';            }
           //孫カテゴリが存在する場合            if($grandchild_cat_num > 0){              echo '<ul class="child-gp submenu">';              //孫カテゴリの一覧取得条件              $cat_grandchild_args = array('child_of'=>$child_val->cat_ID, 'hide_empty' => 0);              //孫カテゴリの一覧取得              $cat_grandchild = get_categories($cat_grandchild_args);              //孫カテゴリリスト出力              foreach($cat_grandchild as $grandchild_val){                $cat_link = get_category_link($grandchild_val -> cat_ID);                echo '<li class="child"><a href="' . $cat_link . '" class="child button"><p>' . $grandchild_val->name . '</p></a></li>';              }              echo '</ul>';            }
           echo '</li>';          }          echo '</ul>';        }        echo '</li>';      }      echo "</ul>"    ?>
0 notes
messedupcodes · 9 years ago
Text
VCCW v.3.x
久しぶりにVCCWをダウンロードしたら、バージョンが3になっていた。 今までCentOSだったのがUbuntuになったりして、実行速度があがったりしたらしい。 そして、v.3以降から、xenial64なるboxを追加することになったらしい。 なにはともあれVCCWをダウンロードしてvagrant up そうすると、xenial64のboxがないぜって怒られる。 vagrant box add vccw-team/xenial64 してみると、そんなファイルはない!って怒られる。 どうしたらいいかわからないまま、もう夜中の1時だよ 続きは明日にするか…
0 notes
messedupcodes · 9 years ago
Text
【Vagrant】Vagrant upはちゃんと走ったのに、今度はブラウザでERR_CONNECTION_TIMED_OUTになっちゃう
vagrant に hostsupdater入れるの忘れてた… $ vagrant plugin install vagrant-hostsupdater 入れて、もう一度Vagrant upしたら無事にサイトも復活。 良かった
0 notes
messedupcodes · 9 years ago
Text
【Vagrant】今度は The name of your virtual machine couldn't be set because VirtualBox is reporting another VM with that name already exists.
そして VBoxManage: error: Could not rename the directory というエラーがでる。 VirtualBox経由で、つくられた仮想マシンを削除した際に、/user/username/VirtualBox VMs/ にフォルダが残ってしまっていたのが問題だったらしい。 https://gyagya1111.blogspot.jp/2015/04/vagrant-up.html
0 notes
messedupcodes · 9 years ago
Text
【Vagrant】Authentication failure. 【その2】
色々やってみたけども、結果から先にいうと、Vagrant 1.8.5にバグがあったらしい http://stknohg.hatenablog.jp/entry/2016/07/26/190330 次のアップデートでは解消されるけども、とりあえずローカルのrubyファイルの修正で対応できる模様。
0 notes
messedupcodes · 9 years ago
Text
【Vagrant】Authentication failure.【その1】
Macのクリーンインストールをしたあと、外付けHDDに保存していたVagrantのディレクトリから、Vagrant upしたところ、Authentication failure が出たという話。 色々調べたところ、 http://qiita.com/shyse/items/9ec50b868b90f847c75f この記事を発見。 つまり、Vagrant 1.7以降は、公開鍵を自分で作ってMacに保存していた場合、その公開鍵とVagrant内の公開鍵(自分で公開鍵を作っていない場合に使われる)が差し替えられるらしい。 Macをクリーンインストールし直したから、システム内に公開鍵がなくなって、Vagrant が Authentication failure を吐いたということだろう。 次はシステム内に公開鍵を作成して、どうなるか見てみよう
0 notes
messedupcodes · 10 years ago
Text
タクソノミーに設定したカスタムフィールドを呼び出したかった
結果的に書いたコード <?php   $terms = get_the_terms($post->ID, ‘your_taxonomy_name’);   if($terms && !is_wp_error($terms)):     foreach($terms as $term){       $term_id = $term->term_id;     }     $term_idsp = ‘paint-terms_’ .$term_id;     $color = get_field(’color’, $term_idsp); ?> <p style=“background-color: <?php echo $color; ?>”>hoge</p> <?php endif; ?>
こんな感じ get_queried_object()->term_id でterm idをとってくるって書いてあるのもたくさん見つけたんだけども 今のwordpressではget_queried_object()にterm_idの返り値はないらしい。 仕方ないから長いコードになってしまった。 もっと短くは書けないものか…
0 notes
messedupcodes · 10 years ago
Text
[CSS3] keyframesをsassのmixinにしたかったの
@mixin keyframes($animation-name){ @-webkit-keyframes #{$animation-name}{ @content; } @-moz-keyframes #{$animation-name}{ @content; } @keyframes #{$animation-name}{ @content; } }
@mixin animation($animation-name){ -webkit-animation: $animation-name; -moz-animation: $animation-name; animation: $animation-name; -webkit-animation-fill-mode: both; -moz-animation-fill-mode: both; animation-fill-mode: both; }
@include keyframes(animationSample){ 0%{ opacity: 0; } 100%{ opacity: 1; } }
.box{ height: 100px; width: 100px; background-color: red; @include animation(animationSample 1.2s ease .15s) }
ポイントは
#{$animation-name}
これ。 こうしなかったせいで、$animation-nameがうまく差し替わらなかったのだ。
0 notes
messedupcodes · 10 years ago
Text
【jQuery】outerWidth
$('hoge').outerWidth({margin:true}); でhogeの幅が取得できないので、何で?と思って調べたら $('hoge').outerWidth(true); こう書かないといけなかったのね…
0 notes
messedupcodes · 10 years ago
Text
【配列】foreachでループさせて抜き出した値を配列に入れなおそうとしてるんだけど…【わからんの】
経緯: Smart Custom FieldsというWordpressのプラグインを使って、辞書ページをつくろうと思ったの Repeaterを使って、予め設定したフィールドセットをループで抜き出して、サブフィールドの値を使ってソート というようなことがしたい。 $hoge = SCF::get('hoge'); でフィールドセットの取得。それを foreach($hoge as $field_name => $field_value): で繰り返して抜き出していく。 抜き出されるのは、$hoge内にあるサブフィールド hoge_a, hoge_b, hoge_c の3つ 想像では $hoge   hoge_a   hoge_b   hoge_c
$hoge   hoge_a   hoge_b   hoge_b
てな感じで抜き出されているはず。 で、このhoge_bの値を使って何かをしたい。 hoge_bには複数の値を含んでいて 値は重複もしているので 重複しているものを1つにして 残ったユニークな値を抜き出したいわけですわ。
だから $hoge_b = $field_value['hoge_b']; でhoge_bの値を抜き出して それを配列に入れなおして それをarray_uniqueで重複削除して
てなことを考えていたんだけど
うまくいかない。
配列の状況をみると 配列に入れなおして綺麗に番号が振られたはずの$hoge_bの値が 全部0番目に入っている
ということは、値はそれぞれ別の配列に組み込まれたってことだよな?
だからarray_mergeで1つにまとめようと思ったんだけど…
うまくいかんのよー うまくいかんのよー こまったなー
0 notes
messedupcodes · 11 years ago
Text
【メモ】都道府県をHTMLに全部入れるの面倒くさいじゃん?
emmet用 list ul>li>a[href="#"]{北海道}^li>a[href="#"]{青森県}^li>a[href="#"]{岩手県}^li>a[href="#"]{宮城県}^li>a[href="#"]{秋田県}^li>a[href="#"]{山形県}^li>a[href="#"]{福島県}^li>a[href="#"]{茨城県}^li>a[href="#"]{栃木県}^li>a[href="#"]{群馬県}^li>a[href="#"]{埼玉県}^li>a[href="#"]{千葉県}^li>a[href="#"]{東京都}^li>a[href="#"]{神奈川県}^li>a[href="#"]{新潟県}^li>a[href="#"]{富山県}^li>a[href="#"]{石川県}^li>a[href="#"]{福井県}^li>a[href="#"]{山梨県}^li>a[href="#"]{長野県}^li>a[href="#"]{岐阜県}^li>a[href="#"]{静岡県}^li>a[href="#"]{愛知県}^li>a[href="#"]{三重県}^li>a[href="#"]{滋賀県}^li>a[href="#"]{京都府}^li>a[href="#"]{大阪府}^li>a[href="#"]{兵庫県}^li>a[href="#"]{奈良県}^li>a[href="#"]{和歌山県}^li>a[href="#"]{鳥取県}^li>a[href="#"]{島根県}^li>a[href="#"]{岡山県}^li>a[href="#"]{広島県}^li>a[href="#"]{山口県}^li>a[href="#"]{徳島県}^li>a[href="#"]{香川県}^li>a[href="#"]{愛媛県}^li>a[href="#"]{高知県}^li>a[href="#"]{福岡県}^li>a[href="#"]{佐賀県}^li>a[href="#"]{長崎県}^li>a[href="#"]{熊本県}^li>a[href="#"]{大分県}^li>a[href="#"]{宮崎県}^li>a[href="#"]{鹿児島県}^li>a[href="#"]{沖縄県}
0 notes
messedupcodes · 11 years ago
Text
html5 の videoタグをjavascriptで制御したい!
けど、どハマり中。 何にはまっているかというと 再生している動画が、任意の秒数にきたところでdisplay:none;していた要素を表示させて、でも表示したあとはユーザが自由に消したり表示させたりできるようにしたいわけです。 で、かんたんに var v = getElementById('video'); v.addEventListener("timeupdate",function(){ var timer = Math.floor(v.currentTime); if(timer >= 3){   $('.hoge').show(); }; }); $('.hoge_btn').click(function(){   $('.hoge').hide(); }); みたいに書いたわけです。 でもこれだと、.hoge_btnをクリックして.hogeを消しても、timerが3秒と等しいか大きかったら.hogeを表示 って命令が効いちゃってて、すぐにまた.hogeが表示されちゃう。ずっと消えてて欲しいのに。 だから、addEventListnerをremoveEventListnerで打ち消せないかといろいろやってみてるけど、うまくいかない←いまここ なんか悔しい 【続報】 考えてみたらremoveEventListenerだけじゃなくて、jQueryの方もリセットしないといけなかったんだな ということで、こんな風にコードを書き換えたらうまくいきました。
var v = getElementById('video'); v.addEventListener("timeupdate", showHoge); private function showHoge(){ var timer = Math.floor(v.currentTime); if(timer >= 3){   $('.hoge').show(); }; if(timer > 4){   v.removeEventListener("timeupdate",showHoge);   $('.hoge').reset(); }; $('.hoge_btn').click(function(){   $('.hoge').hide(); });
これが果たしてスマートな方法なのかは別として とりあえずこれで動いたからよしとする。 もっと美しい書き方はないものか…
0 notes
messedupcodes · 11 years ago
Text
macで隠しファイルや隠しフォルダを表示する
ターミナルを起動して defaults write com.apple.finder AppleShowAllFiles TRUE その後 killall Finder でFinderを再起動 隠しファイルを非表示にする場合はTRUEをFALSEに defaults write com.apple.finder AppleShowAllFiles FALSE
0 notes
messedupcodes · 11 years ago
Text
jQuery Mobile のデフォルトスタイルを解除する
・HTML <input type="text"> ・javascript $(document).bind('mobileinit',function(){   $.mobile.keepNative = 'input[type="text"]'; } javascriptはjquery.jsとjquery.mobile.jsの間に入れないといけない <script src="jquery.js"></script> <script>    ... <script>
<script src="jquery.mobile.js"></script> ↑こんな感じ
0 notes
messedupcodes · 11 years ago
Text
【解決】Varying-Vagrant Vagrantsが突然 502 Bad Gatewauy を吐く
仕事で必要なデータをMAMPに移動させたところ、なぜか直ってしまった。調べてみたら、どうやらPHPのパースエラーが原因だったらしい。 確かにMAMPに移したデータはPHPのエラーが出ていた。 まる1日ちかく頭を悩ませて、原因がそれかいな...orz
0 notes
messedupcodes · 11 years ago
Text
Varying-Vagrant-Vagrants が突然 502 Bad Gateway を吐く
トップページは問題なく表示されるのに、階層を下るとBad Gatewayとなる
vvv  のwww.config の44行目と45行目をコメントアウトして
ターミナルから
% sudo service php5-fpm restart
sudo: service: command not found
となってしまったので、PATHが通ってないのかと思ったら
そういうわけではないらしい←いまここ
ところでbash_profileというファイルが存在しないらしい
コマンドラインにbashを使わずにzshを使ってるからかな?
【参考】
http://laravel.io/forum/05-14-2014-502-bad-gateway-in-vagrant-permission-denied
http://5log.jp/blog/path/
http://qiita.com/harapeko_wktk/items/285d5c2a409346e3d55e
0 notes