10 kullanışlı wordpress döngü yöntemi

Haziran 15, 2009 | In: Anasayfa, Php, Wordpress

Döngüler wordpress bloglarının önemli bir kısmını oluşturmaktadır. Döngüler wordpress veri tabanından bilgileri almakta ve blogunuzda yayınlamaktadır. Döngü hackleri güçlü fonksiyonlardan oluşur ve ortaya inanılmaz güzellikte sonuçlar çıkarırlar.

İki tarih arasındaki yayınlar

Sorun : query_post döngüsü kolaylıkla mesajların tarih bilgilerini alabilmektedir. Ancak ne yazik ki iki tarih arasındaki mesajları sayfalarımızda gösteremekteyiz.

Çözüm : iki tarih arasındaki mesajları almak istediginiz sayfaya aşağıdaki kodu

  1. <?php
  2. function filter_where($where = ) {
  3. $where .= “ AND post_date >= ’2009-03-17′ AND post_date <= ’2009-05-03′”;
  4. return $where;
  5. }
  6. add_filter(‘posts_where’, ‘filter_where’);
  7. query_posts($query_string);
  8. while (have_posts()) :
  9. the_post();
  10. the_content();
  11. endwhile;
  12. ?>

Anasayfada yazıların yinelenmesine izin vermeden fazladan mesaj yayınlamak

Sorun : Özellikle magazin temalarında gördüğümüz anasayfada fazladan bir kaç yazıyı slide show gibi yöntemlerle sunulmaktadır. Bu özelliği kullanmak istediğimizde bu bölümde yayınlayacağımız mesajların site içeriğinde tekrar yayınlanması durumu yani çift kayıt ortaya çıkmaktadır.  Bu kod parçası bu duruma engel niteligini taşıyor.

Çözüm :  1. Biz burada 8 adet yazıyı görüntüleyeceğiz.

  1. <?php
  2. query_posts(’showposts=8′);
  3. $ids = array();
  4. while (have_posts()) : the_post();
  5. $ids[] = get_the_ID();
  6. the_title();
  7. the_content();
  8. endwhile;
  9. ?>

2.  Yazının geri kalanı için bu kodu kullanınız.

  1. <?php
  2. query_posts(array(‘post__not_in’ => $ids));
  3. while (have_posts()) : the_post();
  4. the_title();
  5. the_content();
  6. endwhile;
  7. ?>

İlk Yazıdan sonra Reklam Ekleme

Sorun : Blogunuza reklam koymak ve reklamınıza uygun yer bulmak daha fazla tıklamana anlamına gelir. Bir çok blogcu reklamları sol menuye sayfanın üstüne veya altına koymaktadır. Ancak ziyaretçilerin ilgisini ve dikkatini çeken şey sizin içeriğinizdir. Bu durumlar için içeriğinize örneğin ilk posttan sonra reklam eklemek hoş olmazmıydı?

İşte çözümü :

  1. <?php if (have_posts()) : ?>
  2. <?php $count = 0; ?>
  3. <?php while (have_posts()) : the_post(); ?>
  4. <?php $count++; ?>
  5. <?php if ($count == 2) : ?>
  6. //Paste your ad code here
  7. <h2><a href=“<?php the_permalink(); ?>”><?php the_title(); ?></a></h2>
  8. <?php the_excerpt(); ?>
  9. <?php else : ?>
  10. <h2><a href=“<?php the_permalink(); ?>”><?php the_title(); ?></a></h2>
  11. <?php the_excerpt(); ?>
  12. <?php endif; ?>
  13. <?php endwhile; ?>
  14. <?php endif; ?>

Spesifik fieldlara parametre gizleme

WordPress’in en ilgi çekici özelliklerinden biri olan parametreler bölümünü kendinize göre özelleştirebilirisiniz.

İşte size bir örnek :

  1. <?php query_posts(‘meta_key=review_type&meta_value=movie’);  ?>
  2. <?php if (have_posts()) : ?>
  3. <?php while (have_posts()) : the_post(); ?>

Buraya gireceğiniz özel alanlarla yalnızca belirlediğiniz postları çekmeyi sağlayabilirsiniz.

Gelecek yazılarınızın yayınlanması

WordPress’te en işlevsel özelliklerden biri yine bir yazının gelecek bir tarihe planlanmasıdır. Gelecek tarihlerde yayınlanacak gönderilerinizi bir liste halinde sunarak ziyaretçilerinizin sizi takip etmesini sağlayabilirsiniz.

