0

How would I go about displaying related categories instead of the follow which displays related tags. Please any help would be much appreciated. Thanks in advance

if(!function_exists('solstice_related_post')) {
    function solstice_related_post() {
        global $post;
        $tags = wp_get_post_tags($post->ID);

        if(!empty($tags) && is_array($tags)):
            $simlar_tag = $tags[0]->term_id;

            ?>

            <div class="related-posts">
                <h6><?php esc_html_e('YOU MIGHT ALSO LIKE', 'solstice-theme'); ?></h6>
                <div class="row">
                    <?php
                    $args = array(
                        'tag__in'             => array($simlar_tag),
                        'post__not_in'        => array($post->ID),
                        'posts_per_page'      => 3,
                        'meta_query'          => array(array('key' => '_thumbnail_id', 'compare' => 'EXISTS')),
                        'ignore_sticky_posts' => 1,
                    );
                    $re_query = new WP_Query($args);
                    while ($re_query->have_posts()) : $re_query->the_post();
                        ?>
                        <article <?php post_class('blog-post col-md-4'); ?>>
                            <header>
                                <figure>
                                    <?php the_post_thumbnail('solstice-small'); ?>
                                </figure>
                                <h3><a href="<?php echo esc_url(get_the_permalink()); ?>"><?php the_title(); ?></a></h3>
                                <div class="meta">
                                    <span><?php echo get_the_category_list( ' , ', 'solstice-theme' );?></span>
                                    <span><time datetime="<?php the_time('Y-m-d'); ?>"><?php the_time('F d, Y'); ?></time></span>
                                </div><!-- /meta -->
                            </header>
                        </article>
                    <?php endwhile; wp_reset_postdata(); ?>
                </div><!-- /row -->
            </div><!-- /related-posts -->
            <?php
        endif;
    }
}
0

Replace wp_get_post_tags($post->ID); with

wp_get_post_terms($post->ID);

All parameter and details can be found here

and change the $args parameter

$args = array(
    'category__in'        => array($simlar_tag),
    'post__not_in'        => array($post->ID),
    'posts_per_page'      => 3,
    'meta_query'          => array(array('key' => '_thumbnail_id', 'compare' => 'EXISTS')),
    'ignore_sticky_posts' => 1,
  );

Hope it works!

0

Thanks for the response! However, no related post show after the change. Any ideas?

Here' what I changed the code to:

if(!function_exists('solstice_related_post')) {

function solstice_related_post() { global $post; $tags = wp_get_post_terms($post->ID);

if(!empty($tags) && is_array($tags)):
  $simlar_tag = $tags[0]->term_id;

?>

<div class="related-posts">
  <h6><?php esc_html_e('YOU MIGHT ALSO LIKE', 'solstice-theme'); ?></h6>
  <div class="row">
    <?php
    $args = array(
'category__in'        => array($simlar_tag),
'post__not_in'        => array($post->ID),
'posts_per_page'      => 3,
'meta_query'          => array(array('key' => '_thumbnail_id', 'compare' => 'EXISTS')),
'ignore_sticky_posts' => 1,

); $re_query = new WP_Query($args); while ($re_query->have_posts()) : $re_query->the_post(); ?> >

">

">

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.