5,
),
$atts
);
// Get the latest rap news.
$news = latest_rap_news_get_latest_rap_news( $attributes[‘limit’] );
// If there is no news, return an empty string.
if ( empty( $news ) ) {
return ”;
}
// Create the HTML code for the latest rap news.
$html = ‘
- ‘;
- %s
foreach ( $news as $item ) {
$html .= sprintf(
‘
‘,
$item[‘url’],
$item[‘title’]
);
}
$html .= ‘
‘;
// Return the HTML code.
return $html;
}
/**
* Gets the latest rap news.
*
* @param int $limit The number of news items to return.
* @return array An array of news items.
*/
function latest_rap_news_get_latest_rap_news( $limit ) {
// Get the sources.
$sources = array(
‘https://www.billboard.com/articles/news/hip-hop/9576470/latest-rap-news’,
‘https://www.complex.com/music/latest-hip-hop-news’,
‘https://www.rollingstone.com/music/music-news/latest-hip-hop-news-1235240/’,
‘https://www.hotnewhiphop.com/’,
‘https://www.rap-up.com/’,
);
// Get the news from the sources.
$news = array();
foreach ( $sources as $source ) {
$response = wp_remote_get( $source );
if ( is_wp_error( $response ) ) {
continue;
}
$body = wp_remote_retrieve_body( $response );
$dom = new DOMDocument();
@$dom->loadHTML( $body );
$articles = $dom->getElementsByTagName( ‘article’ );
foreach ( $articles as $article ) {
$title = $article->getElementsByTagName( ‘h2’ )->item(0)->nodeValue;
$url = $article->getElementsByTagName( ‘a’ )->item(0)->getAttribute( ‘href’ );
$news[] = array(
‘title’ => $title,
‘url’ => $url,
);
}
}
// Limit the results.
$news = array_slice( $news, 0, $limit );
// Return the results.
return $news;
}
?>