Hey @quangtuyen1601,
The post_html parameter expects an static HTML structure. It can’t do dynamic code like that, that’s why it’s not “working.”
If you want to render dynamic content like your [count_chap] shortcode then do this instead:
#1 Register a custom Content Tag called {render_count_chap} that will handle the output of your shortcode, like this for example:
/**
* Parses custom content tags in WordPress Popular Posts.
*
* @param string $html The HTML markup from the plugin.
* @param integer $post_id The post/page ID.
* @return string
*/
function wpp_parse_tags_in_popular_posts($html, $post_id) {// Replace custom content tag {render_count_chap} with [count_chap]
if ( false !== strpos($html, '{render_count_chap}') ) {
// Get tags
$count_chap = do_shortcode('[count_chap post_id="' . $post_id . '"]');
// Replace {render_count_chap} with the output of [count_chap]
$html = str_replace('{render_count_chap}', $count_chap, $html);
}
return $html;
}
add_filter('wpp_parse_custom_content_tags', 'wpp_parse_tags_in_popular_posts', 10, 2);
This would go in your theme’s functions.php file.
#2 Update your code so it uses the newly registered {render_count_chap} Content Tag:
function truyen_hot_shortcode($atts) {
// Lấy tham số num từ shortcode, mặc định là 5
$atts = shortcode_atts(array(
'num' => 5,
), $atts, 'truyen_hot');// Bắt đầu xây dựng output HTML
ob_start();
if (function_exists('wpp_get_mostpopular')) {
// Gọi wpp_get_mostpopular() và sử dụng post_html để định dạng HTML cho từng bài viết
wpp_get_mostpopular(array(
'limit' => intval($atts['num']),
'range' => 'all',
'order_by' => 'views',
'post_type' => 'post',
'stats_views' => 1,
'thumbnail_width' => 158,
'thumbnail_height' => 258,
'wpp_start' => '<div uk-slider="autoplay: 1; autoplayInterval: 3000;" class="uk-margin uk-text-center uk-slider uk-slider-container" role="region" aria-roledescription="carousel"><div class="uk-position-relative"><ul class="uk-slider-items uk-grid uk-grid-small uk-grid-match" aria-live="off" role="presentation">',
'wpp_end' => '</ul></div></div>',
'post_html' => '
<li class="uk-active" aria-hidden="false">
<div class="el-item uk-grid-item-match">
<a class="uk-cover-container uk-transition-toggle uk-display-block uk-link-toggle" href="https://projectdmc.org/support/topic/use-do_shortcode-in-post_html/{url}">
<img src="https://projectdmc.org/support/topic/use-do_shortcode-in-post_html/{thumb_url}" width="158" height="258" class="el-image uk-transition-scale-up uk-transition-opaque uk-object-cover" uk-cover="">
<div class="uk-position-bottom-center uk-overlay-primary">
<div class="uk-overlay uk-padding-remove uk-margin-remove-first-child">
<h3 class="el-title uk-h5 uk-margin-top uk-margin-remove-bottom">{text_title}</h3>
<div class="el-meta uk-text-emphasis">
{render_count_chap}, {views} lượt xem
</div>
</div>
</div>
</a>
</div>
</li>',
'default_thumbnail' => get_stylesheet_directory_uri() . '/images/Chua-co-anh-bia.png', // Ảnh mặc định nếu không có thumbnail
));
} else {
echo "<!-- Plugin WordPress Popular Posts không hoạt động -->";
}
return ob_get_clean();
}
add_shortcode('truyen_hot', 'truyen_hot_shortcode');
That should do the trick.
One observation though: that default_thumbnail parameter you have in your code is not doing anything. The wpp_get_mostpopular() function will ignore it as it doesn’t exist. See Parameters to see a full list of parameters accepted by the wpp_get_mostpopular() function.
If you want to set a fallback thumbnail for when posts don’t have one then go to Settings > WordPress Popular Posts > Tools, under Thumbnails you’ll find an option to set the default thumbnail.
