Add a YTPlayer background video on WordPress post category page

I’m writing this post because of the many questions I received on how could be implemented the YTPlayer in a category page of WordPress.

Adding it to the home page (when the home page is the recent post list) and to the front page (the static page that has been set as home in WP)  is quite simple; just go to the settings page of the component and fill all the fields you need; the background video will be there!

Adding it to a generic page or post is as simple as for the homepage; on the post/page editor there’s a button on the top bar of the TinyMCE  that opens a short-code composer to let you inject easily a background video into the post or the page. In the truth you can also use the short-code composer to create a simple light and clean video player in the content itself.


But what about if you want to run a background video on a category list page or on a tag list page or on the search result page?

At the time the plugin has not implemented any way to add a background video on that pages but you can overcome this lack by adding some code to your function.php file in your theme.

WordPress expose Conditional Tags to let us add specific content depending on the page type. Using those conditionals you can add your own function that inject the YTPlayer into any of the page you can’t otherwise reach. You should add this function to the “wp-footer” action.

Below a simple example to add the YTPlayer background video both on the archive page for the Category with ID 9 and on the search result page:

The above code is just an example and could probably be written better :-). It’s here just to give you a hint on how you can solve this kind of problems.