質問編集履歴
1
コード追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -7,4 +7,58 @@
|
|
7
7
|
|
8
8
|
|
9
9
|
###試したこと
|
10
|
-
カスタム投稿に追加してもらった記事のタイトルをfunctions.phpで一覧取得し、そのショートコードを使用して一覧表示しようと思ったのですが、functions.php内で「echo」を使用するとformの外に一覧表示されてしまい、「return」を使用すると1件しか取得できない問題がおきてしまいます。
|
10
|
+
カスタム投稿に追加してもらった記事のタイトルをfunctions.phpで一覧取得し、そのショートコードを使用して一覧表示しようと思ったのですが、functions.php内で「echo」を使用するとformの外に一覧表示されてしまい、「return」を使用すると1件しか取得できない問題がおきてしまいます。
|
11
|
+
下記コードはカテゴリーごとに一覧表示させています。
|
12
|
+
|
13
|
+
```ここに言語を入力
|
14
|
+
function post_list(){
|
15
|
+
$taxonomyName = "product_category";
|
16
|
+
//親タームだとprentが0なのでこれを指定
|
17
|
+
$args = array(
|
18
|
+
'parent' => 0
|
19
|
+
);
|
20
|
+
//親を取得
|
21
|
+
$terms = get_terms($taxonomyName,$args);
|
22
|
+
foreach ($terms as $term) {
|
23
|
+
//親のIDを取得できた
|
24
|
+
$parentId = $term->term_id;
|
25
|
+
|
26
|
+
/*ここから親タームのIDがそれぞれ取得できるので、そこから子タームのリストを作る*/
|
27
|
+
//さっきはparentが0だったが、こんかいはprentが親IDの場合の条件
|
28
|
+
|
29
|
+
$childargs = array(
|
30
|
+
'parent' => $parentId,
|
31
|
+
'hide_empty' => true//投稿がない場合も隠さずにだす
|
32
|
+
);
|
33
|
+
$childterms = get_terms($taxonomyName,$childargs);
|
34
|
+
echo '<div>';
|
35
|
+
|
36
|
+
foreach ($childterms as $childterm) {
|
37
|
+
$targetSlug = $childterm->slug;
|
38
|
+
echo '<h2 id="'.$childterm->slug.'"><a href="'.get_term_link($childterm->slug, 'product_category').'">'.$childterm->name.'</a></h2>';//子タームの名前
|
39
|
+
//子ターム情報が取得できたので、ここからget_posts用に準備
|
40
|
+
$postargs = array(
|
41
|
+
'post_type' => 'product',//さっきのbrandsタクソノミーを使ってる“投稿”のポストタイプ
|
42
|
+
'tax_query' => array(
|
43
|
+
array(
|
44
|
+
'taxonomy' => $taxonomyName,
|
45
|
+
'field' => 'slug',//フィールドをslugにしておくterm_idとかでも良いはず
|
46
|
+
'terms' => $targetSlug//上で準備してある$childterm->slug
|
47
|
+
)
|
48
|
+
)
|
49
|
+
);
|
50
|
+
|
51
|
+
//記事取得
|
52
|
+
$postslist = get_posts( $postargs );
|
53
|
+
foreach ( $postslist as $post ) : setup_postdata( $post );
|
54
|
+
echo get_the_title(); //タイトル
|
55
|
+
|
56
|
+
endforeach;
|
57
|
+
wp_reset_postdata();
|
58
|
+
}
|
59
|
+
echo '</div>';
|
60
|
+
}
|
61
|
+
|
62
|
+
}
|
63
|
+
wpcf7_add_form_tag("list", "post_list");
|
64
|
+
```
|