Güncelleme 27 temmuz 2010

Kod yenilendi sistem yazıdaki etiketlere göre benzer yazıları buluyordu etiketler uyuşmadığında yada yazıda etiket olmadığında "benzer yazı bulanamadı" yazısı görünüyordu yeni kod ile bu uyarı yerine yazının kategorisindeki diğer yazılar listeleniyor

---------------------------------------------------------------------


Bu sıralar resimli benzer yazılara talep olduğunu görüyorum aslında bir çok eklenti vardır ama bana manuel daha kullanışlı geliyor paylaşayım umarım faydası olur

Genelde klasik “benzer yazılar” uygulamaları yazının bulunduğu kategorideki diğer yazıları görüntüler, bizim kullanacağımız kodlar ise yazıya eklenen etiketlere göre bir listeleme yapıyor. Bu kod ile yazıların resimi, özeti ve başlığı görünüyor, bana kalırsa böyle bir uygulama hem daha mantıklı hem de okur açısından daha verimli.





Temanızın functions.php dosyasına aşağıdaki kodları ekleyiz;


function get_thumb($postid=0, $size='full') {
if ($postid<1) 
$postid = get_the_ID();
$thumb = get_post_meta($postid, "resim", TRUE);
if ($thumb != null or $thumb != '') {
echo $thumb; 
}
elseif ($images = get_children(array(
'post_parent' => $postid,
'post_type' => 'attachment',
'numberposts' => '1',
'post_mime_type' => 'image', )))
foreach($images as $image) {
$thumbnail=wp_get_attachment_image_src($image->ID, $size);
?>
<?php echo $thumbnail[0]; ?>
<?php }
else {
echo get_bloginfo ( 'stylesheet_directory' );
echo '/images/resim-yok.gif';
}
}
Not: resimlerde özel alan kullanıyorsanız kod içindeki "resim" bölümünü kendinize göre değiştirin

kod sonundaki echo images klasör ismini temanızın resimlerinin tutulduğu klasöre göre değiştirin ve resim olmayan yazılarda standar resim fgörünmesi için paint ile resim-yok.gif oluşturup klasöre atın


---------------------------------------------------------


Single.php dosyasında comments_template kodunun üstüne aşağıdaki kodları ekleyiniz


<div id="sm">



<h2 class="bz"> Benzer Yazılar </h2>
<?php  
$backup = $post;  
$tags = wp_get_post_tags($post->ID);
$tagIDs = array();
if ($tags) {
$tagcount = count($tags);
for ($i = 0; $i < $tagcount; $i++) {
$tagIDs[$i] = $tags[$i]->term_id;
}
$args=array(
'tag__in' => $tagIDs,
'post__not_in' => array($post->ID),
'showposts'=>4,  // 3 yazi gosterilir
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>

<div class="b">
<div class="cp">

<img src="<?php get_thumb($post->ID, 'full'); ?>" width="120" height="80" alt="<?php the_title(); ?>" />
<h3><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
</div>
</div>

<?php endwhile;
} else { ?>
<?php
$this_post = $post;
$category = get_the_category(); $category = $category[0]; $category = $category->cat_ID;
$posts = get_posts('numberposts=4&offset=0&orderby=post_date&order=DESC&category='.$category);
$count = 0;
foreach ( $posts as $post ) {
if ( $post->ID == $this_post->ID || $count == 5) {
unset($posts[$count]);
}else{
$count ++;
}
}
?>
<?php if ( $posts ) : ?>

<?php function getWords($text, $limit) {
$array = explode(" ", $text, $limit +1);
if(count($array) > $limit) {
unset($array[$limit]);
}
return implode(" ", $array); }
?>
<?php foreach ( $posts as $post ) : ?>
<?php $mycontent = strip_tags($post->post_content);
$excerpt = getWords($mycontent, 15);
$a_title = $excerpt . "..."; ?>

<div class="b">
<div class="cp">

<img src="<?php get_thumb($post->ID, 'full'); ?>" width="120" height="80" alt="<?php the_title(); ?>" />
<h3 class="bz"><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
</div>
</div>




<?php endforeach // $posts as $post ?>

<?php endif // $posts ?>
<?php
$post = $this_post;
unset($this_post);
?>
<?php }
  }
  $post = $backup;  
  wp_reset_query(); 
?>
<div class="clear"></div>

 </div>


Temanızın css dosyasına aşağıdaki kodları ekleyiniz


#sm {background:#ececec;border:1px solid #ababab;height:160px;margin:10px 0px 10px 0px;float:left;}.b h3 {font-size:11px;margin:0 0 2px 3px;}

.b {height:130px;float:left;overflow:hidden;margin:10px 0px 0px 0px;padding-left:10px;width:135px;}.cp {position:relative;}

.b img {height:80px;width:120px;background:#dbdbdb;padding:4px;border-right:1px solid #ababab;border-bottom:1px solid #ababab;}

h3.bz {background:#dbdbdb;font-size:12px;margin:0px 0px 0px 0px;}


Her şey yolunda giderse;