teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

6

解決しました。

2017/06/01 17:24

投稿

thewild
thewild

スコア32

title CHANGED
File without changes
body CHANGED
@@ -130,4 +130,101 @@
130
130
  これは例ですが、このarrayの中にカスタムフィールドで取得した値をループで格納する方法が分からなかったのと、
131
131
  カスタム投稿shopのタイトル(店舗名)と、タームを関連付ける方法が分からなかったです。
132
132
 
133
- よろしくお願いいたします。
133
+ よろしくお願いいたします。
134
+
135
+ ###解決しました。
136
+
137
+ 以下最終コード
138
+ ```php
139
+ <ul>
140
+ <?php
141
+ $args = array(
142
+ 'posts_per_page' => 7,
143
+ 'paged' => $paged,
144
+ 'orderby' => 'post_date',
145
+ 'post_type' => 'shop',
146
+ 'post_status' => 'publish'
147
+ );
148
+ $the_query = new WP_Query($args);
149
+ ?>
150
+ <?PHP if ( $the_query->have_posts() ) :?>
151
+ <?php $bloglist = array(); ?>
152
+ <?php while ( $the_query->have_posts() ) : $the_query->the_post();?>
153
+ <?PHP include locate_template('/feed/rss-one.php'); ?>
154
+ <?php endwhile; ?>
155
+ <?php // 日付でソート
156
+ foreach ((array)$bloglist as $key => $value) {
157
+ $sort[$key] = $value['update'];
158
+ }
159
+ array_multisort($sort, SORT_DESC, $bloglist);
160
+ ?>
161
+ <?php
162
+ for($i = 0 ; $i < count($bloglist); $i++){
163
+ echo $bloglist[$i]['text'] ;
164
+ }
165
+ ?>
166
+ <?php endif; ?>
167
+ <?php wp_reset_postdata(); ?>
168
+ </ul>
169
+ ```
170
+ ↓rss-one.php
171
+ ```php
172
+ <?php
173
+ require_once "Feed.php" ; //rss-phpライブラリを読み込みます
174
+ $feed = new Feed ;
175
+ date_default_timezone_set('Asia/Tokyo');
176
+ $txt = get_field('rss');//←カスタムフィールドを取得
177
+ if($txt){
178
+ $url = $txt;
179
+ $rss = $feed->loadRss( $url ) ;
180
+ $num = 1;//表示させたい件数
181
+ $i=0;
182
+ $desW = 50;//詳細の文字数を制限します。制限しないときは0にします。
183
+ if ( $desW != 0){
184
+ $desW = ($desW*2)+2;
185
+ }
186
+ foreach( $rss->item as $item )
187
+ {
188
+ if($i>=$num){
189
+ }
190
+ else{
191
+ $title = $item->title ; // タイトル
192
+ $link = $item->link ; // リンク
193
+ $timestamp = strtotime( $item->pubDate ) ; // 更新日時
194
+ $description = $item->description ; // 詳細
195
+ $description = str_replace("▼続きを読む","",$description);
196
+ //↑ 続きを読むなど、決まった文章が詳細にはいっている場合に、それを除外する
197
+ $description = strip_tags($description);
198
+ if ( $desW != 0){
199
+ $description = mb_strimwidth($description, 0, $desW, "…",'utf-8');
200
+ }
201
+ ?>
202
+ <?php
203
+ $html = "<li class='rss'>";
204
+ $html .="<p class='post-date'>" . date( 'H:i', $timestamp) . "</p>";
205
+ $html .="<h3><a href=" . $link . "target='_blank'>" . $title . "</a></h3>";
206
+ $html .="<p class='areaname'>";
207
+ $terms = get_the_terms($post->ID,'area');
208
+ foreach( $terms as $term ) {
209
+ $html .= $term->name;
210
+ };
211
+ $html .="</p>";
212
+ $html .="<p class='shopname'><a href=" . get_permalink($post->ID) .">" . the_title('','',false) . "</a></p>";
213
+ $html .="</li>";
214
+ ?>
215
+ <?php $shopblog = array(
216
+ "text"=>$html,
217
+ "update"=>date( 'YmdHi', $timestamp)
218
+ );
219
+ ?>
220
+ <?php
221
+ $i++;
222
+ $bloglist[]=$shopblog;
223
+ }
224
+ }
225
+ }
226
+ ?>
227
+
228
+ ```
229
+
230
+ ありがとうございました!

