【问题标题】:How can I change wordpress theme related posts other to random?如何将 wordpress 主题相关的帖子更改为随机的?
【发布时间】:2015-03-16 04:48:09
【问题描述】:

如何使帖子底部显示的相关帖子成为该类别/标签中的随机帖子?现在它显示来自该类别/标签的最新帖子,但我想显示随机帖子。知道我该怎么做吗? 我在主题的 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 -->

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    添加参数 'orderby' => 'rand' 我认为应该这样做。 http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

    【讨论】:

    • 我试图添加它,但它什么也没做。可能我在错误的地方添加了它......你能告诉我在上面的代码中添加它的位置吗?请
    • 我想你会像这样添加它: $query_args['orderby'] = 'rand';就在您的行之前: $related = new WP_Query($query_args);
    • 不客气!也请随意对答案进行投票,这样我就可以获得所有那些甜蜜的互联网积分;)
    【解决方案2】:

    使用此代码:

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

    【讨论】:

    • 虽然这个答案可能是正确且有用的,但最好在其中附上一些解释来解释它如何帮助解决问题。如果有更改(可能不相关)导致它停止工作并且用户需要了解它曾经是如何工作的,这在未来变得特别有用。
    • 嗯,不包括询问者的问题,但告诉你知道,这段代码说获取当前帖子的 3 个相关帖子,按相同类别随机显示,然后按 243px X 150px 大小显示它们的缩略图并将其链接到他们的固定链接。也不需要任何functions.php 代码作为其他解决方案。
    猜你喜欢
    • 1970-01-01
    • 2012-10-31
    • 2012-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多