###前提・実現したいこと
contactform7で作成したお問い合わせで、
製品を選択する項目があり、チェックボックスやセレクトどちらでもいいのですが、
その製品一覧をお客側でも簡単に増減させたいのですがいい案がありますでしょうか。
例えば、カスタム投稿の記事タイトルを取得しそれを一覧表示させるや、
増減可能のカスタムフィールドに値を入力してもらい、その値を一覧表示させるなど、
###試したこと
カスタム投稿に追加してもらった記事のタイトルをfunctions.phpで一覧取得し、そのショートコードを使用して一覧表示しようと思ったのですが、functions.php内で「echo」を使用するとformの外に一覧表示されてしまい、「return」を使用すると1件しか取得できない問題がおきてしまいます。
下記コードはカテゴリーごとに一覧表示させています。
function post_list(){ $taxonomyName = "product_category"; //親タームだとprentが0なのでこれを指定 $args = array( 'parent' => 0 ); //親を取得 $terms = get_terms($taxonomyName,$args); foreach ($terms as $term) { //親のIDを取得できた $parentId = $term->term_id; /*ここから親タームのIDがそれぞれ取得できるので、そこから子タームのリストを作る*/ //さっきはparentが0だったが、こんかいはprentが親IDの場合の条件 $childargs = array( 'parent' => $parentId, 'hide_empty' => true//投稿がない場合も隠さずにだす ); $childterms = get_terms($taxonomyName,$childargs); echo '<div>'; foreach ($childterms as $childterm) { $targetSlug = $childterm->slug; echo '<h2 id="'.$childterm->slug.'"><a href="'.get_term_link($childterm->slug, 'product_category').'">'.$childterm->name.'</a></h2>';//子タームの名前 //子ターム情報が取得できたので、ここからget_posts用に準備 $postargs = array( 'post_type' => 'product',//さっきのbrandsタクソノミーを使ってる“投稿”のポストタイプ 'tax_query' => array( array( 'taxonomy' => $taxonomyName, 'field' => 'slug',//フィールドをslugにしておくterm_idとかでも良いはず 'terms' => $targetSlug//上で準備してある$childterm->slug ) ) ); //記事取得 $postslist = get_posts( $postargs ); foreach ( $postslist as $post ) : setup_postdata( $post ); echo get_the_title(); //タイトル endforeach; wp_reset_postdata(); } echo '</div>'; } } wpcf7_add_form_tag("list", "post_list");
回答2件
あなたの回答
tips
プレビュー