3

So I have a basic loop on category.php that displays all posts from the specific category, like this:

<?php if (have_posts()) :
    while(have_posts()): the_post();
?>
    <a href="<?php the_permalink(); ?>">
        <div>
            <h3><?php echo the_title();?></h3>
            <p><?php echo the_content(); ?></p>
        </div>
    </a>
<?php
    endwhile;
endif;
?>

I have another loop on another page that displays the most liked posts (posts in order of the number of their likes). That loop is:

    <?php 
    $query_args  = array(
    'post_type' => 'post',
    'posts_per_page' => 2,
    'orderby' => 'meta_value',
    'order' => 'DESC',
    'meta_query' => array(
        'relation' => 'OR',
         array(
            'key' => 'Likes',
            'compare' => 'NOT EXISTS',
            'type' => 'numeric'
         ),
         array(
            'key' => 'Likes',
            'compare' => 'EXISTS',
            'type' => 'numeric'
         )
    )
);
$loop = new WP_Query($query_args);
if ($loop->have_posts()):
    while($loop->have_posts()):
        $loop->the_post();
?>
        <a href="<?php the_permalink(); ?>">
            <div>
                <h3><?php echo the_title();?></h3>
                <p><?php echo the_content(); ?></p>
            </div>
        </a>
<?php
    endwhile;
endif;
?>

So now I want another loop inside category.php that displays posts from the specific category based on the number of likes, or in other words, popular posts from that category. How do I go about combining these two loops to achieve that? Any help will be highly appreciated.

Cheers.

1 Answer 1

1

First you will need the slug of the current category:

  $cat = get_query_var('cat');
  $currentcat = get_category ($cat);

Then you can use the same loop. Just add the following to $query_args to limit the outcome to the current category:

'category_name' => $currentcat->slug,
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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