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;
}
}
?>