admin管理员组

文章数量:1421155

I purchased a theme which had a related post functionality, but it sorts by latest so all the my post in the same category have the same related post content. I would like this set to random but dont know how. Tried looking at a few post in here but it crashed by wordpress site.

Appreciate the help in advanced!

<?php 

$orig_post = $post;
global $post;

$categories = get_the_category($post->ID);

if ($categories) {

    $category_ids = array();

    foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;

    if(vp_metabox('manna_post.post_size') == 'full_post' && is_single()) :
    $args = array(
        'category__in'        => $category_ids,
        'post__not_in'        => array($post->ID),
        'posts_per_page'      => 5, // Number of related posts that will be shown.
        'ignore_sticky_posts' => 1
    );
    else :
    $args = array(
        'category__in'        => $category_ids,
        'post__not_in'        => array($post->ID),
        'posts_per_page'      => 4, // Number of related posts that will be shown.
        'ignore_sticky_posts' => 1
    );
    endif;

    $my_query = new wp_query( $args );

    if( $my_query->have_posts() ) { ?>
        <div class="post-related"><h4 class="block-heading"><span><?php _e('You Might Also Like', 'cassia'); ?></span></h4><ul>
        <?php if(vp_metabox('manna_post.post_size') == 'full_post' && is_single()) : ?>
        <?php while( $my_query->have_posts() ) :
            $my_query->the_post();?> 
            <li>
            <div class="related-item full">
                <?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
                <a href="<?php echo get_permalink() ?>" rel="bookmark"><img src="<?php echo aq_resize($url,176,130,true,true,true); ?>" /></a>
                <h4><a href="<?php echo get_permalink() ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>"><?php the_title(); ?></a></h4>


            </div>
            </li>
        <?php
         endwhile;
         else : ?>
        <?php while( $my_query->have_posts() ) :
            $my_query->the_post(); ?>
            <li>
            <div class="related-item">
                <?php if (  (function_exists('has_post_thumbnail')) && (has_post_thumbnail())  ) { ?>
                    <a href="<?php echo get_permalink() ?>" rel="bookmark"><?php the_post_thumbnail('related'); ?></a>
                <?php } ?>
                <h4><a href="<?php echo get_permalink() ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>"><?php the_title(); ?><br><br><?php the_time( get_option('date_format') ); ?></a></h4>



            </div>
            </li>
        <?php
        endwhile;
        endif;

        echo '</ul></div>';
    } else {
        if(is_home() || is_archive()) {
        echo '<div class="related-divider"></div>';
        }
    }
}
$post = $orig_post;
wp_reset_query();

?>

I purchased a theme which had a related post functionality, but it sorts by latest so all the my post in the same category have the same related post content. I would like this set to random but dont know how. Tried looking at a few post in here but it crashed by wordpress site.

Appreciate the help in advanced!

<?php 

$orig_post = $post;
global $post;

$categories = get_the_category($post->ID);

if ($categories) {

    $category_ids = array();

    foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;

    if(vp_metabox('manna_post.post_size') == 'full_post' && is_single()) :
    $args = array(
        'category__in'        => $category_ids,
        'post__not_in'        => array($post->ID),
        'posts_per_page'      => 5, // Number of related posts that will be shown.
        'ignore_sticky_posts' => 1
    );
    else :
    $args = array(
        'category__in'        => $category_ids,
        'post__not_in'        => array($post->ID),
        'posts_per_page'      => 4, // Number of related posts that will be shown.
        'ignore_sticky_posts' => 1
    );
    endif;

    $my_query = new wp_query( $args );

    if( $my_query->have_posts() ) { ?>
        <div class="post-related"><h4 class="block-heading"><span><?php _e('You Might Also Like', 'cassia'); ?></span></h4><ul>
        <?php if(vp_metabox('manna_post.post_size') == 'full_post' && is_single()) : ?>
        <?php while( $my_query->have_posts() ) :
            $my_query->the_post();?> 
            <li>
            <div class="related-item full">
                <?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
                <a href="<?php echo get_permalink() ?>" rel="bookmark"><img src="<?php echo aq_resize($url,176,130,true,true,true); ?>" /></a>
                <h4><a href="<?php echo get_permalink() ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>"><?php the_title(); ?></a></h4>


            </div>
            </li>
        <?php
         endwhile;
         else : ?>
        <?php while( $my_query->have_posts() ) :
            $my_query->the_post(); ?>
            <li>
            <div class="related-item">
                <?php if (  (function_exists('has_post_thumbnail')) && (has_post_thumbnail())  ) { ?>
                    <a href="<?php echo get_permalink() ?>" rel="bookmark"><?php the_post_thumbnail('related'); ?></a>
                <?php } ?>
                <h4><a href="<?php echo get_permalink() ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>"><?php the_title(); ?><br><br><?php the_time( get_option('date_format') ); ?></a></h4>



            </div>
            </li>
        <?php
        endwhile;
        endif;

        echo '</ul></div>';
    } else {
        if(is_home() || is_archive()) {
        echo '<div class="related-divider"></div>';
        }
    }
}
$post = $orig_post;
wp_reset_query();

?>
Share Improve this question edited Dec 13, 2014 at 15:58 Rarst 100k10 gold badges161 silver badges298 bronze badges asked Dec 13, 2014 at 15:12 KalKal 11 bronze badge 1
  • 1 Always develop with debugging enabled so you can see error messages indicating why your code is not working. – Milo Commented Dec 13, 2014 at 16:44
Add a comment  | 

1 Answer 1

Reset to default 1

Either doesn't seem particularly related. I would look at plugins implementing this more thoroughly if you want meaningfully related results.

But to answer your question literally you can randomize order by 'orderby' => 'rand' query argument, see Order & Orderby Parameters in Codex.

本文标签: How to make related post order by random