-
AuthorPosts
-
February 1, 2022 at 7:20 am #1338155
I’m adding videos to my YouTube channel daily. I have two separate Playlists: Daily Word and Courses. On two separate web pages I’m manually embedding the newest video URL from the “Daily Word” playlist. As you can imagine, I would like to automate this work process.
I would like to always show the latest video from a playlist. So, only show one video on the pages, but always the most recent of the Daily Word playlist. When we upload a new Daily Word video on YouTube, that latest video has to be shown on these two web pages (see below).
Example code:
function requestLastVideo(playlistId, callback, pageToken) { var requestOptions = { playlistId: playlistId, part: 'snippet', maxResults: 50 }; if (pageToken) { requestOptions.pageToken = pageToken; } var request = gapi.client.youtube.playlistItems.list(requestOptions); request.execute(function(response) { var nextPageToken = response.result.nextPageToken; if (nextPageToken) { // we didn't reach the last page yet, fetch next one requestLastVideo(playlistId, callback, nextPageToken); return; }
And you’d call this like so:
var playlistItems = response.result.items; if (playlistItems) { var lastPlaylistItem = playlistItems[playlistItems.length - 1]; callback(lastPlaylistItem.snippet); } else { alert('There are no videos'); } }); }
You can play around with requestOptions.part to reduce the footprint of your call. With this parameter you can control which fields are present in the response. You can find more information detailling possible values in the YouTube API docs for this call.
Two questions:
1. Based on what I’m trying to achieve, will this work? Do you have any recommended changes?
2. Naïve question, what HTML would I use on our page to render the video link?
I hope I explained this properly. Let me know if you need any additional information. Thanks in advance for any guidance you can offer!!!
February 1, 2022 at 11:38 am #1338194Hey Julio,
Thank you for the inquiry.
You would have to pass the result to the Youtube Player API to dynamically load the video player using the data from the very last video.
// https://developers.google.com/youtube/iframe_api_reference#Loading_a_Video_Player
This is the structure of the resource.
// https://developers.google.com/youtube/v3/docs/playlistItems#resource
Unfortunately, this kind of modification is beyond the scope of support. You may need to hire a freelance developer or contact our partner, Codeable.
// https://kriesi.at/contact/customization
Best regards,
IsmaelFebruary 1, 2022 at 1:48 pm #1338224btw. there are a lot of specialized Plugins (even free ones ) for that purpose. I use for a friend that one: youtube embed plus.
You can show by shortcodes different channels with different options etc.
See: https://kurzundschluss.com/February 1, 2022 at 2:59 pm #1338242February 1, 2022 at 4:31 pm #1338263@ismael & @guenni007 I cannot thank you enough for sharing your insights and assistance for my request.
I will definitely look into both approaches.
Fact or Fiction? I have other WordPress users tell me not to use too many plugins because they can conflict with one another and your site, etc. Now, I don’t have enough experience/knowledge on this to share my opinion. When should you use Plug-Ins vs coding (PHP, JavaScript, etc.)? Thoughts?
Thank you again for your help. I’m learning so very much…thanks to this forum and the Enfold Template.
Best Regards,
JulioFebruary 2, 2022 at 4:44 am #1338348Hi Julio,
There’s not really any difference between code added to your site in a plugin, and code added in for example functions.php in your theme, they should work the same from both places. The problem with plugins can sometimes be that they are bloated, and can conflict with each other, or are not maintained on a regular basis. The good thing about plugins is that they can be easily deactivated, so that you can quickly debug if you should run into problems.
Best regards,
RikardFebruary 2, 2022 at 6:19 am #1338380Thank you @Rikard. I fully appreciate your help!!
For now, I’m following up on @guenni007’s suggestion to determine if the YouTube plug-in he suggested can meet my needs. If not, I’ll pursue the coding option that @ismael recommended.
Feel free to close this for now.
Thanks again to everyone who offered their assistance.
Best Regards,
Julio- This reply was modified 2 years, 9 months ago by Jules.
February 2, 2022 at 8:27 am #1338394Hi,
Thanks for the update. I’ll go ahead and close this thread for now then, please open a new thread if you should have any further questions or problems.
Best regards,
Rikard -
AuthorPosts
- The topic ‘Get Latest Video in YouTube Playlist’ is closed to new replies.