İşte Çözüm :

  1. <?php query_posts(’showposts=10&post_status=future’); ?>
  2. <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
  3. <h2><?php the_title(); ?></h2>
  4. <span class=“datetime”><?php the_time(‘j. F Y’); ?></span></p>
  5. <?php endwhile;
  6. else: ?><p>No future events scheduled.</p>
  7. <?php endif; ?>

1 Yıl önce yayınlanan yazılar

Bir çok blogda eski yazılar göz ardı edilmektedir. Bu yöntem sayesinde 1 yıl önce yazmılmış yazılarınızı yayınlayabilirsiniz.

  1. <?php
  2. $current_day = date(‘j’);
  3. $last_year = date(‘Y’)-1;
  4. query_posts(‘day=’.$current_day.‘&year=’.$last_year);
  5. if (have_posts()):
  6. while (have_posts()) : the_post();
  7. the_title();
  8. the_excerpt();
  9. endwhile;
  10. endif;
  11. ?>

Geçmiş yazılaın arşivlenmesi

Bir arşiv sayfası oluşturarakta geçmiş yazılarınıza kolaylıkla ulaşılabilinmesini sağlayabilirsiniz.

  1. <?php
  2. /*
  3. Template Name: Archives
  4. */
  5. ?>
  6. <?php get_header(); ?>
  7. <h2><?php $numposts = $wpdb->get_var(“SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = ’publish’”);
  8. if (0 < $numposts) $numposts = number_format($numposts); ?>
  9. <h2><?php echo $numposts.‘ recipes published since October 06, 2008′; ?>
  10. </h2>
  11. <ul id=“archive-list”>
  12. <?php
  13. $myposts = get_posts(‘numberposts=-1&’);
  14. foreach($myposts as $post) : ?>
  15. <li><?php the_time(‘m/d/y’) ?>: <a href=“<?php the_permalink(); ?>”><?php the_title(); ?></a></li>
  16. <?php endforeach; ?>
  17. </ul>
  18. <?php get_sidebar(); ?>
  19. <?php get_footer(); ?>

Kendi WordPress Döngülerinizi oluşturun

Kullanışlı döngü yapılarıyla ilgili geliştirmek istediğiniz şeyler için kendi looplarınızı yazabilirsiniz.

İşte bir örnek :

  1. <?php
  2. $myPosts = new WP_Query();
  3. $myPosts->query(’showposts=5′);
  4. while ($myPosts->have_posts()) : $myPosts->the_post(); ?>
  5. the_title();
  6. the_content();
  7. endwhile;
  8. ?>

Yapışkan Notların Alınması

Wordpress 2.7 ile gelen yapışkan notları sayfanız içerisine almak isterseniz bu kodları kullanabilirsiniz.

  1. <?php
  2. $sticky = get_option(’sticky_posts’);
  3. rsort( $sticky );
  4. $sticky = array_slice( $sticky, 0, 5);
  5. query_posts( array( ‘post__in’ => $sticky, ‘caller_get_posts’ => 1 ) );
  6. if (have_posts()) :
  7. while (have_posts()) : the_post();
  8. the_title();
  9. the_excerpt();
  10. endwhile;
  11. endif;
  12. ?>

Resimleri Döngü Olarak Almak

Blogunuzda kullanacağınız resimleri döngü yöntemi ile listeyebiliyorsunuz.

  1. function catch_that_image() {
  2. global $post, $posts;
  3. $first_img = ;
  4. ob_start();
  5. ob_end_clean();
  6. $output = preg_match_all(‘/<img.+src=[\'"]([^\'"]+)[\'"].*>/i’, $post->post_content, $matches);
  7. $first_img = $matches [1] [0];
  8. if(emptyempty($first_img)){ //Defines a default image
  9. $first_img = “/images/default.jpg”;
  10. }
  11. return $first_img;
  12. }

bu kodu functions sayfasına ekleyebilirsiniz.

  1. <?php
  2. if (have_posts()) :
  3. while (have_posts()) : the_post(); ?>
  4. <a href=“<?php the_permalink();?>” title=“<?php the_title(); ?>” class=“img-loop”>
  5. <img src=“http://media2.smashingmagazine.com/wp-content/uploads/images/wordpress-loop-hacks/<?php echo catch_that_image() ?>” alt=“<?php the_title(); ?>” />
  6. </a>
  7. endwhile;
  8. endif;
  9. ?>

Kaynaklar

Comment Form

Categories