出力結果の違う2つのカスタムフィールドを、同じように扱いたいと思っています。
(違いは下記コードでvar_dump()
して書いた通りです。)
具体的には、同じ値を持つ記事を関連記事として表示したいのですけれど、
なぜか次のコードで、tag_single
はきちんと関連記事として表示されるのに対し、tag_custom
は表示できません。
▼tag_single
の場合
php
1<?php 2$tags = SCF::get('tag_single'); 3var_dump($tags); 4//array(2) { [0]=> string(6) "値A" [1]=> string(6) "値B" } 5?> 6 7<?php 8$myposts = get_posts( array( 9 'post_type' => 'post', 10 'posts_per_page' => '4', 11 'post__not_in' => array( $post->ID ), 12 'meta_query' => array( 13 array( 14 'key' => 'tag_single', 15 'value' => $tags, 16 'compare' => 'IN' 17 ) 18 ), 19 'orderby' => 'rand' 20) ); ?> 21 22<?php if( $myposts ): ?> 23… 24<?php endif; ?>
▼tag_custom
の場合
php
1<?php 2$tag_custom = get_post_meta(get_the_ID(),'tag_custom',false); 3var_dump($tag_custom); 4//array(1) { [0]=> string(14) "値A| 値B" } 5 6 foreach($tag_custom as $tag_custom1) { 7 $tag_custom2 = explode( '|', $tag_custom1 ); 8} 9?> 10 11<?php 12$myposts = get_posts( array( 13 'post_type' => 'custom', 14 'posts_per_page' => '4', 15 'post__not_in' => array( $post->ID ), 16 'meta_query' => array( 17 array( 18 'key' => 'tag_custom', 19 'value' => $tag_custom2, 20 'compare' => 'IN' 21 ) 22 ), 23 'orderby' => 'rand' 24) ); ?> 25 26<?php if( $myposts ): ?> 27… 28<?php endif; ?>
ご覧のようにtag_custom
の場合は配列は[0]だけでその中に値Aと値Bがまとまって出力されてしまうので、一度explode
して再配列化しました。こうすればtag_single
と同じように扱うことができると思っているのですが、、できず。(>_<)
どなたか直し方を教えて頂ければと思います。
よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/05/15 18:30
2018/05/15 19:04 編集
退会済みユーザー
2018/05/15 19:25 編集
退会済みユーザー
2018/05/15 19:27
退会済みユーザー
2018/05/15 19:32
2018/05/15 19:34
退会済みユーザー
2018/05/15 19:37
2018/05/15 19:41
退会済みユーザー
2018/05/15 19:55
退会済みユーザー
2018/05/15 19:57
2018/05/15 20:06
退会済みユーザー
2018/05/15 20:19