1. Code
  2. WordPress

Create a YouTube Section With WordPress

Read Time:6 minsLanguages:

So I was reading a few blogs that I read, as you do on a lazy afternoon, and I came across Ideate . I loved the theme so much, I wanted to re-create the little TV box they have that contains a YouTube video. In this tutorial, we'll have a look at how!


We'll assume that you have a live WordPress installation, whether it be local or hosted.
There are tutorials on running WordPress locally title="geeks are sexy on how to install WordPress locally under XP">here for Windows,
here for OS X

Step 1 - The Necessities

Alrighty. So apart from the obvious need of a WordPress installation, we're going
to need a few extra things too. I got a whole bunch of files you're gonna need to
have in your theme folder. Name a new folder in wp-content/themes 'youtubeFeature',
and put all this stuff into it.

Edit style.css if you want, though it's default wont hurt. Go to your wp-admin and
activate the theme!

We need a video post!

Now for the tutorials sake, we need to make a new post in wp-admin. So open up your
wp-admin, click write new post. We only want the content to be the youtube vid,
right? Go to your favorite YouTube video, and in the info section on the right
(right next to the video player itself) you should see the embed bit.

Grab that bit of code, and paste it straight into your wp-admin write new post content
area! Don't forget to make sure that you're editing in HTML mode not Visual:

. Very important! You need to change some parameters so that the
video fits our screen. On both occasions, change the '425' in the width parameters
to '250', and the '344' to '210'. Go down to the categories, and click '+ Add New
Category', and aptly name it 'Video':

Click Publish, and we're away!

Step 2 - HTML

We'll need some base HTML and basic WP info, right? We'll do that now, then we can
add the important WordPress code later on. This does contain some WordPress
code, but not the bits that show the YouTube vid!

Huh? Did you say that's a lot? Yeah... It's a bit I guess. But it's all basic stuff.
Link the stylesheet in, show the h1 a img (which happens to say IBLOG in
Arial), show a little tag, and close all the divs. Real easy stuff.

Step 3 - WordPress code

As I said before, we need 2 loops. One for the video section, the other for the
main loop. Easy done with WP_Query! Our first loop query will tell the loop to only
show the content of the most recent post under the category 'Video'. Replace the
tv-Section comment with:

You get the parameters in the query, right? Like I said, post from category name:
Video, only show 1 post. Simple. If you don't understand WP_Query, have a look back
at this tutorial for a more extensive look. Great! So if you'd added
the post correctly, with only the YouTube content as the content, it should look
something like this:

The second loop is a much simpler, basic loop. Just your Average Joe loop. It should
replace the second comment in index.php!

Now, without any CSS styling just yet, it should look something similar to this:

Step 4 - CSS

Here is where the magic happens!

To start off, we'll get some basic stuff out of the way.

This is just resetting the margins, setting standards for the body (giving it the
background of 'bg.png' and #998835, a pukey yellow colour!). Centers the whole
thing, and makes it a max of 800px.

Alright! now to style our TV!

The reason we're editing the object within the TV section is because no matter where
that object moves on the page, it's going to take it's padding and background with
it. So to save problems, giving the object such attributes fixes the background
behind the vid - no matter what content there is. It also assures us that if there
is any other content accidentally put in, it's displayed against the blank white
of the container so you can see it's there! (Or you could give the p a grey background
as a caption, who knows?)

Firstly, get it out of the way to the right. A given. Secondly, we give it our TV
image as the background! By making the width and height of the TV section the same
as what the YouTube video was set as, it constrains it to that small square - so
we know it will remain in the right place. The padding just displays the rest of
the TV. In my WYSIWYG editor, it's possible to view the padding (with the dotted
line!). So to give you and idea of what the very specific padding does I'll show
you a screen shot. The solid line cutting through the top half is the negative margin!

The negative top margin just shoves the whole thing a little higher up the page,
so that it's halfway between what is soon to be the white container div, and the
top of the body (pukey yellow!) Our TV should now look something like this:

Next up is the content and container fixing. the container needs to be white, while
the content div needs to be narrow with the small shadowed thing at the top. Easy

And finally, the rest of the post content! We've been paying so much attention to
our TV, that the regular loop feels insignificant! Let's give it some life with
some red and grey and floaty images!

And now it should look somewhat similar to this:

It look rather like our Photoshop mockup, and not unlike
the Ideate website's header... Yes I know it's missing
some patriotic flags!

Well done! You've put a Youtube Vid in the tube in a wordpress skin!

Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.