###前提・実現したいこと
wordpressのカスタム投稿タイプ(shop)のカスタムフィールドに、rssのurlを入力し複数のrssを日付順に並び替えたい。
カスタム投稿タイプのターム(店舗名)も一緒に出力したい為、投稿に関連付けたワードプレスのカスタムフィールドの値で出力したい。
###発生している問題
rssの取得をし、一覧の表示は出来ましたが、rssで取得したブログの投稿順にソート出来ない。
###該当のソースコード
php
1<ul> 2 <?php 3 $args = array( 4 'posts_per_page' => 7, 5 'paged' => $paged, 6 'orderby' => 'post_date', 7 'post_type' => 'shop', 8 'post_status' => 'publish' 9 ); 10 $the_query = new WP_Query($args); 11 ?> 12 <?php if ( $the_query->have_posts() ) :?> 13 <?php while ( $the_query->have_posts() ) : $the_query->the_post();?> 14 <?php 15 $bloglist = array(); 16 get_template_part('feed/rss-one'); 17 var_dump($bloglist); 18 ?> 19 <?php endwhile; ?> 20 <?php endif; ?> 21 <?php wp_reset_postdata(); ?> 22</ul>
上記で取得しているrss-oneというテンプレ
php
1<?php 2require_once "Feed.php" ; //rss-phpライブラリを読み込みます 3$feed = new Feed ; 4date_default_timezone_set('Asia/Tokyo'); 5$txt = get_field('rss');//←カスタムフィールドを取得 6if($txt){ 7 $url = $txt; 8 $rss = $feed->loadRss( $url ) ; 9 $num = 1;//表示させたい件数 10 $i=0; 11 $desW = 50;//詳細の文字数を制限します。制限しないときは0にします。 12 if ( $desW != 0){ 13 $desW = ($desW*2)+2; 14 } 15 foreach( $rss->item as $item ) 16 { 17 if($i>=$num){ 18 } 19 else{ 20 $title = $item->title ; // タイトル 21 $link = $item->link ; // リンク 22 $timestamp = strtotime( $item->pubDate ) ; // 更新日時 23 $description = $item->description ; // 詳細 24 $description = str_replace("▼続きを読む","",$description); 25 //↑ 続きを読むなど、決まった文章が詳細にはいっている場合に、それを除外する 26 $description = strip_tags($description); 27 if ( $desW != 0){ 28 $description = mb_strimwidth($description, 0, $desW, "…",'utf-8'); 29 } 30 ?> 31 <?php 32 $html = "<li class='rss'>"; 33 $html .="<p class='post-date'>" . date( 'H:i', $timestamp) . "</p>"; 34 $html .="<h3><a href=" . $link . "target='_blank'>" . $title . "</a></h3>"; 35 $html .="<p class='areaname'>"; 36 $terms = get_the_terms($post->ID,'area'); 37 foreach( $terms as $term ) { 38 $html .= $term->name; 39 }; 40 $html .="</p>"; 41 $html .="<p class='shopname'><a href=" . get_permalink($post->ID) .">" . the_title('','',false) . "</a></p>"; 42 $html .="</li>"; 43 ?> 44 <?php $shopblog = array( 45 "text"=>$html, 46 "update"=>date( 'YmdHi', $timestamp) 47 ); 48 ?> 49 <?php 50 $i++; 51 $bloglist[]=$shopblog; 52 } 53 } 54} 55?> 56
###補足情報
店舗ごとに外部ブログがあり、最新記事を一つだけ表示するようにしてます。
当たり前ですが現在はrssの更新順ではなく、ワードプレスに投稿した記事の日付順になってますので、これをこの形のままrssの更新順にしたいです。
###不明点
php
1<?php $shopblog = array( 2 "text"=>$html, 3 "update"=>date( 'YmdHi', $timestamp) 4 );
という部分で$shopblogにテキストデータと、日付を関連付けて格納したのですが、
・新たに変数に格納するのを記述する場所
・ループで出力したこの値を新たに変数に格納する方法
・出力する方法
が不明です。
##試した方法
ちなみにkei344さんが添付してくださったリンク先の方法もはじめに試しましたが、
php
1// 取得するフィードのURLを指定 2 $urls = array( 3 "http://www.lesson5.info/?feed=rss2", 4 "http://www.lesson5.info/?feed=rss2", 5 "http://www.lesson5.info/?feed=rss2", 6 "http://www.lesson5.info/?feed=rss2", 7 "http://www.lesson5.info/?feed=rss2", 8 "http://www.lesson5.info/?feed=rss2" 9 );
これは例ですが、このarrayの中にカスタムフィールドで取得した値をループで格納する方法が分からなかったのと、
カスタム投稿shopのタイトル(店舗名)と、タームを関連付ける方法が分からなかったです。
よろしくお願いいたします。
###解決しました。
以下最終コード
php
1<ul> 2 <?php 3 $args = array( 4 'posts_per_page' => 7, 5 'paged' => $paged, 6 'orderby' => 'post_date', 7 'post_type' => 'shop', 8 'post_status' => 'publish' 9 ); 10 $the_query = new WP_Query($args); 11 ?> 12 <?PHP if ( $the_query->have_posts() ) :?> 13 <?php $bloglist = array(); ?> 14 <?php while ( $the_query->have_posts() ) : $the_query->the_post();?> 15 <?PHP include locate_template('/feed/rss-one.php'); ?> 16 <?php endwhile; ?> 17 <?php // 日付でソート 18 foreach ((array)$bloglist as $key => $value) { 19 $sort[$key] = $value['update']; 20 } 21 array_multisort($sort, SORT_DESC, $bloglist); 22 ?> 23 <?php 24 for($i = 0 ; $i < count($bloglist); $i++){ 25 echo $bloglist[$i]['text'] ; 26 } 27 ?> 28 <?php endif; ?> 29 <?php wp_reset_postdata(); ?> 30</ul>
↓rss-one.php
php
1<?php 2require_once "Feed.php" ; //rss-phpライブラリを読み込みます 3$feed = new Feed ; 4date_default_timezone_set('Asia/Tokyo'); 5$txt = get_field('rss');//←カスタムフィールドを取得 6if($txt){ 7 $url = $txt; 8 $rss = $feed->loadRss( $url ) ; 9 $num = 1;//表示させたい件数 10 $i=0; 11 $desW = 50;//詳細の文字数を制限します。制限しないときは0にします。 12 if ( $desW != 0){ 13 $desW = ($desW*2)+2; 14 } 15 foreach( $rss->item as $item ) 16 { 17 if($i>=$num){ 18 } 19 else{ 20 $title = $item->title ; // タイトル 21 $link = $item->link ; // リンク 22 $timestamp = strtotime( $item->pubDate ) ; // 更新日時 23 $description = $item->description ; // 詳細 24 $description = str_replace("▼続きを読む","",$description); 25 //↑ 続きを読むなど、決まった文章が詳細にはいっている場合に、それを除外する 26 $description = strip_tags($description); 27 if ( $desW != 0){ 28 $description = mb_strimwidth($description, 0, $desW, "…",'utf-8'); 29 } 30 ?> 31 <?php 32 $html = "<li class='rss'>"; 33 $html .="<p class='post-date'>" . date( 'H:i', $timestamp) . "</p>"; 34 $html .="<h3><a href=" . $link . "target='_blank'>" . $title . "</a></h3>"; 35 $html .="<p class='areaname'>"; 36 $terms = get_the_terms($post->ID,'area'); 37 foreach( $terms as $term ) { 38 $html .= $term->name; 39 }; 40 $html .="</p>"; 41 $html .="<p class='shopname'><a href=" . get_permalink($post->ID) .">" . the_title('','',false) . "</a></p>"; 42 $html .="</li>"; 43 ?> 44 <?php $shopblog = array( 45 "text"=>$html, 46 "update"=>date( 'YmdHi', $timestamp) 47 ); 48 ?> 49 <?php 50 $i++; 51 $bloglist[]=$shopblog; 52 } 53 } 54} 55?> 56
ありがとうございました!
回答1件
あなたの回答
tips
プレビュー