• Resolved Clayton Chase

    (@claytonchase)


    Hey guys,

    I’m trying to dynamically show the most popular posts in the category of the post currently being viewed. For example, if I’m reading a post in the ‘WordPress’ category, I’d like to show all the top posts in the sidebar of the ‘WordPress’ category. Then if I am reading a post in the ‘Tutorials’ category the sidebar widget would show the most popular posts in the ‘Tutorials’ category.

    I hope that makes sense. This might have already been covered.

    Thanks!

    • This topic was modified 6 years, 11 months ago by Clayton Chase.
Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Hector Cabrera

    (@hcabrera)

    Hi @claytonchase,

    Currently there’s no way to have the widget automatically filter posts by category.

    You can, however, use the wpp_get_mostpopular() template tag for that. For example:

    $args = array(
        'range'     => 'last24hours',
        'post_type' => 'post'
    );
    
    // We're currently viewing a category archive page
    if ( is_category() ) {
        // Retrieve the ID of the category
        $args['cat'] = get_queried_object_id();
    }
    
    wpp_get_mostpopular( $args );
    Thread Starter Clayton Chase

    (@claytonchase)

    Thanks for the reply! I’ll give it a shot.

    I achieved it like this (very simplified though)

     $categories = get_the_category();  //get the category  
            $amadeus_pp = array (
            'range' => 'last30days',
            'order_by' => 'views',
            'post_type' => 'post',
            'limit' => 5,
            'title_length' => 70,
            'cat' => $categories[0]->cat_ID,
            'thumbnail_width' => 50,
            'thumbnail_height' => 50,
            'stats_views' => 0,  
            'post_html' => '<li style="margin-left: 0;"><div class="ba-pop-post"><div class="ba-pop-post-image">{thumb}</div><div class="ba-pop-post-title">{title}</div></div></li>',
            );
            // output the query
            wpp_get_mostpopular($amadeus_pp);
    Plugin Author Hector Cabrera

    (@hcabrera)

    Just realized that I completely misunderstood what the OP was trying to achieve. @divemasterza’s solution is the right answer, thanks for sharing it!

    Note thought that as is it’ll work only if the code is placed somewhere inside The Loop.

    Chantal

    (@boekluxevilla)

    Hi @divemasterza,

    I am looking for this solution to but i don’t know where i must put this code? Can u please show me te way?

    I’m only using the widget at the moment but i think i have to use the template tag?

    Thanks,
    Chantal

    Hi Chantal,

    There are couple ways to achieve this.

    You could create:

    The first option you will need to create a new template as described in the link, in the other two options you will place in the functions.php of your theme (using a child theme is better to preserve easily your code after updates).

    In my case, I used a widget in a sidebar because it was the most flexible to me. You can use this generator to create your code to place in your functions.php It is well commented and you’ll see quickly where you’ll need to insert your custom code. You could wrap everything in an “if statement” to make sure it only displays on posts (my sidebar was only showing on posts so it wasn’t necessary for me)

    if ( is single() ) {
    //Your code
    }

    A big thank you to @hcabrera for a well-documented plugin

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Display popular posts of the current category’ is closed to new replies.