0
votes

I want to add some related articles at the bottom of my wordpress posts.

All my posts are categorized in BOTH a parent and child category. I need to pull four RANDOM related posts from the same CHILD category as the main post (not the first four in the category) then display them at the bottom of the post, through single.php.

I'm a newbie to WP & the loop.... any ideas on how I do this?

Edited to include the code I'm using to display posts on category pages - the random related posts need to be in this same grid.

          <!-- START OF THE FLEX CONTAINER, THE UNORDERED LIST -->
    <ul class="grid-wrap">

        <!-- WP LOOP STARTS HERE -->
        <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

            <!-- LIST ITEM FOR EACH POST -->
            <li class="grid-item">

                <!-- FEATURED IMAGE FOR THE POST -->
                <p>
                    <?php if ( has_post_thumbnail() ) : ?>
                                    <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'small' ); ?></a>
                    <?php endif; ?>
                </p>

                <!-- POST TITLE -->
                <h2 class="title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>

                <?php endwhile; ?>
                <?php endif; ?>

            <!-- END OF THE LIST ITEM -->
            </li>

        <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>></div>

    <!-- END OF THE FLEX CONTAINER -->
    </ul>
1

1 Answers

0
votes

Assuming that the code is already written for related post, you need to add the random order into it.

  $args = array(
    'orderby'        => 'rand',
    'posts_per_page' => '1',

);
$query = new WP_Query( $args );

Please change it according to you code. If you have any issue please let me know about it.

Thanks and Regards