Support » Plugin: WordPress Popular Posts » dynamic categories filter

  • Resolved jonsby

    (@jonsby)


    Hi, I was looking at a way to show related posts only to the categories assigned to the current post in single php.

    i use this to get a comma separated list of the categories of the current post.

    foreach((get_the_category()) as $category) {
    $myCats = $category->cat_ID . ‘, ‘;
    }
    i was wondering if i can put the value of $myCats to the cat” ” parameter in the template tag? i can’t seem to get it to work.

    https://wordpress.org/plugins/wordpress-popular-posts/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Héctor Cabrera

    (@hcabrera)

    Hi there!

    Yes, that should work. Please post the full code here so I can check it out.

    `<?php
    foreach((get_the_category()) as $category) {
    $myCats .= $category->cat_ID . ‘, ‘;
    }
    ?>

    <?php
    if (function_exists(‘wpp_get_mostpopular’))
    wpp_get_mostpopular( ‘range=”all” limit=3 thumbnail_width=215&thumbnail_height=100 stats_views=0 cat=”‘ . $myCats . ‘” post_type=”post,blogg’ );
    ?>

    Plugin Author Héctor Cabrera

    (@hcabrera)

    Is that code inside the loop? If the answer is no, then you need to pass the post / page ID to the get_the_category() function for it to work. Try the following:

    <?php
    global $post;
    $categories = get_the_category($post->ID);
    
    foreach($categories as $category) {
        $myCats .= $category->cat_ID . ', ';
    }
    
    // remove trailing comma
    $myCats = rtrim($myCats, ', ');
    ?>
    
    <?php
    // You had a few errors in here, I fixed them for you
    if (function_exists('wpp_get_mostpopular'))
        wpp_get_mostpopular( 'range="all"&limit=3&thumbnail_width=215&thumbnail_height=100&stats_views=0&cat="' . $myCats . '"&post_type="post,blogg' );
    ?>

    Works great!

    Thanks for the help and for providing such excellent support.

    I donated a few bucks from the plugin page. Should be enough for a beer or two 🙂

    Plugin Author Héctor Cabrera

    (@hcabrera)

    Don’t mention it. And thank you for the nice gesture! 🙂

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘dynamic categories filter’ is closed to new replies.