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.
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:
- Assign the post to your video category
- Paste your embed code into the content and your ‘embed’ custom field
- And Publish Your Post!