Anabele Rosemary
Anabele Rosemary

Reputation: 17

How to display popular post by view in current category?

In case: If I want to display a popular post in a category post. So, when I open "XXX" or "YYY" category, will display a popular post first from "XXX" or "YYY" category.

Upvotes: 1

Views: 2487

Answers (1)

Sagive
Sagive

Reputation: 1827

The question title is a bit confusing. there is a way of getting the "popular" posts by comment count but that "by view" in the title of your question suggest you are looking for a different way to go about it?

.
if you want to check post popularity by post views...

First you need to attach a "views count" to each post. a complete function here: catWhoCodes

Now that you got a way to check which posts are popular you need to create a list of post related to the current category but filtered by the post count... here is an easy way to go about it.

<?php
    $category = get_category( get_query_var( 'cat' ) );
    $curCatId = $category->cat_ID;

    $args = array(
        'numberposts'   => 10,
        'cat'           => $curCatId,
        'meta_key'      => 'views',
        'orderby'       => 'meta_value_num',
        'order'         => 'DESC'
    );
    $popPosts = get_posts( $args );


    echo '<ul>';
    foreach ( $popPosts as $popPost ) {
        setup_postdata( $popPost );

        echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';

    }
    echo '</ul>';   

wp_reset_postdata();
?>

.
To get popular posts by comments count

<?php
    $category = get_category( get_query_var( 'cat' ) );
    $curCatId = $category->cat_ID;

    $args = array(
        'numberposts'   => 10,
        'cat'           => $curCatId,
        'orderby'       => 'comment_count'
    );
    $popPosts = get_posts( $args );

    echo '<ul>';
    foreach ( $popPosts as $popPost ) {
        setup_postdata( $popPost );

        echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';

    }
    echo '</ul>';   

wp_reset_postdata();
?>

.
Related:

.
Best of luck,
Sagive.

Upvotes: 1

Related Questions