简体   繁体   English

如何将与wordpress主题相关的其他帖子更改为随机?

[英]How can I change wordpress theme related posts other to random?

How can I make the related posts shown in the bottom of the post be random posts from that category/tag? 如何使帖子底部显示的相关帖子成为该类别/标签中的随机帖子? Right now it shows the latest posts from that categories/tags but I would like to show random posts. 现在,它显示了该类别/标签的最新信息,但我想显示随机信息。 Any idea how I can do that? 知道我该怎么做吗? I found this in my theme's functions.php . 我在主题的functions.php中找到了它。 I'm a newbie so please explain it as easy as possible. 我是新手,所以请尽可能简单地解释一下。

Thanks a lot! 非常感谢!

/**
 * 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 -->

Add the argument 'orderby' => 'rand' I think should do it. 我认为应该添加参数'orderby'=>'rand'。 http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

With this code: 使用此代码:

<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(); 
                    ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM