Categories
Blogging How To Themes WordPress

Creating A Video Section On Your WordPress Blog

A while back, I started experimenting with posting videos on my blog. At the time, the easiest way to organize them was to create a dedicated “Video” category. I’d tag any video post with it, exclude them from the homepage and pull the latest one into the sidebar using a bit of custom code.

It worked, but it wasn’t ideal.

Over time, as WordPress evolved and custom post types became more common, I shifted to using CPTs to separate video content from blog posts entirely. This gave me more control over how videos were displayed, archived and managed across my site.

In this updated guide, I’ll show you both approaches, starting with the original method using categories, then moving into a modern CPT-based workflow. Whether you’re just starting out or want to upgrade how videos are handled on your site, you’ll find a solution that fits.

A Simple Category-Based Setup (Legacy Method)

If you’re not ready to dive into custom post types, you can still create a basic video section using WordPress categories. This was my original approach and it still works for simple use cases.

Here’s how:

1. Create a Video Category
Go to Posts > Categories and add a new category called “Video.” Tag all of your video-related posts with this category.

2. Exclude Video Posts from the Blog Feed
To keep video posts out of your main blog loop, add this snippet before the loop in your theme’s index.php or via a plugin like Code Snippets:

if (is_home()) {
  query_posts(array_merge(array('cat' => '-X'), $wp_query->query));
}

Replace -X with the negative ID of your “Video” category. You can find the category ID by hovering over the category name in the admin panel.

3. Display the Latest Video in the Sidebar
You can use a custom field called embed to store the video embed code and pull it into a widgetized area. Here’s a simplified example:

$args = array(
  'category_name' => 'video',
  'posts_per_page' => 1
);
$video_query = new WP_Query($args);
if ($video_query->have_posts()) {
  while ($video_query->have_posts()) {
    $video_query->the_post();
    $embed = get_post_meta(get_the_ID(), 'embed', true);
    echo '<div class="sidebar-video">';
    echo $embed; // Render the embed code
    echo '<p><a href="' . get_permalink() . '">Watch: ' . get_the_title() . '</a></p>';
    echo '</div>';
  }
  wp_reset_postdata();
}

This lets you show the most recent video post and link to its full page. You could expand on this by adding a “See all videos” link to the category archive page.

This setup is lightweight, easy to manage and doesn’t require any plugins. But as your site grows, switching to custom post types offers a lot more flexibility.

Why Use a Custom Post Type for Videos

While assigning a “Video” category to posts can work, using a custom post type (CPT) offers better separation and organization. CPTs allow you to manage video content independently from blog posts, providing flexibility in design and functionality. This approach is especially beneficial if you plan to have a dedicated video archive or need custom fields specific to videos.

Creating a Custom Post Type for Videos

You can register a CPT manually by adding code to your theme’s functions.php file or by using a plugin. Here’s how to do it manually:

function create_video_post_type() {
  register_post_type('video',
    array(
      'labels' => array(
        'name' => __('Videos'),
        'singular_name' => __('Video')
      ),
      'public' => true,
      'has_archive' => true,
      'supports' => array('title', 'editor', 'thumbnail'),
      'rewrite' => array('slug' => 'videos'),
    )
  );
}
add_action('init', 'create_video_post_type');

This code registers a new post type called “video” with support for titles, editors and thumbnails. It also creates an archive page accessible at yourdomain.com/videos.

Displaying Videos on Your Site

1. Video Archive Page

WordPress automatically generates an archive page for your CPT at yourdomain.com/videos. To customize its appearance, create a template file named archive-video.php in your theme directory. This file will control the layout and design of your video archive.

2. Single Video Page

Similarly, you can create a single-video.php template to customize the display of individual video posts. This allows you to design a unique layout for video content, separate from standard blog posts.

3. Embedding Videos

Within each video post, you can embed videos using the WordPress block editor. Simply add a “Video” block and insert your video URL or upload a video file. For more control, consider using custom fields to store video URLs or embed codes.

Enhancing Video Management with Plugins

For those who prefer not to handle code, several plugins can simplify the process:

1. Custom Post Type UI

This plugin provides an interface to create and manage custom post types and taxonomies without writing code.

2. Advanced Custom Fields (ACF)

ACF allows you to add custom fields to your CPTs, such as video URLs, durations or descriptions. This enhances the flexibility and organization of your video content.

3. Video Gallery Plugins

Plugins like “Video Gallery – YouTube Gallery” or “All-in-One Video Gallery” offer advanced features for displaying video content, including responsive galleries, lightbox effects and category filtering.

Conclusion

By leveraging custom post types and appropriate plugins, you can create a dedicated and organized video section on your WordPress site. This approach provides flexibility in design and functionality, enhancing the user experience for your audience.

If you found this post helpful, please consider leaving a comment or sharing it with others.

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 *