Blogging How To Themes WordPress

Creating A Video Section On Your WordPress Blog

Awhile back I decided to start posting videos on my blog. Once I had a few videos up I began to rethink my approach. Ultimately I wanted to prevent videos from showing up where normal posts are displayed and instead display my most recent video in the sidebar.

To do this, I would need to:

  • Suppress all video posts from being displayed with other posts
  • Create an archive of all video posts
  • Resize and place my most recent video in the sidebar

The first step was pretty easy. I just created a new category called “Video Post” and assigned it to all of my video posts. This would become a fairly intuitive list of videos people could access at their will.

Next I wanted to remove all video posts from being displayed among other posts on the home page. However, I still wanted my videos to be displayed in RSS feeds as well as archives and search pages. To do this, I just modified the default WordPress query used to pull the most recent posts by placing the following code before “The Loop”.

//Hide a category from the home page
global $wp_query;
		array('cat' => -66),

Be sure to change ‘-66’ to the id of whatever category you are trying to hide. How do I find the category IDs?

Also, if you are not sure where the WordPress Loop begins, it is usually located inside of index.php in your theme folder and starts with:

//Beginning of the WordPress Loop located in index.php of theme
if ( have_posts() ) : while ( have_posts() ) : the_post();

The last step is to display my most recent video in the sidebar. To do this I decided my best course of action was to save the embed code of my videos into a custom field as well as within the content of the post. This would allow me to easily pull the embed code and I would still be able to add text along with my video within the content.

Create custom field for embed code

In the example below I used the custom field ’embed’ in my posts. Just paste this code in your sidebar where you would like your video to be displayed.

//Display resized most recent video along with link to previous video
ID, $key, true);
		if ($custom_field) {
			// Set values
			$width = 'width="'.$width.'"';
			$height = 'height="'.$height.'"';
			$custom_field = preg_replace( '/width="[^"]+"/' , $width , $custom_field );
			$custom_field = preg_replace( '/height="[^"]+"/' , $height , $custom_field );
			echo '
  • Videos

    '; ?>
  • You will notice I also included a link to my last video however you can remove this or replace it with a link to your video archive. This code should work with most video embed codes. Be sure to change the width and height so everything fits nicely into your sidebar.

    And that’s it. Now all you need to do when adding a new video post is:

    1. Assign the post to your video category
    2. Paste your embed code into the content and your ’embed’ custom field
    3. And Publish Your Post!

    9 replies on “Creating A Video Section On Your WordPress Blog”

    Great post! I am suprised there is no comments!

    I am working on something similar where the video posts will be fetched to a playlist widget so that when you click on the playlist item you will get forwarded to the actual video post page.
    And as an extra feature I hope to sync youtube comments with wp comments so that you get the full comment stream at both places…

    A better way of excluding a category is to use the ‘category_name’ combined with the category slug. If you move a site then the category number in the DB may change while the slug will stay the same. Should save you work in the long run.

    Interesting. This is actually helpful for something I was trying to figure out with the videos that we place on blogs. Our videos have an added issue of maintain the alpha channel using flash video but your code seems to be great. I wonder though if this method would also be more effective if you wanted to employ a video sitemap for indexing purposes?

    Hey Jon, just came across your site and this post which looks like exactly what I need, but it doesn’t seem to be working for me. Not sure if the code up above that you’re saying should be added to the sidebar is complete, but there’s no opening or closing PHP tags, it ends on an tag that’s closing , but there’s no above this. I’m also not seeing anything in the code specifying to grab the embed custom field, but I’m not a PHP guy, just HTML and CSS. Any help?

    Leave a Reply

    Your email address will not be published. Required fields are marked *