5

追記修正

2017/06/01 17:24

投稿

thewild
thewild

スコア32

title CHANGED
File without changes
body CHANGED
@@ -99,69 +99,7 @@
99
99
  店舗ごとに外部ブログがあり、最新記事を一つだけ表示するようにしてます。
100
100
  当たり前ですが現在はrssの更新順ではなく、ワードプレスに投稿した記事の日付順になってますので、これをこの形のままrssの更新順にしたいです。
101
101
 
102
- ##追記
103
- ```php
104
- <?php
105
- require_once "Feed.php" ; //rss-phpライブラリを読み込みます
106
- $feed = new Feed ;
107
- date_default_timezone_set('Asia/Tokyo');
108
- $txt = get_field('rss');//←カスタムフィールドを取得
109
- if($txt){
110
- $url = $txt;
111
- $rss = $feed->loadRss( $url ) ;
112
- $num = 1;//表示させたい件数
113
- $i=0;
114
- $desW = 50;//詳細の文字数を制限します。制限しないときは0にします。
115
- if ( $desW != 0){
116
- $desW = ($desW*2)+2;
117
- }
118
- foreach( $rss->item as $item )
119
- {
120
- if($i>=$num){
121
- }
122
- else{
123
- $title = $item->title ; // タイトル
124
- $link = $item->link ; // リンク
125
- $timestamp = strtotime( $item->pubDate ) ; // 更新日時
126
- $description = $item->description ; // 詳細
127
- $description = str_replace("▼続きを読む","",$description);
128
- //↑ 続きを読むなど、決まった文章が詳細にはいっている場合に、それを除外する
129
- $description = strip_tags($description);
130
- if ( $desW != 0){
131
- $description = mb_strimwidth($description, 0, $desW, "…",'utf-8');
132
- }
133
- ?>
134
- /////////ここから変更////////////
135
- <?php
136
- $html = "<li class='rss'>";
137
- $html .="<p class='post-date'>" . date( 'H:i', $timestamp) . "</p>";
138
- $html .="<h3><a href=" . $link . "target='_blank'>" . $title . "</a></h3>";
139
- $html .="<p class='areaname'>";
140
- $terms = get_the_terms($post->ID,'area');
141
- foreach( $terms as $term ) {
142
- $html .= $term->name;
143
- };
144
- $html .="</p>";
145
- $html .="<p class='shopname'><a href=" . get_permalink($post->ID) .">" . the_title('','',false) . "</a></p>";
146
- $html .="</li>";
147
- ?>
148
- <?php $shopblog = array(
149
- "text"=>$html,
150
- "update"=>date( 'YmdHi', $timestamp)
151
- );
152
- $bloglist = array();
153
- ?>
154
- <?php
155
- $i++;
156
- $bloglist []=$shopblog;
157
- var_dump($bloglist);
158
- }
159
- }
160
- }
161
- ?>
162
102
 
163
- ```
164
-
165
103
  ###不明点
166
104
  ```php
167
105
  <?php $shopblog = array(
@@ -190,4 +128,6 @@
190
128
  );
191
129
  ```
192
130
  これは例ですが、このarrayの中にカスタムフィールドで取得した値をループで格納する方法が分からなかったのと、
193
- カスタム投稿shopのタイトル(店舗名)と、タームを関連付ける方法が分からなかったです。
131
+ カスタム投稿shopのタイトル(店舗名)と、タームを関連付ける方法が分からなかったです。
132
+
133
+ よろしくお願いいたします。

4

コード内容を現状に

2017/06/01 15:53

投稿

thewild
thewild

スコア32

