• Resolved igogra

    (@igogra)


    I need to display 2 blocks of popular posts in the same page but only the first one in the code is being displayed. One has to show the most viewed posts of the week and another one has to show the most viewed posts of the day. They use different templates.

    My code is:

    ...
    <div>
    <?php
    if (function_exists('wpp_get_mostpopular')) {
       $most_viewed_week = 'limit=8&range="weekly"&order_by="views"&post_type="news"';
       add_filter('wpp_custom_html', 'html_most_viewed_week', 10, 2);
       wpp_get_mostpopular($most_viewed_week);
    }
    ?>
    </div>
    ...
    <div>
    <?php
    if (function_exists('wpp_get_mostpopular')) {
       $most_viewed_day = 'limit=6&range="daily"&order_by="views"&post_type="news"';
       add_filter('wpp_custom_html', 'html_most_viewed_day', 10, 2);
       wpp_get_mostpopular($most_viewed_day);
    }
    ?>
    </div>
    ...

    Is there any way to display both blocks in the same page? How can I do it? Thanks

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

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

    (@hcabrera)

    Hi there!

    Just tested your code locally (with a couple of minor changes) and everything works OK over here. Here’s what I did:

    <!-- Weekly block -->
    <?php
    if ( function_exists('wpp_get_mostpopular') ) {
       wpp_get_mostpopular( 'limit=8&range="weekly"&order_by="views"&post_type="post"' );
    }
    ?>
    <!-- Weekly block -->
    
    <!-- Daily block -->
    <?php
    if ( function_exists('wpp_get_mostpopular') ) {
       wpp_get_mostpopular( 'limit=6&range="daily"&order_by="views"&post_type="post"' );
    }
    ?>
    <!-- Daily block -->
    • I removed the DIVs wrapping each block since you can add them in the wpp_custom_html filter hook (see below).
    • I also removed the add_filter calls from your code since these must be placed inside your theme’s functions.php file (or in a plugin, if you know how to).

    To simplify your code a bit futher, you can also use a single function to output different HTML markups for each time range:

    function wpp_html_most_viewed( $popular_posts_obj, $instance ) {
    
      // Wrapper for the daily block
      if ( 'daily' == $instance['range'] ) {
        $output = '<div class="wpp-list-daily">';
      } // Wrapper for the weekly (or any other time range) block
      else {
        $output = '<div class="wpp-list-weekly">';
      }
    
      // Loop each post
      foreach( $popular_posts_obj as $popular_post ) {
    
        if ( 'daily' == $instance['range'] ) {
          // Here goes your daily markup code for each post
        }
        else {
          // Here goes your weekly markup code for each post
        }
    
      }
    
      // Close the wrapper
      if ( 'daily' == $instance['range'] ) {
        $output = '</div>';
      }
      else {
        $output = '</div>';
      }
    
      // ... and then return the custom HTML output to the plugin
      return $output;
    
    }
    add_filter( 'wpp_custom_html', 'wpp_html_most_viewed', 10, 2 );

    The code above should be pretty self explanatory, however feel free to ask any questions you may have.

Viewing 1 replies (of 1 total)
  • The topic ‘How to display 2 blocks of popular posts in the same page’ is closed to new replies.