Widget:UPMakingADifference: Difference between revisions
Appearance
Created page with " <includeonly> <div class="update-news"> <div class="update-online-loading"> Loading latest Making a Difference article... </div> <div class="update-online-content" style="display:none;"> <div class="update-news-image"> <img class="update-online-image" src="" alt="" style="display:none;"> </div> <div class="update-news-content"> <div class="update-..." |
No edit summary |
||
| (9 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
<includeonly> | <includeonly> | ||
<div class="update-news"> | <div class="update-news update-making-difference"> | ||
<div class="update-online-loading"> | <div class="update-online-loading"> | ||
| Line 21: | Line 20: | ||
<div class="update-online-date"></div> | <div class="update-online-date"></div> | ||
<h2 class="update-online-title"></h2> | <h2 class="update-online-title" style="color: #000000 !important;"></h2> | ||
<div class="update-online-excerpt"></div> | <div class="update-online-excerpt"></div> | ||
| Line 45: | Line 44: | ||
*/ | */ | ||
var widgets = | var widgets = document.querySelectorAll('.update-news'); | ||
var widget = widgets[widgets.length - 1]; | |||
var widget = | |||
if (!widget) { | if (!widget) { | ||
| Line 59: | Line 55: | ||
*/ | */ | ||
var loading = | var loading = widget.querySelector('.update-online-loading'); | ||
var content = widget.querySelector('.update-online-content'); | |||
var image = widget.querySelector('.update-online-image'); | |||
var content = | var title = widget.querySelector('.update-online-title'); | ||
var excerpt = widget.querySelector('.update-online-excerpt'); | |||
var link = widget.querySelector('.update-online-link'); | |||
var image = | var dateElement = widget.querySelector('.update-online-date'); | ||
var title = | |||
var excerpt = | |||
var link = | |||
var dateElement = | |||
/* | /* | ||
* | * Making a Difference API | ||
* Category ID: 5063 | |||
*/ | */ | ||
var api = | var api = | ||
'https://up.edu.ph/wp-json/wp/v2/posts' + | 'https://up.edu.ph/wp-json/wp/v2/posts' + | ||
'?categories=5063&per_page=1'; | '?categories=5063&per_page=1&_embed'; | ||
/* | /* | ||
| Line 109: | Line 93: | ||
if (!posts || posts.length === 0) { | if (!posts || posts.length === 0) { | ||
throw new Error('No articles found'); | throw new Error( | ||
'No Making a Difference articles found' | |||
); | |||
} | } | ||
| Line 139: | Line 125: | ||
dateElement.textContent = | dateElement.textContent = | ||
articleDate.toLocaleDateString( | articleDate.toLocaleDateString('en-US', { | ||
year: 'numeric', | |||
month: 'long', | |||
day: 'numeric' | |||
}); | |||
/* | /* | ||
* | * FEATURED IMAGE | ||
*/ | */ | ||
var | var imageUrl = ''; | ||
var media = null; | |||
var | |||
if ( | |||
post._embedded && | |||
post._embedded['wp:featuredmedia'] && | |||
post._embedded['wp:featuredmedia'].length > 0 | |||
) { | |||
media = post._embedded['wp:featuredmedia'][0]; | |||
} | } | ||
if (media) { | |||
var sizes = | |||
media.media_details && | |||
media.media_details.sizes | |||
? media.media_details.sizes | |||
: {}; | |||
/* | |||
* Prefer the largest available image. | |||
*/ | |||
if (sizes.full && sizes.full.source_url) { | |||
imageUrl = sizes.full.source_url; | |||
} else if ( | |||
sizes.large && | |||
sizes.large.source_url | |||
) { | |||
imageUrl = sizes.large.source_url; | |||
} else if ( | |||
sizes.medium_large && | |||
sizes.medium_large.source_url | |||
) { | |||
imageUrl = sizes.medium_large.source_url; | |||
} else if ( | |||
media.source_url | |||
) { | |||
imageUrl = media.source_url; | |||
} else if ( | |||
sizes.medium && | |||
sizes.medium.source_url | |||
) { | |||
imageUrl = sizes.medium.source_url; | |||
} | } | ||
} | } | ||
/* | /* | ||
* DISPLAY | * DISPLAY IMAGE | ||
*/ | */ | ||
if ( | if (imageUrl) { | ||
image.src = imageUrl; | |||
image.alt = post.title.rendered | |||
.replace(/<[^>]*>/g, ''); | |||
image.style.display = 'block'; | |||
image.onerror = function () { | |||
console.error( | |||
'Unable to load Making a Difference image:', | |||
imageUrl | |||
); | |||
} | image.style.display = 'none'; | ||
}; | |||
} | } | ||
/* | /* | ||
* SHOW | * SHOW CONTENT | ||
*/ | */ | ||
loading.style.display = 'none'; | loading.style.display = 'none'; | ||
content.style.display = ' | content.style.display = 'block'; | ||
}) | }) | ||
/* | |||
* ERROR HANDLING | |||
*/ | |||
.catch(function (error) { | .catch(function (error) { | ||
| Line 258: | Line 245: | ||
Displays the latest article from the UP website's | Displays the latest article from the UP website's | ||
Making a Difference category, including its publication | Making a Difference category, including its featured | ||
date, excerpt, article link | image, publication date, excerpt, and article link. | ||
=== Usage === | === Usage === | ||
Latest revision as of 07:03, 26 October 2026
UP Making a Difference Widget
Displays the latest article from the UP website's Making a Difference category, including its featured image, publication date, excerpt, and article link.
Usage
{{#widget:UPMakingADifference}}