• Resolved glasshalfpool

    (@glasshalfpool)


    I currently use WordPress Popular Posts across my site.

    On posts, I would like to be able to filter the popular posts to the current category(s).

    Is this something that I can do via the shortcode or would I need to write some custom PHP to achieve this? If I need to write custom code does that then mean I need to change from using the [wpp] shortcode to create a new shortcode or does the custom PHP overwrite the default settings of [wpp]?

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Hector Cabrera

    (@hcabrera)

    Hey @glasshalfpool,

    You’d need some custom PHP code for that, yes, as the shortcode doesn’t have a feature that allows it to automatically detect the category of the current post.

    We can use the shortcode_atts_{shortcode} filter hook to dynamically set the cat parameter for the [wpp] shortcode, like so for example:

    /**
    * Filters popular posts list by current post's category ID.
    *
    * @param array $out The output array of shortcode attributes.
    * @return array $out The (modified) output array of shortcode attributes.
    */
    function wp_1356_filter_popular_posts_by_current_post_category( $out ) {
    if ( is_single() ) {
    $current_post_ID = get_queried_object_id();

    // Get the first category
    $categories = get_the_category( $current_post_ID );

    if ( ! empty( $categories ) ) {
    $category_ID = $categories[0]->term_id;
    $out['cat'] = $category_ID;
    }
    }

    return $out;
    }
    add_filter( 'shortcode_atts_wpp', 'wp_1356_filter_popular_posts_by_current_post_category', 1, 1 );
    Thread Starter glasshalfpool

    (@glasshalfpool)

    Hi @hcabrera

    That works perfectly thank you very much.

    Plugin Author Hector Cabrera

    (@hcabrera)

    Don’t mention it, glad I could help!

Viewing 3 replies - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.