title CHANGED
File without changes
body CHANGED
@@ -13,18 +13,22 @@
13
13
  ```php
14
14
  <ul>
15
15
  <?php
16
- $args = array(
16
+ $args = array(
17
- 'posts_per_page' => 7,
17
+ 'posts_per_page' => 7,
18
- 'paged' => $paged,
18
+ 'paged' => $paged,
19
- 'orderby' => 'post_date',//ここで指定してもカスタム投稿の投稿順になるだけなので意味がない。
19
+ 'orderby' => 'post_date',
20
- 'post_type' => 'shop',//shopという名のカスタム投稿
20
+ 'post_type' => 'shop',
21
- 'post_status' => 'publish'
21
+ 'post_status' => 'publish'
22
- );
22
+ );
23
- $the_query = new WP_Query($args);?>
23
+ $the_query = new WP_Query($args);
24
- <?PHP
24
+ ?>
25
- if ( $the_query->have_posts() ) :?>
25
+ <?php if ( $the_query->have_posts() ) :?>
26
26
  <?php while ( $the_query->have_posts() ) : $the_query->the_post();?>
27
+ <?php
28
+ $bloglist = array();
27
- <?PHP get_template_part('feed/rss-one'); ?>
29
+ get_template_part('feed/rss-one');
30
+ var_dump($bloglist);
31
+ ?>
28
32
  <?php endwhile; ?>
29
33
  <?php endif; ?>
30
34
  <?php wp_reset_postdata(); ?>
@@ -37,13 +41,13 @@
37
41
  require_once "Feed.php" ; //rss-phpライブラリを読み込みます
38
42
  $feed = new Feed ;
39
43
  date_default_timezone_set('Asia/Tokyo');
40
- $txt = get_field('rss');//←カスタムフィールドでurlを取得
44
+ $txt = get_field('rss');//←カスタムフィールドを取得
41
45
  if($txt){
42
46
  $url = $txt;
43
47
  $rss = $feed->loadRss( $url ) ;
44
48
  $num = 1;//表示させたい件数
45
49
  $i=0;
46
- $desW = 50;
50
+ $desW = 50;//詳細の文字数を制限します。制限しないときは0にします。
47
51
  if ( $desW != 0){
48
52
  $desW = ($desW*2)+2;
49
53
  }
@@ -63,21 +67,27 @@
63
67
  $description = mb_strimwidth($description, 0, $desW, "…",'utf-8');
64
68
  }
65
69
  ?>
66
- <li class="rss">
67
- <p class="post-date"><?php echo date( "H:i", $timestamp); ?></p>
68
- <h3><a href="<?php echo $link; ?>" target="_blank"><?php echo $title; ?></a></h3>
69
- <p class="areaname">
70
- <?php
70
+ <?php
71
+ $html = "<li class='rss'>";
72
+ $html .="<p class='post-date'>" . date( 'H:i', $timestamp) . "</p>";
73
+ $html .="<h3><a href=" . $link . "target='_blank'>" . $title . "</a></h3>";
74
+ $html .="<p class='areaname'>";
71
- $terms = get_the_terms($post->ID,'area');
75
+ $terms = get_the_terms($post->ID,'area');
72
- foreach( $terms as $term ) {
76
+ foreach( $terms as $term ) {
73
- echo $term->name;
77
+ $html .= $term->name;
74
- }
78
+ };
79
+ $html .="</p>";
80
+ $html .="<p class='shopname'><a href=" . get_permalink($post->ID) .">" . the_title('','',false) . "</a></p>";
81
+ $html .="</li>";
75
- ?>
82
+ ?>
83
+ <?php $shopblog = array(
76
- </p>
84
+ "text"=>$html,
77
- <p class="shopname"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
85
+ "update"=>date( 'YmdHi', $timestamp)
86
+ );
78
- </li>
87
+ ?>
79
88
  <?php
80
89
  $i++;
90
+ $bloglist[]=$shopblog;
81
91
  }
82
92
  }
83
93
  }

3

コード内容を現状に修正

2017/06/01 15:48

投稿

thewild
thewild

スコア32

title CHANGED
File without changes
body CHANGED
File without changes

2

試した方法を追加

2017/06/01 15:41

投稿

thewild
thewild

スコア32

title CHANGED
File without changes
body CHANGED
@@ -163,4 +163,21 @@
163
163
  という部分で$shopblogにテキストデータと、日付を関連付けて格納したのですが、
164
164
  ・新たに変数に格納するのを記述する場所
165
165
  ・ループで出力したこの値を新たに変数に格納する方法
166
+ ・出力する方法
166
- が不明です。
167
+ が不明です。
168
+
169
+ ##試した方法
170
+ ちなみにkei344さんが添付してくださったリンク先の方法もはじめに試しましたが、
171
+ ```php
172
+ // 取得するフィードのURLを指定
173
+ $urls = array(
174
+ "http://www.lesson5.info/?feed=rss2",
175
+ "http://www.lesson5.info/?feed=rss2",
176
+ "http://www.lesson5.info/?feed=rss2",
177
+ "http://www.lesson5.info/?feed=rss2",
178
+ "http://www.lesson5.info/?feed=rss2",
179
+ "http://www.lesson5.info/?feed=rss2"
180
+ );
181
+ ```
182
+ これは例ですが、このarrayの中にカスタムフィールドで取得した値をループで格納する方法が分からなかったのと、
183
+ カスタム投稿shopのタイトル(店舗名)と、タームを関連付ける方法が分からなかったです。

1

追記

2017/05/31 19:27

投稿

thewild
thewild

スコア32

title CHANGED
File without changes
body CHANGED
@@ -87,4 +87,80 @@
87
87
 
88
88
  ###補足情報
89
89
  店舗ごとに外部ブログがあり、最新記事を一つだけ表示するようにしてます。
90
- 当たり前ですが現在はrssの更新順ではなく、ワードプレスに投稿した記事の日付順になってますので、これをこの形のままrssの更新順にしたいです。
90
+ 当たり前ですが現在はrssの更新順ではなく、ワードプレスに投稿した記事の日付順になってますので、これをこの形のままrssの更新順にしたいです。
91
+
92
+ ##追記
93
+ ```php
94
+ <?php
95
+ require_once "Feed.php" ; //rss-phpライブラリを読み込みます
96
+ $feed = new Feed ;
97
+ date_default_timezone_set('Asia/Tokyo');
98
+ $txt = get_field('rss');//←カスタムフィールドを取得
99
+ if($txt){
100
+ $url = $txt;
101
+ $rss = $feed->loadRss( $url ) ;
102
+ $num = 1;//表示させたい件数
103
+ $i=0;
104
+ $desW = 50;//詳細の文字数を制限します。制限しないときは0にします。
105
+ if ( $desW != 0){
106
+ $desW = ($desW*2)+2;
107
+ }
108
+ foreach( $rss->item as $item )
109
+ {
110
+ if($i>=$num){
111
+ }
112
+ else{
113
+ $title = $item->title ; // タイトル
114
+ $link = $item->link ; // リンク
115
+ $timestamp = strtotime( $item->pubDate ) ; // 更新日時
116
+ $description = $item->description ; // 詳細
117
+ $description = str_replace("▼続きを読む","",$description);
118
+ //↑ 続きを読むなど、決まった文章が詳細にはいっている場合に、それを除外する
119
+ $description = strip_tags($description);
120
+ if ( $desW != 0){
121
+ $description = mb_strimwidth($description, 0, $desW, "…",'utf-8');
122
+ }
123
+ ?>
124
+ /////////ここから変更////////////
125
+ <?php
126
+ $html = "<li class='rss'>";
127
+ $html .="<p class='post-date'>" . date( 'H:i', $timestamp) . "</p>";
128
+ $html .="<h3><a href=" . $link . "target='_blank'>" . $title . "</a></h3>";
129
+ $html .="<p class='areaname'>";
130
+ $terms = get_the_terms($post->ID,'area');
131
+ foreach( $terms as $term ) {
132
+ $html .= $term->name;
133
+ };
134
+ $html .="</p>";
135
+ $html .="<p class='shopname'><a href=" . get_permalink($post->ID) .">" . the_title('','',false) . "</a></p>";
136
+ $html .="</li>";
137
+ ?>
138
+ <?php $shopblog = array(
139
+ "text"=>$html,
140
+ "update"=>date( 'YmdHi', $timestamp)
141
+ );
142
+ $bloglist = array();
143
+ ?>
144
+ <?php
145
+ $i++;
146
+ $bloglist []=$shopblog;
147
+ var_dump($bloglist);
148
+ }
149
+ }
150
+ }
151
+ ?>
152
+
153
+ ```
154
+
155
+ ###不明点
156
+ ```php
157
+ <?php $shopblog = array(
158
+ "text"=>$html,
159
+ "update"=>date( 'YmdHi', $timestamp)
160
+ );
161
+ ```
162
+
163
+ という部分で$shopblogにテキストデータと、日付を関連付けて格納したのですが、
164
+ ・新たに変数に格納するのを記述する場所
165
+ ・ループで出力したこの値を新たに変数に格納する方法
166
+ が不明です。