• 02-02-2009, 23:29:18
    #1
    Platin üye
    Merhaba,
    wordpress de rastgele 5 kategori seçip istediğim bir yerde yazdırmak istiyorum. Epeyce aradım ama işin içinden çıkamadım.

    Şöyle bir eklendi buldum.
    Wordpress plugin: Random categories with random posts Screenflicker Developments

    Ama bunda postları da basıyor.

    Bu eklentide sanırım rastgele kategori seçen fonksiyon şu
        function random_category($ms_catID = NULL) {
            //    returns a random cateogry
            global $wpdb;
            
            $ms_sql  =  "SELECT cat_ID, cat_name, category_count FROM $wpdb->categories WHERE category_count >= '1'";
            if ( $ms_catID ) $ms_sql    .=    " AND cat_ID = '$ms_catID'";
            $ms_sql    .=    " ORDER BY RAND() LIMIT 1";
            
            $ms_query   =  $wpdb->get_results($ms_sql);
            
            foreach ( $ms_query as $cat )
            {
                $ms_category = array( 'id'=>$cat->cat_ID, 'name'=>$cat->cat_name, 'post_count'=>$cat->category_count );
            }
            return $ms_category;
        }
    eklentinin tamamnının kodları ise şöyle. yardımcı olabilirseniz çok sevinirim.
    <?php
       /*
       Plugin Name: Random Category w/ Random Posts
       Plugin URI: http://www.screenflicker.com/blog/web-development/wordpress-plugin-random-categories-with-random-posts/
       Description: Displays a random category and a random list of post titles from the random category. Categories and posts are determined through a user configurable custom field. Usage: echo random_category_and_posts('display_type', 'element_before_heading', 'element_after_heading', number_of_posts, includeExcerpt, require_custom_field, 'custom_field_value'); -- see post at Screenflicker.com for more detailed explanation.
       Version: 0.5
       Author: Mike Stickel
       Author URI: http://www.screenflicker.com/
       */
       
        function random_category_and_posts($type="ul", $beforeHead="h2", $afterHead="h2", $numPosts="5", $ms_categoryID = NULL, $includeExcerpt=false, $customField="", $customValue="")
        {
            global $wpdb; // Global wordpress variables
              
            //    set the default before and after values
            if ( $beforeHead == '' ) $beforeHead    =    'h2';
            if ( $afterHead == '' )    $afterHead    =    'h2';
            if ( $numPosts == '' ) $numPosts    =    '5';
            
            // Get a random category
            $ms_category    =    random_category($ms_categoryID);
            
            
            if ( $ms_category['post_count'] < $numPosts ) {
                $numPosts    =    $ms_category['post_count'];
            }
            
            $ms_articles    =    random_posts($ms_category['id'], $includeExcerpt, $numPosts, $customField, $customValue);
            
            //    Display the category name
            $ms_result  =  ("<" . $beforeHead . ">" . $ms_category['name'] . "</" . $afterHead . ">\n");
            
            if ( count($ms_articles) >= 1 ) {
                // Set up the display as a "list","paragraph", or "paragragh with breaks"
                if ( $type == "br" ) {    //    paragraph with <br />
                    $ms_result    .=    "<p>";
                    $after    =    "<br />\n";
                    $end    =    "</p>\n";
                } else if ( $type == "p" ) {    // paragraphs
                    $before    =    "<p>";
                    $after    =    "</p>\n";
                } else if ( $type == "" || $type == "ul" ) {    //    default list
                    $ms_result    .=    "<ul>\n";
                    $before    =    "<li>";
                    $after    =    "</li>\n";
                    $end    =    "</ul>\n";
                }
                
                for ( $a = 0; $a < count($ms_articles); $a++ ) {
                    if ( $before )    $ms_result    .=    $before;
                    
                    $ms_result    .=    '<a href="'.$ms_articles[$a]['permalink'].'" rel="bookmark" title="Permanent link:'. $ms_articles[$a]['title'].'">'.$ms_articles[$a]['title'].'</a>';
                
                    if ($includeExcerpt && $ms_articles[$a]['excerpt'] ) {
                        $ms_result    .=    '<p>'.$ms_articles[$a]['excerpt']."</p>\n";
                    }
                
                    if ( $after ) $ms_result    .=    $after;
                }
            }
            
            if ( $end ) $ms_result    .=    $end;
            
            return  $ms_result;
       }
    
        function random_category($ms_catID = NULL) {
            //    returns a random cateogry
            global $wpdb;
            
            $ms_sql  =  "SELECT cat_ID, cat_name, category_count FROM $wpdb->categories WHERE category_count >= '1'";
            if ( $ms_catID ) $ms_sql    .=    " AND cat_ID = '$ms_catID'";
            $ms_sql    .=    " ORDER BY RAND() LIMIT 1";
            
            $ms_query   =  $wpdb->get_results($ms_sql);
            
            foreach ( $ms_query as $cat )
            {
                $ms_category = array( 'id'=>$cat->cat_ID, 'name'=>$cat->cat_name, 'post_count'=>$cat->category_count );
            }
            return $ms_category;
        }
        
        function random_posts($ms_catID, $excerpt = '', $numPosts = '5', $customField = '', $customValue = '') {
            // returns an array of posts
            global $wpdb;
            
            $postSQL =  "SELECT $wpdb->post2cat.post_id, $wpdb->post2cat.category_id, $wpdb->posts.ID, $wpdb->posts.post_title, $wpdb->posts.post_excerpt";
            if ( $customField ) $postSQL .= ", $wpdb->postmeta.post_id, $wpdb->postmeta.meta_key, $wpdb->postmeta.meta_value";
            if ( $includeExcerpt )
            {
                $postSQL    .=    ", $wpdb->posts.post_excerpt";
            }
            $postSQL    .=    " FROM $wpdb->post2cat, $wpdb->posts";
            if ( $customField ) $postSQL    .=    ", $wpdb->postmeta";
            $postSQL    .=    " WHERE $wpdb->post2cat.category_id = '$ms_catID' AND $wpdb->posts.ID = $wpdb->post2cat.post_id AND $wpdb->posts.post_status = 'publish'";
            
            //    If the custom field is defined add it to the SQL
            if ( $customField )
            {
                $postSQL    .=    " AND $wpdb->postmeta.meta_key = '$customField' AND $wpdb->postmeta.meta_value = '$customValue' AND $wpdb->postmeta.post_id = $wpdb->posts.ID";
            }
            $postSQL    .=    " ORDER BY RAND() LIMIT $numPosts";
            
            $ms_articles    =    $wpdb->get_results($postSQL);
            
            if ( $ms_articles ) {
                foreach ( $ms_articles as $item ) {
                    $ms_posts[] = array( 'title'=>str_replace('"','',stripslashes($item->post_title)),
                                         'permalink'=>post_permalink($item->ID),
                                        'excerpt'=>$item->post_excerpt
                                        );
                }
                
                return $ms_posts;
            } else {
                return false;
            }
        }
    ?>
  • 02-02-2009, 23:31:59
    #2
    bu eklenti tam olarak ne işe yaryıror, bir bölümde rastgele kategori gösterip, içinde yazıları mı gösteriyor?
  • 02-02-2009, 23:36:57
    #3
    Alıntı
    if ( $numPosts == '' ) $numPosts = '5';
    bu satırı

    Alıntı
    $numPosts = '0';
    böyle yaparsan yazı göstermez
  • 02-02-2009, 23:41:13
    #4
    Niphellin dediği gibi yaparsanızda olur ama veritabanından fonksiyon okuduğu için sitenizde veritabanınız fazla büyükse ufakda olsa yavaş açılma olur.


    function random_posts($ms_catID, $excerpt = '', $numPosts = '5', $customField = '', $customValue = '') {
            // returns an array of posts
            global $wpdb;
            $postSQL =  "SELECT $wpdb->post2cat.post_id, $wpdb->post2cat.category_id, $wpdb->posts.ID, $wpdb->posts.post_title, $wpdb->posts.post_excerpt";
            if ( $customField ) $postSQL .= ", $wpdb->postmeta.post_id, $wpdb->postmeta.meta_key, $wpdb->postmeta.meta_value";
            if ( $includeExcerpt )
            {
                $postSQL    .=    ", $wpdb->posts.post_excerpt";
            }
            $postSQL    .=    " FROM $wpdb->post2cat, $wpdb->posts";
            if ( $customField ) $postSQL    .=    ", $wpdb->postmeta";
            $postSQL    .=    " WHERE $wpdb->post2cat.category_id = '$ms_catID' AND $wpdb->posts.ID = $wpdb->post2cat.post_id AND $wpdb->posts.post_status = 'publish'";
            //    If the custom field is defined add it to the SQL
            if ( $customField )
            {
                $postSQL    .=    " AND $wpdb->postmeta.meta_key = '$customField' AND $wpdb->postmeta.meta_value = '$customValue' AND $wpdb->postmeta.post_id = $wpdb->posts.ID";
            }
            $postSQL    .=    " ORDER BY RAND() LIMIT $numPosts";
            $ms_articles    =    $wpdb->get_results($postSQL);
            if ( $ms_articles ) {
                foreach ( $ms_articles as $item ) {
                    $ms_posts[] = array( 'title'=>str_replace('"','',stripslashes($item->post_title)),
                                         'permalink'=>post_permalink($item->ID),
                                        'excerpt'=>$item->post_excerpt
                                        );
                }
                return $ms_posts;
    bu kodları silerseniz rastgele yazıları göstermez.
  • 02-02-2009, 23:47:06
    #5
    burakH doğru söylemiş ama eklentinin kullanımında post değeri çağırıyor

    o kodlar silinizse hata verme olasılığı vardır .denemedim.

    teşekkürler burakH
  • 03-02-2009, 00:00:00
    #6
    Platin üye
    arkadaşlar ille bu eklenti üzerinden gitmek zorunda değiliz. Ben bunu çalıştıramadım bile
    kısaca istediğim istediğim bir yerde rastgele 5 kategoriyi basmak.
  • 03-02-2009, 00:02:15
    #7
    Bende denemedim bugün 20 saatten beri pc başındayım 2 saatlik daha işim var ve acayip yoğunum beynim zonklamaya,gözlerim ağrımaya başladı.1-2 dklık molalar arasında mesaj yazıyorum deneme şansım olmadı.Kodlarıda pek inceleyemedim ama %90 olur.
  • 03-02-2009, 00:06:27
    #8
    Eklentinin orjinal hali çalışmadıysa işimiz zor olur.Bu tür eski eklentiler yeni versiyonda malesef çalışmıyor ve wp.org'da bu tür eklentilerle artık ilgilenmiyor.Bende böyle eklenti arıyordum.Olmazsa artık yarın bu eklenti üzerinde çalışırım şu işlerimi bir halledeyim ve güzel bir uyku çekeyim bir
  • 03-02-2009, 00:11:38
    #9
    Platin üye
    Teşekkürler arkadaşım. Cevabını bekliyorum.