Youtube video in query
-
I have posted a standard youtube link on a page. It displays this specific video on a page.
Is there a way to call a specific youtube video from outside of WordPress, to play in WordPress site without having to create every single videos in a different posts?
-
I’m sorry to say, but I’m not following what you want to do. But then again, that’s not a new thing for me sometimes. ??
What do you want to end up with? If I’m guesing right you’re wanting to add multiple videos on a single page? Or is that completely wrong??
Here’s what I’m looking to do:
https://mor10.com/simple-video-embedding-with-custom-fields-in-wordpress-youtube/I have created a custom field named: video_url in the post.
Where do I put that php code stated in the above link? Inside the post?
I want a link that when we hv: https://www.mywebsite.com/?video_url=myyoutubeid
Then it will display that particular youtube video in the website.
Where should the code be posted?There’s a few approaches you could use, I suggest you create a custom page template. Then you would add a new generic page post_type based on this template.
Instead of getting the video code from post meta as in your linked article, you would get it from
$_GET['video_url']
. Your link would need to also include the page slug you assigned when you added the generic video page. Assuming you use pretty permalinks, something like this:example.com/view-video/?video_id=youtubeid
If you really must have an URL like you illustrated without the page slug, you would need to add a rewrite rule to open that page if the video_id query var exists.
So, how is that done in custom page template?
Assuming all videos are from YouTube, the embed code can be part of the template, so only the ID needs to be passed in the URL. Create a new template file in your theme folder that looks something like this:
<?php /** * Template Name: YouTube Viewer * * Displays YouTube video when ID passed as 'video_id': * https://example.com/my-page-slug/?video_id=ell0SiTZyX8 */ get_header(); ?> <div id="container"> <div id="content"> <iframe width="640" height="360" src="https://www.youtube.com/embed/<?php echo sanitize_text_field( $_GET['video_id'] ); ?>?feature=player_detailpage" frameborder="0" allowfullscreen> </iframe> </div><!-- #content --> </div><!-- #container --> <?php get_sidebar(); get_footer();
Add a new page, selecting “YouTube Viewer” as the template. Assign a title to the page to establish a permalink for the page. No content needs to be entered. Add something like “?video_id=ell0SiTZyX8” to the permalink. The video will be seen on the page with your theme’s header, footer, etc.
The template is untested but it at least illustrates the concept.
Perfect!!! Thanks a million.
Another thing about video. Any trick to make another page like Youtube style, that look something like this dynamically? (with thumbnail, video title, description, duration in the playlist and channel)
Basically the main video on the left, the list of videos on the right.
You’d have to have all that data available somehow. It could be managed with posts or a custom post type along with custom fields. Then you would add more code to do a custom query for the video posts that have the data. The query should at least exclude the current main video, but you could get more elaborate by querying for videos with the same category or tags, etc. Then you run a loop to display all the data in a list, much like the default WP home page shows posts, except it’s narrow and off to one side.
As for making two columns, at the most basic, the left side is in one div, the right in another, and CSS is used to place the columns. Without CSS, the content would simply be one long column. Depending on your needs, there’s more elaborate column setups. Plenty of examples on the ‘net.
If you don’t already have such video data in posts, create a few for testing purposes. Don’t go crazy yet with content, you may decide there’s a better way to organize the data as you get into it. Regardless of how it’s organized, there’s a way to make it work, but one way may be better than others.
Now that you have a basic template to work from, you’re going to need to do some of the modifications yourself. I’m not abandoning you, but there’s a limit to how much code I do for people here. My primary goal here is to help people work these things out for themselves.
To start with, once you have some sample posts, add a query and loop below the iframe. If all works out, the posts will appear below the video. We’ll deal with columns later, the main thing is to get content on the page somewhere. Work from the information and examples at Class Reference/WP Query.
The loop is really just copy/paste (don’t forget to add the closing
?>
to get us back to HTML mode). The only real challenge is determining the proper arguments for the new WP_Query object. Exactly what those are depends on how you’ve organized the posts containing the video data. You’ll only see the post titles to begin with. We’ll add more content later.Also edit your wp-config.php file to define WP_DEBUG as
true
. This way you’ll be informed what and where any mistakes have occurred. Give all of this a shot, then report back. If you’re successful, while you’re waiting for my reply, work on getting the content into two columns. It’s all HTML and CSS, no PHP coding involved. For now, place the CSS in a<style>
block right after theget_header()
line.When you report back, include info on how you’ve managed the video data. Outputting that data is the next step. Good luck!
woowwww….looks complicated
Thanks.will try this out. Thanks.
It always sounds complicated when it’s explained in conversation. Just focus on one small portion at a time, you’ll get it all eventually. Then you will see it wasn’t so bad, and you learned a lot along the way so maybe you won’t need to ask for help next time.
I don’t mind helping, but I do have a certain style. It may make you uncomfortable, but you’ll be the better for it ??
- The topic ‘Youtube video in query’ is closed to new replies.