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

回答編集履歴

6

コード修正

2016/02/22 14:14

投稿

takotakot
takotakot

スコア1111

answer CHANGED
@@ -30,22 +30,26 @@
30
30
  $feed_items = $ameblo_feed->get_items(0, $maxitems);
31
31
  endif;
32
32
 
33
+ // ここからメイン
33
34
  $post_next = TRUE;
35
+ $have_posts_checked = have_posts();
34
- while(true) {
36
+ while(TRUE) {
35
- if(have_posts()) {
37
+ if($have_posts_checked) {
36
38
  if($post_next) {
37
39
  the_post();
38
40
  $post_next = FALSE;
41
+ $have_posts_checked = have_posts();
39
42
  }
40
- if($counter >= count($feed_items) || get_the_date() > $feed_items[$counter]->get_date()) {
43
+ if($counter >= count($feed_items) || strcmp(get_the_date('Ymd'), $feed_items[$counter]->get_date('Ymd')) > 0) {
41
- the_post();
42
- the_time("Y.m.j");
44
+ the_time('Y.m.d');
45
+ the_title();
46
+ echo '<br>';
43
47
  $post_next = TRUE;
44
48
  continue;
45
49
  }
46
50
  }
47
- if($counter < count($feed_items) {
51
+ if($counter < count($feed_items)) {
48
- printf('<span class="date">%s</span><br />', $feed_items[$counter]->get_date('Y.m.d'));
52
+ printf('<span class="date">%s</span>%s<br />', $feed_items[$counter]->get_date('Y.m.d'), $feed_items[$counter]->get_title());
49
53
  ++$counter;
50
54
  }else{
51
55
  break;

5

the_post

2016/02/22 14:14

投稿

takotakot
takotakot

スコア1111

answer CHANGED
@@ -30,11 +30,17 @@
30
30
  $feed_items = $ameblo_feed->get_items(0, $maxitems);
31
31
  endif;
32
32
 
33
+ $post_next = TRUE;
33
34
  while(true) {
34
35
  if(have_posts()) {
36
+ if($post_next) {
37
+ the_post();
38
+ $post_next = FALSE;
39
+ }
35
40
  if($counter >= count($feed_items) || get_the_date() > $feed_items[$counter]->get_date()) {
36
41
  the_post();
37
42
  the_time("Y.m.j");
43
+ $post_next = TRUE;
38
44
  continue;
39
45
  }
40
46
  }

4

ミス修正

2016/02/22 08:43

投稿

takotakot
takotakot

スコア1111

answer CHANGED
@@ -32,14 +32,14 @@
32
32
 
33
33
  while(true) {
34
34
  if(have_posts()) {
35
- if($counter >= count($feed_items) || get_the_date() > $item->get_date()) {
35
+ if($counter >= count($feed_items) || get_the_date() > $feed_items[$counter]->get_date()) {
36
36
  the_post();
37
37
  the_time("Y.m.j");
38
38
  continue;
39
39
  }
40
40
  }
41
41
  if($counter < count($feed_items) {
42
- printf('<span class="date">%s</span><br />', $item->get_date('Y.m.d'));
42
+ printf('<span class="date">%s</span><br />', $feed_items[$counter]->get_date('Y.m.d'));
43
43
  ++$counter;
44
44
  }else{
45
45
  break;

3

処理イメージ

2016/02/22 07:40

投稿

takotakot
takotakot

スコア1111

answer CHANGED
@@ -7,6 +7,47 @@
7
7
 
8
8
  ループを一つにし、`the_post();` か `$item->get_title()` か、どちらかを用いるイメージですね。
9
9
 
10
+
11
+ 時間がないので、概要だけ…
12
+ ```PHP
13
+ <?php
14
+ include_once(ABSPATH . WPINC . '/feed.php');
15
+
16
+ $display_posts_count = 20;
17
+ $get_posts_count = 40;
18
+
19
+ query_posts('post_type=post&paged='.$paged);
20
+
21
+ $ameblo_feed = fetch_feed(array(
22
+ 'http://feedblog.ameba.jp/rss/ameblo/', // feed1
23
+ // feed2'http://rssblog.ameba.jp/AmebloID/rss20.xml',
24
+ // feed3'http://rssblog.ameba.jp/AmebloID/rss20.xml'
25
+ ));
26
+
27
+ $counter = 0;
28
+ if (!is_wp_error( $ameblo_feed ) ) :
29
+ $maxitems = $ameblo_feed->get_item_quantity($get_posts_count);
30
+ $feed_items = $ameblo_feed->get_items(0, $maxitems);
31
+ endif;
32
+
33
+ while(true) {
34
+ if(have_posts()) {
35
+ if($counter >= count($feed_items) || get_the_date() > $item->get_date()) {
36
+ the_post();
37
+ the_time("Y.m.j");
38
+ continue;
39
+ }
40
+ }
41
+ if($counter < count($feed_items) {
42
+ printf('<span class="date">%s</span><br />', $item->get_date('Y.m.d'));
43
+ ++$counter;
44
+ }else{
45
+ break;
46
+ }
47
+ }
48
+ ?>
49
+ ```
50
+
10
51
  ---
11
52
  [記事一覧をソート(並び替え)する](http://blog.anekipedia.com/cus/%E8%A8%98%E4%BA%8B%E4%B8%80%E8%A6%A7%E3%82%92%E3%82%BD%E3%83%BC%E3%83%88%EF%BC%88%E4%B8%A6%E3%81%B3%E6%9B%BF%E3%81%88%EF%BC%89%E3%81%99%E3%82%8B.html)
12
53
  [WordPressで思った通りの順番で記事をソート](http://www.off-soft.net/blog/wordpress/sort_post.html)

2

間違ったので修正

2016/02/22 06:01

投稿

takotakot
takotakot

スコア1111

answer CHANGED
@@ -1,14 +1,11 @@
1
- query_posts を使っているようなの
1
+ PHPプログラミングする必要がありますね。
2
- 私の以下の回答と
3
- https://wpdocs.osdn.jp/%E3%83%86%E3%83%B3%E3%83%97%E3%83%AC%E3%83%BC%E3%83%88%E3%82%BF%E3%82%B0/query_posts
4
- をよく読んで下さい。
5
- `<?php query_posts('post_type=post&paged='.$paged); ?>` を `<?php query_posts('post_type=post&orderby=date&paged='.$paged); ?>` ではだめですか?
6
- http://www.understandard.net/wordpress/wp009.html
7
- も読んでみたらいかがでしょうか。
8
2
 
3
+ 0. while ループで処理するとよいでしょう
9
- > `query_posts` 等で取得する際に`order` や `orderby` 与えると、並び替えができます。
4
+ 0. wordpress 側の最新記事とfeed 側の最新記事の時刻比較します。新しい方を出力します。
5
+ the_time("Y.m.j") と $item->get_date('Y.m.d') とを比較することになるでしょう。
6
+ 0. どちらかがなくなれば、もう片方をすべて出力します。
10
7
 
11
- という回答では不測でみません…
8
+ ループを一つに、`the_post();` `$item->get_title()` か、どちらかを用いるイメージでね。
12
9
 
13
10
  ---
14
11
  [記事一覧をソート(並び替え)する](http://blog.anekipedia.com/cus/%E8%A8%98%E4%BA%8B%E4%B8%80%E8%A6%A7%E3%82%92%E3%82%BD%E3%83%BC%E3%83%88%EF%BC%88%E4%B8%A6%E3%81%B3%E6%9B%BF%E3%81%88%EF%BC%89%E3%81%99%E3%82%8B.html)

1

query_posts

2016/02/22 05:22

投稿

takotakot
takotakot

スコア1111

answer CHANGED
@@ -1,3 +1,16 @@
1
+ query_posts を使っているようなので…
2
+ 私の以下の回答と
3
+ https://wpdocs.osdn.jp/%E3%83%86%E3%83%B3%E3%83%97%E3%83%AC%E3%83%BC%E3%83%88%E3%82%BF%E3%82%B0/query_posts
4
+ をよく読んで下さい。
5
+ `<?php query_posts('post_type=post&paged='.$paged); ?>` を `<?php query_posts('post_type=post&orderby=date&paged='.$paged); ?>` ではだめですか?
6
+ http://www.understandard.net/wordpress/wp009.html
7
+ も読んでみたらいかがでしょうか。
8
+
9
+ > `query_posts` 等で取得する際に、`order` や `orderby` を与えると、並び替えができます。
10
+
11
+ という回答では不測でしたか?すみません…
12
+
13
+ ---
1
14
  [記事一覧をソート(並び替え)する](http://blog.anekipedia.com/cus/%E8%A8%98%E4%BA%8B%E4%B8%80%E8%A6%A7%E3%82%92%E3%82%BD%E3%83%BC%E3%83%88%EF%BC%88%E4%B8%A6%E3%81%B3%E6%9B%BF%E3%81%88%EF%BC%89%E3%81%99%E3%82%8B.html)
2
15
  [WordPressで思った通りの順番で記事をソート](http://www.off-soft.net/blog/wordpress/sort_post.html)
3
16
  [カスタムフィールドで並び替え](http://elearn.jp/wpman/column/c20130809_01.html)