function scrape_villages_entertainment() {
$url = ‘https://www.thevillagesentertainment.com/nightly-entertainment/’;
$response = wp_remote_get($url);
$output = ‘
Top 5 Nightly Entertainment Events
- ‘;
- ‘ . esc_html(trim($title)) . ‘
- Could not load events. Try again later!
if (!is_wp_error($response)) {
$html = wp_remote_retrieve_body($response);
libxml_use_internal_errors(true); // Ignore HTML errors
$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
// Adjust selector based on page structure (guessing common classes)
$events = $xpath->query(‘//h2[contains(@class, “entry-title”) or contains(@class, “event-title”)]//a | //div[contains(@class, “event”) or contains(@class, “calendar-item”)]//a’);
$count = 0;
foreach ($events as $event) {
if ($count >= 5) break; // Limit to 5 events
$title = $event->textContent;
$link = $event->getAttribute(‘href’);
// Ensure link is absolute
if (strpos($link, ‘http’) !== 0) {
$link = ‘https://www.thevillagesentertainment.com’ . $link;
}
$output .= ‘
‘;
$count++;
}
} else {
$output .= ‘
‘;
}
$output .= ‘
‘;
return $output;
}
add_shortcode(‘villages_events’, ‘scrape_villages_entertainment’);
Content retrieved from: https://www.thevillagesentertainment.com/nightly-entertainment/.