arie_cristianD

show popular post list shortcode

Jun 14th, 2026
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.63 KB | None | 0 0
  1. if ( ! function_exists( 'custom_jnews_popular_post_lists_shortcode' ) ) {
  2.     /**
  3.      * Render popular post list based on selected query sort.
  4.      *
  5.      * Usage: [custom_jnews_popular_post_lists sortBy="popular_post_day" limit="4"]
  6.      *
  7.      * @param array $atts Shortcode attributes.
  8.      *
  9.      * @return string
  10.      */
  11.     function custom_jnews_popular_post_lists_shortcode( $atts ) {
  12.         $atts = shortcode_atts(
  13.             array(
  14.                 'sortby' => 'popular_post_day',
  15.                 'limit'  => 4,
  16.             ),
  17.             array_change_key_case( (array) $atts, CASE_LOWER ),
  18.             'custom_jnews_popular_post_lists'
  19.         );
  20.  
  21.         $valid_value = array( 'popular_post_day', 'popular_post_week', 'popular_post_month', 'popular_post' );
  22.         $sort_by     = sanitize_key( $atts['sortby'] );
  23.         $sort_by     = in_array( $sort_by, $valid_value, true ) ? $sort_by : 'popular_post_day';
  24.         $limit       = max( 1, absint( $atts['limit'] ) );
  25.  
  26.         $output = '';
  27.  
  28.         $output .= '<div class="custom-popular-posts-lists">';
  29.         $widget  = new \JNews\Widget\Normal\Element\TabPostWidget();
  30.  
  31.         if ( function_exists( 'jnews_view_counter_query' ) ) {
  32.             $results = \JNews\Module\ModuleQuery::do_query(
  33.                 array(
  34.                     'post_type'              => 'post',
  35.                     'sort_by'                => $sort_by,
  36.                     'post_offset'            => 0,
  37.                     'number_post'            => $limit,
  38.                     'pagination_number_post' => $limit,
  39.                 )
  40.             );
  41.  
  42.             if ( isset( $results['result'] ) ) {
  43.                 $output .= $widget->trending_content( $results['result'] );
  44.             }
  45.         }
  46.  
  47.         $output .= '</div>';
  48.  
  49.         return jnews_sanitize_output( $output );
  50.     }
  51. }
  52.  
  53. add_shortcode( 'custom_jnews_popular_post_lists', 'custom_jnews_popular_post_lists_shortcode' );
Advertisement
Add Comment
Please, Sign In to add comment