如何随机更改wordpress主题相关帖子?

时间:2015-03-15 02:32:50

标签: php wordpress

如何使帖子底部显示的相关帖子成为该类别/标签的随机帖子?现在它显示了该类别/标签的最新帖子,但我想显示随机帖子。知道我怎么能这样做吗?  我在主题的functions.php中找到了这个。 我是新手,所以请尽可能简单地解释。

非常感谢!

/**
 * Related Posts
 *
 * @since 1.0
 */
function dp_related_posts($args = '') {
    global $post;

    $query_args = array();

    $defaults = array(
        'view' => 'grid-mini',
        'number' => 0,
        'fields' => '' // object, html or leave it blank
    );
    $args = wp_parse_args($args, $defaults);
    extract($args);

    // Only displayed on singular post pages
    if(!is_singular())
        return;

    // Check limited number
    if(!$number)
        return;

    // Check taxonomies
    $taxes = get_post_taxonomies($post->ID);

    if(empty($taxes))
        return;

    $taxes = array_unique(array_merge(array('post_tag', 'category'), $taxes));

    $tax_query = array();
    $in_tax_query_array = array();
    $and_tax_query_array = array();

    foreach($taxes as $tax) {
        if($tax == 'post_format') {
            // Post format
            $post_format = get_post_format($post->ID);
            if(!$post_format) $post_format = 'standard';
            $post_format_query_array = array(
                'taxonomy' => 'post_format',
                'field' => 'slug',
                'terms' => 'post-format-'.$post_format,
                'operator' => 'IN'
            );

            continue;
        }

        $terms = get_the_terms($post->ID, $tax);

        if(empty($terms))
            continue;
        $term_ids = array();
        foreach($terms as $term)
            $term_ids[] = $term->term_id;

        $in_tax_query_array[$tax] = array(
            'taxonomy' => $tax,
            'field' => 'id',
            'terms' => $term_ids,
            'operator' => 'IN'
        );

        $and_tax_query_array[$tax] = array(
            'taxonomy' => $tax,
            'field' => 'id',
            'terms' => $term_ids,
            'operator' => 'AND'
        );
    }

    if(empty($in_tax_query_array) && empty($and_tax_query_array))
        return;     

    $query_args = array(
        'post_type' => get_post_type($post->ID),
        'ignore_sticky_posts' => true, 
        'posts_per_page' => $number
    );

    $current_post_id = $post->ID;
    $found_posts = array();

    // Multiple Taxonomy Query: relation = AND, operator = AND
    $query_args['tax_query'] = $and_tax_query_array;
    $query_args['tax_query'][] = $post_format_query_array;
    $query_args['tax_query']['relation'] = 'AND';
    $query_args['post__not_in'] = array($post->ID);
    $related = new WP_Query($query_args); 
    foreach($related->posts as $post)
        $found_posts[] = $post->ID;

    // Multiple Taxonomy Query: relation = AND, operator = IN
    if(count($found_posts) < $number) {
        $query_args['tax_query'] = $in_tax_query_array;
        $query_args['tax_query'][] = $post_format_query_array;
        $query_args['tax_query']['relation'] = 'AND';
        $query_args['post__not_in'] = array_merge(array($current_post_id), $found_posts);
        $related = new WP_Query($query_args); 
        foreach($related->posts as $post)
            $found_posts[] = $post->ID;
    }

    $post_format_query = array(
        'taxonomy' => 'post_format',
        'field' => 'slug',
        'terms' => get_post_format(),
        'operator' => 'IN'
    );

    // Foreach Each Taxonomy Query: operator = AND
    if(count($found_posts) < $number) {

        foreach($and_tax_query_array as $and_tax_query) {
            $query_args['tax_query'] = array($and_tax_query);
            $query_args['tax_query'][] = $post_format_query_array;
            $query_args['tax_query']['relation'] = 'AND';
            $query_args['post__not_in'] = array_merge(array($current_post_id), $found_posts);
            $related = new WP_Query($query_args);
            foreach($related->posts as $post)
                $found_posts[] = $post->ID;

            if(count($found_posts) > $number)
                break;
        }
    }

    // Foreach Each Taxonomy Query: operator = IN
    if(count($found_posts) < $number) {

        foreach($in_tax_query_array as $in_tax_query) {
            $query_args['tax_query'] = array($in_tax_query);
            $query_args['tax_query'][] = $post_format_query_array;
            $query_args['tax_query']['relation'] = 'AND';
            $query_args['post__not_in'] = array_merge(array($current_post_id), $found_posts);
            $related = new WP_Query($query_args);
            foreach($related->posts as $post)
                $found_posts[] = $post->ID;

            if(count($found_posts) > $number)
                break;
        }
    }

    if(empty($found_posts))
        return;

    $query_args['tax_query'] = '';
    $query_args['post__in'] = $found_posts;
    $related = new WP_Query($query_args);

    if($fields == 'object')
        return $related;

    if(!empty($args['template']) && is_callable($args['template'])) {
        call_user_func($args['template'], $related);
        return;
    }
    ?>

    <div class="section-box related-posts">
        <div class="section-header"><h3 class="section-title"><?php _e('You may also like', 'dp') ?></h3></div>

        <div class="section-content <?php echo $view; ?>"><div class="nag cf">
            <?php if( $related->have_posts() ) : while( $related->have_posts() ) : $related->the_post(); 
            global $post;
            global $section_view;
            $section_view = 'grid-mini';
            get_template_part('item-video');
            endwhile; endif; wp_reset_query(); ?>
        </div></div>
    </div><!-- end .related-posts -->

2 个答案:

答案 0 :(得分:1)

添加参数&#39; orderby&#39; =&GT; &#39;兰特&#39;我认为应该这样做。 http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

答案 1 :(得分:0)

使用此代码:

<div class="relatedposts">
    <h3>Random related articles</h3>
    <?php
    $related = get_posts( array( 'category__in' => wp_get_post_categories($post->ID), 'orderby' => 'rand' , 'numberposts' => 3, 'post__not_in' => array($post->ID) ) );
    if( $related ) foreach( $related as $post ) {
    setup_postdata($post); ?>

    <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
    <?php the_post_thumbnail(array(243,150)); ?></a>

    <?php }
    wp_reset_postdata(); 
                    ?>