Jump to content

Widget:UPDateOnline: Difference between revisions

From Iskomunidad
No edit summary
No edit summary
 
(8 intermediate revisions by the same user not shown)
Line 1: Line 1:
<includeonly>
<includeonly>
<div class="your-news-class" id="update-online-news">


     <div id="update-online-loading">
<div class="update-news">
 
     <div class="update-online-loading">
         Loading latest news...
         Loading latest news...
     </div>
     </div>


     <div id="update-online-content" style="display:none;">
     <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-online-date" id="update-online-date"></div>
            <div class="update-online-date"></div>


        <h2 class="update-online-title" id="update-online-title"></h2>
            <h2 class="update-online-title"></h2>


        <div class="update-online-excerpt" id="update-online-excerpt"></div>
            <div class="update-online-excerpt"></div>


        <a class="update-online-link"
            <a class="update-online-link"
          id="update-online-link"
              href=""
          href=""
              target="_blank"
          target="_blank"
              rel="noopener noreferrer">
          rel="noopener noreferrer">
                Read more →
            Read more →
            </a>
         </a>
 
         </div>


     </div>
     </div>


</div>
</div>


<script>
<script>
(function () {
(function () {


     fetch(
     /*
         'https://upd.edu.ph/wp-json/wp/v2/posts?categories=4&per_page=1'
    * --------------------------------
     )
    * Find THIS widget instance
    * --------------------------------
    */
 
    var widgets =
        document.querySelectorAll('.update-news');
 
    var widget =
        widgets[widgets.length - 1];
 
    if (!widget) {
        return;
    }
 
 
    /*
    * --------------------------------
    * Find elements inside this widget
    * --------------------------------
    */
 
    var loading =
        widget.querySelector(
            '.update-online-loading'
        );
 
    var content =
        widget.querySelector(
            '.update-online-content'
        );
 
    var image =
        widget.querySelector(
            '.update-online-image'
        );
 
    var title =
        widget.querySelector(
            '.update-online-title'
        );
 
    var excerpt =
        widget.querySelector(
            '.update-online-excerpt'
        );
 
    var link =
        widget.querySelector(
            '.update-online-link'
        );
 
    var dateElement =
        widget.querySelector(
            '.update-online-date'
        );
 
 
    /*
    * --------------------------------
    * UPDate Online API
    * --------------------------------
    */
 
    var api =
         'https://upd.edu.ph/wp-json/wp/v2/posts' +
        '?categories=4&per_page=1';
 
 
     /*
    * --------------------------------
    * Retrieve latest article
    * --------------------------------
    */
 
    fetch(api)
 
        .then(function (response) {
 
            if (!response.ok) {
                throw new Error(
                    'Unable to retrieve UPDate Online'
                );
            }
 
            return response.json();
 
        })
 


    .then(function (response) {
        .then(function (posts) {
        return response.json();
    })


    .then(function (posts) {
            if (!posts || posts.length === 0) {
                throw new Error(
                    'No UPDate Online article found'
                );
            }


        if (!posts || !posts.length) {
            throw new Error('No article found');
        }


        var post = posts[0];
            var post =
                posts[0];


        /* Title */
        document.getElementById('update-online-title').innerHTML =
            post.title.rendered;


        /* Excerpt */
            /*
        document.getElementById('update-online-excerpt').innerHTML =
            * --------------------------------
            post.excerpt.rendered;
            * TITLE
            * --------------------------------
            */


        /* Article URL */
            title.innerHTML =
        document.getElementById('update-online-link').href =
                post.title.rendered;
            post.link;


        /* Date */
        var date = new Date(post.date);


        document.getElementById('update-online-date').textContent =
            /*
            date.toLocaleDateString('en-US', {
            * --------------------------------
                year: 'numeric',
            * EXCERPT
                month: 'long',
            * --------------------------------
                day: 'numeric'
            */
            });


        /* Show article */
            excerpt.innerHTML =
        document.getElementById('update-online-loading').style.display =
                post.excerpt.rendered;
            'none';


        document.getElementById('update-online-content').style.display =
            'block';


    })
            /*
            * --------------------------------
            * ARTICLE LINK
            * --------------------------------
            */


    .catch(function (error) {
            link.href =
                post.link;


        document.getElementById('update-online-loading').innerHTML =
            'Unable to load the latest UPDate Online article.';


        console.error(error);
            /*
            * --------------------------------
            * DATE
            * --------------------------------
            */


    });
            var articleDate =
                new Date(post.date);
 
 
            dateElement.textContent =
                articleDate.toLocaleDateString(
                    'en-US',
                    {
                        year: 'numeric',
                        month: 'long',
                        day: 'numeric'
                    }
                );
 
 
            /*
            * --------------------------------
            * ARTICLE CONTENT
            * --------------------------------
            */
 
            var article =
                document.createElement('div');
 
 
            article.innerHTML =
                post.content.rendered;
 
 
            /*
            * --------------------------------
            * FIND FIRST PARAGRAPH
            * --------------------------------
            */
 
            var paragraphs =
                article.querySelectorAll('p');
 
 
            var firstParagraph =
                null;
 
 
            for (
                var i = 0;
                i < paragraphs.length;
                i++
            ) {
 
                if (
                    paragraphs[i]
                        .textContent
                        .trim() !== ''
                ) {
 
                    firstParagraph =
                        paragraphs[i];
 
                    break;
                }
 
            }
 
 
            /*
            * --------------------------------
            * FIND FIRST IMAGE AFTER
            * FIRST PARAGRAPH
            * --------------------------------
            */
 
            var selectedImage =
                null;
 
 
            if (firstParagraph) {
 
                var images =
                    article.querySelectorAll('img');
 
 
                for (
                    var j = 0;
                    j < images.length;
                    j++
                ) {
 
                    var position =
                        firstParagraph.compareDocumentPosition(
                            images[j]
                        );
 
 
                    if (
                        position &
                        Node.DOCUMENT_POSITION_FOLLOWING
                    ) {
 
                        selectedImage =
                            images[j];
 
                        break;
                    }
 
                }
 
            }
 
 
            /*
            * --------------------------------
            * DISPLAY IMAGE
            * --------------------------------
            */
 
            if (selectedImage) {
 
                var imageUrl =
                    selectedImage.getAttribute(
                        'src'
                    );
 
 
                if (!imageUrl) {
 
                    imageUrl =
                        selectedImage.getAttribute(
                            'data-src'
                        );
 
                }
 
 
                if (!imageUrl) {
 
                    imageUrl =
                        selectedImage.getAttribute(
                            'data-lazy-src'
                        );
 
                }
 
 
                if (imageUrl) {
 
                    image.src =
                        imageUrl;
 
                    image.alt =
                        selectedImage.getAttribute(
                            'alt'
                        ) ||
                        post.title.rendered;
 
                    image.style.display =
                        'block';
 
                }
 
            }
 
 
            /*
            * --------------------------------
            * SHOW CONTENT
            * --------------------------------
            */
 
            loading.style.display =
                'none';
 
            content.style.display =
                'block';
 
        })
 
 
        /*
        * --------------------------------
        * ERROR HANDLING
        * --------------------------------
        */
 
        .catch(function (error) {
 
            console.error(
                'UPDate Online Widget:',
                error
            );
 
            loading.textContent =
                'Unable to load the latest UPDate Online article.';
 
        });


})();
})();
</script>
</script>
</includeonly>
</includeonly>
<noinclude>
== UPDate Online Widget ==
Displays the latest UPDate Online article
and the first image appearing after the
opening paragraph.
=== Usage ===
<pre>
{{#widget:UPDateOnline}}
</pre>
</noinclude>

Latest revision as of 00:17, 7 October 2026



UPDate Online Widget

Displays the latest UPDate Online article and the first image appearing after the opening paragraph.

Usage

{{#widget:UPDateOnline}}