按类别随机显示相关帖子

时间:2013-02-18 作者:Olive

我使用一个smthemes,主题中有这个代码来显示相关的帖子。它显示了最近创建的相关帖子,但我想随机显示相关帖子,我该怎么做?

global $SMTheme;
if ($SMTheme->get( \'layout\', \'related\' )) { ?>
                <div class="related-posts">
                <h3><?php echo $SMTheme->_( \'relatedposts\' )?>:</h3><ul>
    <?php
    $postid=$post->ID;
    $showed_posts=array();
    $tags=get_the_tags($postid);
    $categories = get_the_category($postid);
    $post_cnt=0;
    $first=0;

    if ($categories&&($post_cnt<$SMTheme->get( \'layout\', \'relatedcnt\' ))) {
        $category_ids = array();
        foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
        $showed_posts[]=$postid;
        $args=array(
        \'category__in\' => $category_ids,
        \'post__not_in\' => $showed_posts,
        \'showposts\'=>$SMTheme->get( \'layout\', \'relatedcnt\' )-$post_cnt,
        \'caller_get_posts\'=>1);
        $posts=get_posts($args);
        if( count($posts)>0 ) {
            foreach ($posts as $p) {
                $ttl=iconv_substr( $p->post_title, 0, 150, \'utf-8\' );
                ?>
                <li<?php echo (!($first%4))?" class=\'first\'":""?>><?php
                $post_cnt++;
                if ($SMTheme->get( \'layout\', \'colors\' )) {
                    $r=rand(1,150);
                    $g=rand(1,150);
                    $b=rand(1,150);
                } else {
                    $r=$g=$b=1;
                }
                $first++;
           if(has_post_thumbnail($p->ID))  {
                ?><a href="<?php echo get_permalink($p->ID); ?>" title="<?php printf( $SMTheme->_( \'permalink\' ), $p->post_title ); ?>" class=\'img\'><?php
                echo get_the_post_thumbnail($p->ID,array(126,126), array(\'title\'=>$p->post_title, \'alt\'=>$p->post_title));
                ?></a><?php  
            } else {
                ?><a href="<?php echo get_permalink($p->ID); ?>" title="<?php printf( $SMTheme->_( \'permalink\' ), $p->post_title ); ?>" class=\'img\'><img src=\'<?php echo get_template_directory_uri(); ?>/images/smt/article_related.png\' width=\'126\' height=\'126\' alt=\'article\' /></a><?php  
            }
        ?><br /><a href="<?php echo get_permalink($p->ID) ?>" title="<?php printf( $SMTheme->_( \'permalink\' ), $p->post_title ); ?>" rel="bookmark" class="related-ttl"><div class=\'related-bg\' style=\'background:rgb(<?php echo $r.",".$g.",".$b?>)\'></div><span><?php echo $p->post_title ?></span></a></li>
                <?php
            }
        }
    }

    if (!$post_cnt) {
        ?><p><?php echo $SMTheme->_( \'norelatedposts\' )?></p><?php
    }
    ?></ul></div>
<?php } ?> 

1 个回复
最合适的回答,由SO网友:Milo 整理而成

在的参数中get_posts, 添加\'orderby\' => \'rand\'. 看见get_posts 在Codex中获取可用参数的完整列表。

$args = array(
    \'category__in\' => $category_ids,
    \'post__not_in\' => $showed_posts,
    \'showposts\' => $SMTheme->get( \'layout\', \'relatedcnt\' )-$post_cnt,
    \'caller_get_posts\' => 1,
    \'orderby\' => \'rand\'
);

结束

相关推荐

将Query_Posts()和Get_Posts()组合为一个查询

我正在创建一个网站,它使用博客的普通帖子和事件的自定义帖子类型(由事件插件生成)。我成功地使用了不同的查询来获取这两个查询,但我想将它们合并到一个查询中。尝试过各种各样的事情,但都没有成功。以下是对博客的查询:<?php $mainFeatures = array( \'numberposts\' => -1, \'order\'=> \'DESC\', \'orderby\' => \'post_date\'); $postslist = get_posts( $mai