<?php
/**
 * Template part for displaying HTML
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/
 *
 * @package Nevo
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

$label = Nevo()->get_setting( 'trending_posts_label' );
$number = Nevo()->get_setting( 'trending_posts_number' );
$cat = Nevo()->get_setting( 'trending_posts_cat' );
$order_by = Nevo()->get_setting( 'trending_posts_order_by' );
$sort_order = Nevo()->get_setting( 'trending_posts_sort_order' );

$html = '';

$args = array(
	'post_type' => 'post',
	'posts_per_page' => intval($number),
	'orderby' => esc_attr($order_by),
	'order' => esc_attr($sort_order),
	'category_name' => esc_attr($cat),
	'post_status'    => 'publish', // Chỉ lấy bài viết đã xuất bản
    'fields'         => 'ids=>titles', // Chỉ lấy ID và tiêu đề để giảm tải
);

$trending = new WP_Query($args);

if ($trending->have_posts()) : 

	$html .= '<div class="trending-posts-section nevo-container">';

	$html .= '<div class="trending-posts">';

	$html .= '<strong><span>' . esc_html( $label ) . '</span></strong>';

	$html .= '<ul class="trending-list list-inline">';

	while ($trending->have_posts()) : $trending->the_post();

		$html .= '<li class="trending-fade"><a href="' . esc_url(get_permalink()).'">' . esc_html(get_the_title()).'</a></li>';

	endwhile;

	$html .= '</ul>';

	$html .= '</div>';
	$html .= '</div>';

endif;
wp_reset_postdata();

echo $html;