当使用带有某些属性的WordPress短代码时,尽管有这种情况,它也不会显示任何内容。
我正在尝试为希望在其站点上进行新闻聚合以使其内容进行更新的客户端在WordPress中创建一个功能。在此期间,我对RSS feed进行了一些反复试验(因为这是我第一次使用其中的多个方法)。借助WordPress上的调试功能,似乎什么都不会引发任何错误,而从查看代码来看,一切似乎对我来说都是正常的。
正在全新安装在PHP 7.2上运行的WordPress 5.2.2中对该脚本进行测试
这是编写的脚本(如果您知道可以在代码中使用的更好的编码实践等,请告诉我,因为我也有改进的余地)
function differenceInHours($startdate, $enddate)
{
$starttimestamp = strtotime($startdate);
$endtimestamp = strtotime($enddate);
$difference = abs($endtimestamp - $starttimestamp) / 3600;
return $difference;
}
function differenceInMinutes($startdate, $enddate)
{
$starttimestamp = strtotime($startdate);
$endtimestamp = strtotime($enddate);
$difference = abs($endtimestamp - $starttimestamp) / 60;
return $difference;
}
function customthemefunction($atts = [])
{
$atts = array_change_key_case((array)$atts, CASE_LOWER);
$fts_atts = shortcode_atts([
'team' => 'default',
], $atts, 'footballnewsfeed');
switch (esc_html__($fts_atts['team'], 'footballnewsfeed')) {
case "arsenal":
$RSS = array("https://readarsenal.com/feed",
"https://www.football.london/arsenal-fc/?service=rss",
"https://www.dailymail.co.uk/sport/football_league_championship/index.rss",
"https://footballfacts.net/category/arsenal/feed/",
"https://balkantalk.com/category/premier-league/arsenal/feed/",
"https://www.footballtransfertavern.com/category/premier-league/arsenal/feed/",
"https://paininthearsenal.com/category/arsenal-news/feed/",
"https://icfootballnews.com/category/arsenal/feed",
"https://www.justarsenal.com/feed",
"https://football-talk.co.uk/topics/arsenal/feed/");
ob_start();
sortRSS($RSS);
return ob_get_clean();
case "astonvilla":
$RSS = array("https://www.hitc.com/en-gb/topic/aston-villa-fc/rss.xml",
"https://readastonvilla.com/feed",
"https://www.dailymail.co.uk/sport/teampages/aston-villa.rss",
"https://the4thofficial.net/category/leagues/english-premier-league/aston-villa/feed/",
"https://madaboutepl.net/category/leagues/englishchampionship/astonvilla/feed/",
"https://www.mediareferee.com/tag/aston-villa/");
ob_start();
sortRSS($RSS);
return ob_get_clean();
case "blackburn":
$RSS = array("https://footballleagueworld.co.uk/category/sky-bet-championship/blackburn-rovers/feed/",
"https://www.hitc.com/en-gb/topic/blackburn-rovers-fc/rss.xml",
"https://www.dailymail.co.uk/sport/teampages/blackburn-rovers.rss",
"https://www.theguardian.com/football/blackburn/rss",
"https://www.mediareferee.com/tag/blackburn-rovers/feed");
ob_start();
sortRSS($RSS);
return ob_get_clean();
case "bolton":
$RSS = array("https://www.hitc.com/en-gb/topic/bolton-wanderers-fc/rss.xml",
"https://thefootballfaithful.com/tag/bolton-wanderers/feed/",
"https://www.theguardian.com/football/boltonwanderers/rss",
"https://www.dailymail.co.uk/sport/teampages/bolton-wanderers.rss",
"https://www.mediareferee.com/tag/bolton-wanderers/feed");
ob_start();
sortRSS($RSS);
return ob_get_clean();
case "brighton":
$RSS = array("https://thefootballfaithful.com/tag/brighton-hove-albion/feed/",
"https://www.hitc.com/en-gb/topic/brighton-and-hove-albion-fc/feed",
"https://www.theguardian.com/football/brightonfootball/rss",
"https://www.dailymail.co.uk/sport/teampages/brighton-and-hove-albion.rss",
"https://www.theargus.co.uk/sport/albion/rss/",
"http://api.sportskeeda.com/en/v1/feed/team/brighton-hove-albion",
"https://madaboutepl.net/category/leagues/englishpremierleague/brightonhove/feed/",
"https://www.mediareferee.com/tag/brighton-and-hove-albion/feed");
ob_start();
sortRSS($RSS);
return ob_get_clean();
case "chelsea":
$RSS = array("https://www.dailymail.co.uk/sport/chelsea_transfer_news/index.rss",
"https://www.dailymail.co.uk/sport/teampages/chelsea.rss",
"https://footballfacts.net/category/chelsea/feed",
"https://madaboutepl.net/category/leagues/englishpremierleague/chelsea/feed",
"https://www.football.london/chelsea-fc/?service=rss",
"https://the4thofficial.net/category/leagues/english-premier-league/chelsea/feed",
"https://thefootballfaithful.com/tag/chelsea/feed",
"http://api.sportskeeda.com/en/v1/feed/team/chelsea",
"https://www.mediareferee.com/tag/chelsea/feed",
"http://chelseapresser.co.uk/feed/");
ob_start();
sortRSS($RSS);
return ob_get_clean();
case "everton":
$RSS = array("https://www.dailymail.co.uk/sport/teampages/everton.rss",
"https://readeverton.com/feed",
"https://www.footballfancast.com/premier-league/everton/feed",
"https://madaboutepl.net/category/leagues/englishpremierleague/everton/feed",
"https://the4thofficial.net/category/leagues/english-premier-league/everton/feed/",
"https://thefootballfaithful.com/tag/everton/feed",
"http://api.sportskeeda.com/en/v1/feed/team/everton",
"https://www.liverpoolecho.co.uk/all-about/everton-fc/?service=rss",
"https://www.mediareferee.com/tag/everton/feed");
ob_start();
sortRSS($RSS);
return ob_get_clean();
case "fulham":
$RSS = array("https://www.dailymail.co.uk/sport/teampages/fulham.rss",
"http://www.fulhamfc.com/news/rss",
"https://the72.co.uk/category/fulham-championship/feed/",
"https://www.footballfancast.com/championship/fulham/feed",
"https://madaboutepl.net/category/leagues/englishpremierleague/fulham/feed/",
"https://the4thofficial.net/category/leagues/english-premier-league/fulham/feed/",
"https://thefootballfaithful.com/tag/fulham/feed",
"http://api.sportskeeda.com/en/v1/feed/team/fulham",
"https://www.mediareferee.com/tag/fulham/feed");
ob_start();
sortRSS($RSS);
return ob_get_clean();
case "liverpool":
$RSS = array("https://www.dailymail.co.uk/sport/teampages/liverpool.rss",
"https://www.caughtoffside.com/tags/premier-league/liverpool/feed/",
"https://www.liverpoolecho.co.uk/all-about/liverpool-fc/?service=rss",
"https://www.footballfancast.com/championship/liverpool/feed",
"https://madaboutepl.net/category/leagues/englishpremierleague/liverpool/feed/",
"https://the4thofficial.net/category/leagues/english-premier-league/liverpool/feed/",
"https://thefootballfaithful.com/tag/liverpool/feed",
"http://api.sportskeeda.com/en/v1/feed/team/liverpool",
"https://www.mediareferee.com/tag/liverpool/feed");
ob_start();
sortRSS($RSS);
return ob_get_clean();
case "manchestercity":
$RSS = array("https://www.dailymail.co.uk/sport/teampages/manchester-city.rss",
"https://www.caughtoffside.com/tags/premier-league/manchester-city/feed/",
"https://www.footballfancast.com/championship/manchester-city/feed",
"https://madaboutepl.net/category/leagues/englishpremierleague/manchester-city/feed/",
"https://the4thofficial.net/category/leagues/english-premier-league/manchester-city/feed/",
"https://thefootballfaithful.com/tag/manchester-city/feed/",
"http://api.sportskeeda.com/en/v1/feed/team/manchester-city",
"https://www.mediareferee.com/tag/manchester-city/feed/");
ob_start();
sortRSS($RSS);
return ob_get_clean();
case "manchesterunited":
$RSS = array("https://www.dailymail.co.uk/sport/teampages/manchester-united.rss",
"https://www.caughtoffside.com/tags/premier-league/manchester-united/feed/",
"https://www.footballfancast.com/championship/manchester-united/feed",
"https://madaboutepl.net/category/leagues/englishpremierleague/manchester-united/feed/",
"https://the4thofficial.net/category/leagues/english-premier-league/manchester-united/feed/",
"https://thefootballfaithful.com/tag/manchester-united/feed/",
"http://api.sportskeeda.com/en/v1/feed/team/manchester-united",
"https://www.mediareferee.com/tag/manchester-united/feed/");
ob_start();
sortRSS($RSS);
return ob_get_clean();
case "newcastleunited":
$RSS = array("https://www.dailymail.co.uk/sport/teampages/newcastle-united.rss",
"https://www.caughtoffside.com/tags/premier-league/newcastle-united/feed/",
"https://www.footballfancast.com/championship/newcastle-united/feed",
"https://madaboutepl.net/category/leagues/englishpremierleague/newcastle-united/feed/",
"https://the4thofficial.net/category/leagues/english-premier-league/newcastle-united/feed/",
"https://thefootballfaithful.com/tag/newcastle-united/feed/",
"http://api.sportskeeda.com/en/v1/feed/team/newcastle-united",
"https://www.mediareferee.com/tag/newcastle-united/feed/");
ob_start();
sortRSS($RSS);
return ob_get_clean();
case "norwich":
$RSS = array("https://www.dailymail.co.uk/sport/teampages/norwich-fc.rss",
"https://www.caughtoffside.com/tags/premier-league/norwich-fc/feed/",
"https://www.footballfancast.com/championship/norwich-fc/feed",
"https://madaboutepl.net/category/leagues/englishpremierleague/norwich-fc/feed/",
"https://the4thofficial.net/category/leagues/english-premier-league/norwich-fc/feed/",
"https://thefootballfaithful.com/tag/norwich-fc/feed/",
"http://api.sportskeeda.com/en/v1/feed/team/norwich-fc",
"https://www.mediareferee.com/tag/norwich-fc/feed/");
ob_start();
sortRSS($RSS);
return ob_get_clean();
case "qpr":
$RSS = array("https://www.dailymail.co.uk/sport/teampages/qpr-fc.rss",
"https://www.caughtoffside.com/tags/premier-league/qpr-fc/feed/",
"https://www.footballfancast.com/championship/qpr-fc/feed",
"https://madaboutepl.net/category/leagues/englishpremierleague/qpr-fc/feed/",
"https://the4thofficial.net/category/leagues/english-premier-league/qpr-fc/feed/",
"https://thefootballfaithful.com/tag/qpr-fc/feed/",
"http://api.sportskeeda.com/en/v1/feed/team/qpr-fc",
"https://www.mediareferee.com/tag/qpr-fc/feed/");
ob_start();
sortRSS($RSS);
return ob_get_clean();
case "reading":
$RSS = array("https://www.dailymail.co.uk/sport/teampages/reading-fc.rss",
"https://www.caughtoffside.com/tags/premier-league/reading-fc/feed/",
"https://www.footballfancast.com/championship/reading-fc/feed",
"https://madaboutepl.net/category/leagues/englishpremierleague/reading-fc/feed/",
"https://the4thofficial.net/category/leagues/english-premier-league/reading-fc/feed/",
"https://thefootballfaithful.com/tag/reading-fc/feed/",
"http://api.sportskeeda.com/en/v1/feed/team/reading-fc",
"https://www.mediareferee.com/tag/reading-fc/feed/");
ob_start();
sortRSS($RSS);
return ob_get_clean();
case "southampton":
$RSS = array("https://www.dailymail.co.uk/sport/teampages/southampton-fc.rss",
"https://www.caughtoffside.com/tags/premier-league/southampton-fc/feed/",
"https://www.footballfancast.com/championship/southampton-fc/feed",
"https://madaboutepl.net/category/leagues/englishpremierleague/southampton-fc/feed/",
"https://the4thofficial.net/category/leagues/english-premier-league/southampton-fc/feed/",
"https://thefootballfaithful.com/tag/southampton-fc/feed/",
"http://api.sportskeeda.com/en/v1/feed/team/southampton-fc",
"https://www.mediareferee.com/tag/southampton-fc/feed/");
ob_start();
sortRSS($RSS);
return ob_get_clean();
case "stokecity":
$RSS = array("https://www.dailymail.co.uk/sport/teampages/stoke-city.rss",
"https://www.caughtoffside.com/tags/premier-league/stoke-city/feed/",
"https://www.footballfancast.com/championship/stoke-city/feed",
"https://madaboutepl.net/category/leagues/englishpremierleague/stoke-city/feed/",
"https://the4thofficial.net/category/leagues/english-premier-league/stoke-city/feed/",
"https://thefootballfaithful.com/tag/stoke-city/feed/",
"http://api.sportskeeda.com/en/v1/feed/team/stoke-city",
"https://www.mediareferee.com/tag/stoke-city/feed/");
ob_start();
sortRSS($RSS);
return ob_get_clean();
case "sunderland":
$RSS = array("https://www.dailymail.co.uk/sport/teampages/sunderland-fc.rss",
"https://www.caughtoffside.com/tags/premier-league/sunderland-fc/feed/",
"https://www.footballfancast.com/championship/sunderland-fc/feed",
"https://madaboutepl.net/category/leagues/englishpremierleague/sunderland-fc/feed/",
"https://the4thofficial.net/category/leagues/english-premier-league/sunderland-fc/feed/",
"https://thefootballfaithful.com/tag/sunderland-fc/feed/",
"http://api.sportskeeda.com/en/v1/feed/team/sunderland-fc",
"https://www.mediareferee.com/tag/sunderland-fc/feed/");
ob_start();
sortRSS($RSS);
return ob_get_clean();
case "swanseacity":
$RSS = array("https://www.dailymail.co.uk/sport/teampages/swansea-city-fc.rss",
"https://www.caughtoffside.com/tags/premier-league/swansea-city-fc/feed/",
"https://www.footballfancast.com/championship/swansea-city-fc/feed",
"https://madaboutepl.net/category/leagues/englishpremierleague/swansea-city-fc/feed/",
"https://the4thofficial.net/category/leagues/english-premier-league/swansea-city-fc/feed/",
"https://thefootballfaithful.com/tag/swansea-city-fc/feed/",
"http://api.sportskeeda.com/en/v1/feed/team/swansea-city-fc",
"https://www.mediareferee.com/tag/swansea-city-fc/feed/");
ob_start();
sortRSS($RSS);
return ob_get_clean();
case "tottenham":
$RSS = array("https://www.dailymail.co.uk/sport/teampages/tottenham-fc.rss",
"https://www.caughtoffside.com/tags/premier-league/tottenham-fc/feed/",
"https://www.footballfancast.com/championship/tottenham-fc/feed",
"https://madaboutepl.net/category/leagues/englishpremierleague/tottenham-fc/feed/",
"https://the4thofficial.net/category/leagues/english-premier-league/tottenham-fc/feed/",
"https://thefootballfaithful.com/tag/tottenham-fc/feed/",
"http://api.sportskeeda.com/en/v1/feed/team/tottenham-fc",
"https://www.mediareferee.com/tag/tottenham-fc/feed/");
ob_start();
sortRSS($RSS);
return ob_get_clean();
case "westbromwichalbion":
$RSS = array("https://www.dailymail.co.uk/sport/teampages/west-bromwich-albion.rss",
"https://www.caughtoffside.com/tags/premier-league/west-bromwich-albion/feed/",
"https://www.footballfancast.com/championship/west-bromwich-albion/feed",
"https://madaboutepl.net/category/leagues/englishpremierleague/west-bromwich-albion/feed/",
"https://the4thofficial.net/category/leagues/english-premier-league/west-bromwich-albion/feed/",
"https://thefootballfaithful.com/tag/west-bromwich-albion/feed/",
"http://api.sportskeeda.com/en/v1/feed/team/west-bromwich-albion",
"https://www.mediareferee.com/tag/west-bromwich-albion/feed/");
ob_start();
sortRSS($RSS);
return ob_get_clean();
case "westham":
$RSS = array("https://www.dailymail.co.uk/sport/teampages/west-ham-fc.rss",
"https://www.caughtoffside.com/tags/premier-league/west-ham-fc/feed/",
"https://www.footballfancast.com/championship/west-ham-fc/feed",
"https://madaboutepl.net/category/leagues/englishpremierleague/west-ham-fc/feed/",
"https://the4thofficial.net/category/leagues/english-premier-league/west-ham-fc/feed/",
"https://thefootballfaithful.com/tag/west-ham-fc/feed/",
"http://api.sportskeeda.com/en/v1/feed/team/west-ham-fc",
"https://www.mediareferee.com/tag/west-ham-fc/feed/");
ob_start();
sortRSS($RSS);
return ob_get_clean();
case "wiganathletic":
$RSS = array("https://www.dailymail.co.uk/sport/teampages/wigan-athletic-fc.rss",
"https://www.caughtoffside.com/tags/premier-league/wigan-athletic-fc/feed/",
"https://www.footballfancast.com/championship/wigan-athletic-fc/feed",
"https://madaboutepl.net/category/leagues/englishpremierleague/wigan-athletic-fc/feed/",
"https://the4thofficial.net/category/leagues/english-premier-league/wigan-athletic-fc/feed/",
"https://thefootballfaithful.com/tag/wigan-athletic-fc/feed/",
"http://api.sportskeeda.com/en/v1/feed/team/wigan-athletic-fc",
"https://www.mediareferee.com/tag/wigan-athletic-fc/feed/");
ob_start();
sortRSS($RSS);
return ob_get_clean();
case "wolves":
$RSS = array("https://www.dailymail.co.uk/sport/teampages/wolves-fc.rss",
"https://www.caughtoffside.com/tags/premier-league/wolves-fc/feed/",
"https://www.footballfancast.com/championship/wolves-fc/feed",
"https://madaboutepl.net/category/leagues/englishpremierleague/wolves-fc/feed/",
"https://the4thofficial.net/category/leagues/english-premier-league/wolves-fc/feed/",
"https://thefootballfaithful.com/tag/wolves-fc/feed/",
"http://api.sportskeeda.com/en/v1/feed/team/wolves-fc",
"https://www.mediareferee.com/tag/wolves-fc/feed/");
ob_start();
sortRSS($RSS);
return ob_get_clean();
default:
$RSS = array("http://feeds.bbci.co.uk/sport/football/rss.xml?edition=uk",
"https://www.dailymail.co.uk/sport/england_football/index.rss",
"https://www.dailymail.co.uk/sport/football_league_championship/index.rss",
"https://www.dailymail.co.uk/sport/premierleague/index.rss",
"https://www.football.london/?service=rss",
"https://www.thesun.co.uk/sport/football/feed/");
ob_start();
sortRSS($RSS);
return ob_get_clean();
}
}
function sortRSS($RSS) {
$breaking = array();
$thirtytohour = array();
$hourtotwo = array();
$twotofour = array();
$four = array();
/*
* BREAKING NEWS ===========================================================================================================================
*/
for ($a = 0; $a <= sizeof($RSS) - 1; $a++) {
$xml = "";
$invalidRSS = false;
if (@simplexml_load_file($RSS[$a]))
$xml = simplexml_load_file($RSS[$a]);
else
$invalidRSS = true;
$now = date("H:i:s", time());
if ($invalidRSS) {
return "Invalid RSS Feed: " . $RSS[$a];
}
$z = 0;
foreach ($xml->channel->item as $item) {
if ($z > 5)
break;
$publish_date = $item->pubDate;
if (differenceInMinutes($publish_date, $now) <= 30) {
$item_title = $item->title;
$item_link = $item->link;
$item_desc = $item->description;
$res = "<p><a href='" . $item_link . "'>" . $item_title . "</a><br>" . $item_desc . "<br>";
array_push($breaking, $res);
$z++;
}
}
/*
* 30 MINS TO ONE HOUR OLD ===========================================================================================================================
*/
$x = 0;
foreach ($xml->channel->item as $item) {
if ($x > 5)
break;
$publish_date = $item->pubDate;
if (differenceInMinutes($publish_date, $now) >= 30 && differenceInMinutes($publish_date, $now) <= 60) {
$item_title = $item->title;
$item_link = $item->link;
$item_desc = $item->description;
$res = "<p><a href='" . $item_link . "'>" . $item_title . "</a><br>" . $item_desc;
array_push($thirtytohour, $res);
$x++;
}
}
/*
* ONE HOUR OLD TO 2 HOURS OLD ===========================================================================================================================
*/
$c = 0;
foreach ($xml->channel->item as $item) {
if ($c > 5)
break;
$publish_date = $item->pubDate;
if (differenceInHours($publish_date, $now) >= 1 && differenceInHours($publish_date, $now) <= 2) {
$item_title = $item->title;
$item_link = $item->link;
$item_desc = $item->description;
$res = "<p><a href='" . $item_link . "'>" . $item_title . "</a><br>" . $item_desc;
array_push($hourtotwo, $res);
$c++;
}
}
/*
* 2 HOURS OLD TO 4 HOURS OLD ===========================================================================================================================
*/
$v = 0;
foreach ($xml->channel->item as $item) {
if ($v > 5)
break;
$publish_date = $item->pubDate;
if (differenceInHours($publish_date, $now) >= 2 && differenceInHours($publish_date, $now) <= 4) {
$item_title = $item->title;
$item_link = $item->link;
$item_desc = $item->description;
$res = "<p><a href='" . $item_link . "'>" . $item_title . "</a><br>" . $item_desc;
array_push($twotofour, $res);
$v++;
}
}
/*
* 4 HOURS OLD ===========================================================================================================================
*/
$b = 0;
foreach ($xml->channel->item as $item) {
if ($b > 5)
break;
$publish_date = $item->pubDate;
if (differenceInHours($publish_date, $now) > 4) {
$item_title = $item->title;
$item_link = $item->link;
$item_desc = $item->description;
$res = "<p><a href='" . $item_link . "'>" . $item_title . "</a><br>" . $item_desc;
array_push($four, $res);
$b++;
}
}
}
return bespoke_fts_feed($breaking, $thirtytohour, $hourtotwo, $twotofour, $four);
}
function bespoke_fts_feed($breaking, $thirtytohour, $hourtotwo, $twotofour, $four)
{
echo "<h2>BREAKING NEWS</h2><br>";
for ($i = 0; $i <= sizeof($breaking) - 1; $i++) {
echo $breaking[$i];
}
echo "<br>";
echo "<h2>30 MINUTES TO 1 HOUR OLD</h2><br>";
for ($i = 0; $i <= sizeof($thirtytohour) - 1; $i++) {
echo $thirtytohour[$i];
}
echo "<br>";
echo "<h2>1 HOUR TO 2 HOURS OLD</h2><br>";
for ($i = 0; $i <= sizeof($hourtotwo) - 1; $i++) {
echo $hourtotwo[$i];
}
echo "<br>";
echo "<h2>2 HOURS TO 4 HOURS OLD</h2><br>";
for ($i = 0; $i <= sizeof($twotofour) - 1; $i++) {
echo $twotofour[$i];
}
echo "<br>";
echo "<h2>4 HOURS OLD</h2><br>";
for ($i = 0; $i <= sizeof($four) - 1; $i++) {
echo $four[$i];
}
echo (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ?
"https" : "http") . "://" . $_SERVER['HTTP_HOST'] .
$_SERVER['REQUEST_URI'];
}
add_shortcode('footballnewsfeed', 'customthemefunction');
现在,预期结果应该是由新闻网站提供的链接列表和简短描述,并按正确的时间类别进行过滤。
这适用于以下属性:阿森纳,布莱克本,布尔顿,埃弗顿,布赖顿,富勒姆,但不适用于其他任何属性(而是不返回任何内容,甚至不返回bespoke_fts_feed的回声部分,无论如何)。
没有错误代码(甚至不会生成error_log)或其他任何东西,这让我很困惑,我也不知道我要Google尝试解决什么问题。
预先感谢您的帮助,如果您需要任何帮助,我非常乐意提供。