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;
query_posts(
	array_merge(
		array('cat' => -66),
		$wp_query->query
	)
);

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!
    0saves
    Leave a comment below and continue the conversation, or subscribe to my RSS feed to get articles like this delivered automatically to your feed reader.
    • http://tweople.com/ website spokesperson

      Nice Jon Bishop! would closely help with my WP blogs featuring mostly videos. Keep it up!

    • Pingback: Your Questions About Online Games | NET Games

    • http://websiteactorlive.com/ website spokesperson

      Hey Jon Bishop! Great post, especially since I have over 20 WP blogs (most with video) so this will help a lot!

    • Josh

      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?

    • http://websitetalkingheads.com video spokesperson

      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?

      • http://www.jonbishop.com Jon Bishop

        It seems like this could be accomplished with a custom page template and a custom query that displayed links to all posts within the video category. Glad you liked the post.

    • http://wordpresshostinghq.com Mark

      Very nice thoughts information John. I like to do some test sometime in one of my site.

    • http://curtismchale.ca curtismchale

      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.

    • http://kitkonsult.se Kiper IT-konsult

      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…