go41

add latest video from a post in WordPress sidebar

von Joern am 9. Jan. 2015 | Keine Kommentare

I am going to add a video to the sidebar. This video is taken from the last post in a category you have to set.
The post actually is filtered for an oembed video, only this video will be displayed in a size you can set.
For testing I just do it on a theme demo site here:

(demo not there anymore!)
First of all you should have a post with an embeded video, WordPress allows you to embed a video by just inserting the URL in a single line in the post editor, like this:
http://www.youtube.com/watch?v=wLph9-ZkMX8
looking at the single post it should show you the video.
Let’s do it (sample should be to see here (demo not there anymore!) )
STOP: BACKUP YOUR FILES WE ARE GOING TO CHANGE, just in case…
We have to edit sidebar.php and add two lines to load another template called sidebarclip.php (we will create this file later)
Lines to add in sidebar.php: (in Arthemia under div id="sidebar" just above div id="sidebar-ads" )
<?php if(file_exists(TEMPLATEPATH . ‚/sidebarclip.php‘)) { // get the clip-script
include (TEMPLATEPATH . ‚/sidebarclip.php‘); } ?>
This code just looks for the following template file in your themes folder, It’s just a ‚manual widget‘ ;-)
Create a new file called sidebarclip.php with the following content
<div id="sidebar-video">
<?php $category_id = get_cat_ID( ‚Videos‘ ); // set your category name here, if you know the ID use $category_id = 6; ?>
<h3>Featured Video <a style="float: right;" href="<?php $category_link = get_category_link( $category_id ); echo $category_link; ?>" title="more Videos">more Videos</a></h3>
<?php
query_posts(‚posts_per_page=1&cat=$category_id‘);
if ( have_posts() ) : while ( have_posts() ) : the_post();?>
<?php if((function_exists(‚resizeMarkup‘)) && (function_exists(‚get_embedhtml‘))) {
$resizedvideo = resizeMarkup((get_embedhtml($postID)), array(
‚width’=>300,
‚height’=>250 ));
if($resizedvideo != “)
{
echo $resizedvideo;
} else { // if no Video
echo ’no Clip available‘;
}
} ?>
<p>Read the post:<br /><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></p>

<?php
endwhile;endif;
wp_reset_query();
?>
</div>
beeing here I can see something new in the sidebar already, but where is the video clip?

We still have to add some functions in functions.php of your theme folder, here the script: you should add it at the end of functions.php before the closing ‚?>‘

// the function to resize oembed videos
function resizeMarkup($markup, $dimensions)
{
$w = $dimensions[‚width‘];
$h = $dimensions[‚height‘];

$patterns = array();
$replacements = array();
if( !empty($w) )
{
$patterns[] = ‚/width="([0-9]+)"/‘;
$patterns[] = ‚/width:([0-9]+)/‘;

$replacements[] = ‚width="‘.$w.’"‘;
$replacements[] = ‚width:‘.$w;
}

if( !empty($h) )
{
$patterns[] = ‚/height="([0-9]+)"/‘;
$patterns[] = ‚/height:([0-9]+)/‘;

$replacements[] = ‚height="‘.$h.’"‘;
$replacements[] = ‚height:‘.$h;
}

$patterns[] = ‚/</param><embed/‘;
$patterns[] = ‚/allowscriptaccess="always"/‘;

$replacements[] = ‚</param><param name="wmode" value="transparent"></param><embed‘;
$replacements[] = ‚wmode="transparent" allowscriptaccess="always"‘;

return preg_replace($patterns, $replacements, $markup);
}

// get_embedhtml
function get_embedhtml($postID)
{
$custom_field_keys = get_post_custom_keys();
foreach ( $custom_field_keys as $key => $value ) {
$valuet = trim(substr($value, 1, 4));
if ( ‚oemb‘ == $valuet ) { // look if a oembed key exists
$oembkey = $value; //now set the the key we found as oembkey
$oembvalue = get_post_custom_values($oembkey); //get the value of oembkey as oembvalue
if (isset($oembvalue[0])) {
$embedhtml = ($oembvalue[0]);
return $embedhtml;
}
}
}

}

function add_transparent($oembvideo) {
$patterns = array();
$replacements = array();
$patterns[] = ‚/</param><embed/‘;
$patterns[] = ‚/allowscriptaccess="always"/‘;

$replacements[] = ‚</param><param name="wmode" value="transparent"></param><embed‘;
$replacements[] = ‚wmode="transparent" allowscriptaccess="always"‘;

return preg_replace($patterns, $replacements, $oembvideo);

return $oembvideo;
}
add_filter(‚embed_oembed_html‘, ‚add_transparent‘);
Works! Lucky we are, but still could apply some styling to the new div id="sidebar-video" to make it look nice.
This is done in style.css, I use:
#sidebar-video {

width:300px;

float:right;

border:1px solid #DDDDDD;

background:#FFFFFF;

padding:10px 9px;

margin: 0 0 10px;

}
Try it, having problems ask for help!
____________________
you find me on Google+, Twitter and Facebook

(von: Joern)

Hier noch 1 weitere Ergebnisse dieses Threads:

Re: add latest video from a post in WordPress sidebar :: Reply by Joern

16. Feb. 2011 (von: Joern)

An addition:Here we take an embeded video from a post in a certain category. The code above in sidebarclip.php loads by default only one latest post. You can however change:query_posts('posts_per_page=1&cat=$category_id');to load let's say 4 posts ... Weiterlesen →

Autor:

Du findest mich auch auf Twitter und Facebook!

Schreibe einen Kommentar

Pflichtfelder sind mit * markiert.


Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.

weitere forum Beiträge: