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”.
-
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:
-
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.

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.
-
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 '
-
<li id="recent-videos" class="widget widget_recent_videos">
-
<h3>Videos</h3>
-
';
-
?>
-
<ul>
-
<li style="text-align: center;"><strong><a href="<?=get_permalink(); ?>">ID); ?></a></strong>
-
<ul>
-
<li>Next Video: <a href="<?=get_permalink(); ?>">ID); ?></a></li>
-
</ul>
-
</li>
-
</ul>
-
</li>
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:
- Assign the post to your video category
- Paste your embed code into the content and your ‘embed’ custom field
- And Publish Your Post!







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.