Penguination Project


Editing default wordpress theme tutorial #2

codeHello, we will continue default wordpress theme editing tutorial, in tutorial #1 we fixed default wordpress theme to enable sidebar showing in all posts and categories, and today you will learn how to add most recent video in your right sidebar , or somwhere else inside of your wordpress blog.  We will try to explain our best PHP code parts you should insert or edit, and we hope this will be the  good start in creating your own wordpress themes.

Please take a look at our example, of what are we going to create : Here

Let’s start coding :

First create category “videos” and post at least one video from youtube or any other flash video.

Now we will need “video” category id before we will start PHP coding of our sidebar, login to your wp-admin, click on posts, categories , you will see categories list, click on created video category.

In your browser address field you will see something like that : http://penguinationproject.com/preview/wp-admin/categories.php?action=edit&cat_ID=3

Category id number  is 3 (cat_ID=3).

Now when we know our category id , let’s start coding.

Click on appearance in your left admin menu, click edit , you will see files list on right menu. Click on sidebar.php

Find html code  <div id=”sidebar” role=”complementary”>

After this code and before <ul> tag starts insert PHP code below , we commented inside of code where you should add your category id number.

<?php if ( have_posts() ) : ?>

// take a look at line below, &cat=3 , change 3 to your category id.

<?php $my_query = new WP_Query('showposts=1&cat=3');

while ($my_query->have_posts()) : $my_query->the_post();
  $do_not_duplicate = $post->ID;?>

<?php
$id =$post->ID;
$the_content =
$wpdb->get_var("SELECT post_content FROM $wpdb->posts WHERE ID = $id ");
$pattern = '!<embed.*?src="(.*?)"!';

preg_match_all($pattern, $the_content, $matches);
$obj_src = $matches['1'][0];
?>

<?php if($obj_src != '') 

{ 

echo '<b>Latest Video</b><br><br><object width="160" height="120">
<param name="movie" value="'.$obj_src.'"></param>
<param name="allowFullScreen" value="true"></param>
<param name="allowscriptaccess" value="always"></param>
<embed src="'.$obj_src.'" type="application/x-shockwave-flash"
allowscriptaccess="always" allowfullscreen="true" width="160" height="120"></embed>
</object><br><br>'; 

}

?>
 <?php endwhile; ?>

<?php endif; ?>

Now save the file , go to your wordpress blog’s  homepage , You will see most recent video on your blogs sidebar .

From now once you are adding video in “video” category , it will automatically appear on your sidebar.

We will continue hacking default wordpress theme soon. And we wish you all the best ! Please leve comments.

Good luck.

Post Metadata

Date
June 14th, 2009

Author
admin


1 Trackbacks & Pingbacks

  1. June 14, 2009 6:07 pm

    Editing default wordpress theme tutorial | Penguination Project | bllogger :

1 Comments

  1. nice stuff i was looking for this thanks for sharing


Leave a Reply