Title: use do_shortcode() in post_html
Last modified: October 3, 2024

---

# use do_shortcode() in post_html

 *  Resolved [quangtuyen1601](https://wordpress.org/support/users/quangtuyen1601/)
 * (@quangtuyen1601)
 * [1 year, 8 months ago](https://wordpress.org/support/topic/use-do_shortcode-in-post_html/)
 *     ```wp-block-code
       <?phpfunction 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="{url}">                            <img src="{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">                                        ' . do_shortcode('[count_chap post_id="{pid}"]') . ', {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');?>
       ```
   
 * i want to use **‘ . do_shortcode(‘[count_chap post_id=”{pid}”]’) . ‘** in post_html
   but it seems it doesn’t receive the {pid} as expected
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fuse-do_shortcode-in-post_html%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 5 replies - 1 through 5 (of 5 total)

 *  Plugin Author [Hector Cabrera](https://wordpress.org/support/users/hcabrera/)
 * (@hcabrera)
 * [1 year, 8 months ago](https://wordpress.org/support/topic/use-do_shortcode-in-post_html/#post-18052074)
 * Hey [@quangtuyen1601](https://wordpress.org/support/users/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](https://github.com/cabrerahector/wordpress-popular-posts/wiki/2.-Template-tags#content-tags)
   called {render_count_chap} that will handle the output of your shortcode, like
   this for example:
 *     ```wp-block-code
       /** * 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:
 *     ```wp-block-code
       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="{url}">                            <img src="{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](https://github.com/cabrerahector/wordpress-popular-posts/wiki/2.-Template-tags#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.
 *  Thread Starter [quangtuyen1601](https://wordpress.org/support/users/quangtuyen1601/)
 * (@quangtuyen1601)
 * [1 year, 8 months ago](https://wordpress.org/support/topic/use-do_shortcode-in-post_html/#post-18052226)
 * I tried and succeeded, you are very enthusiastic, thank you very much
 *  Thread Starter [quangtuyen1601](https://wordpress.org/support/users/quangtuyen1601/)
 * (@quangtuyen1601)
 * [1 year, 8 months ago](https://wordpress.org/support/topic/use-do_shortcode-in-post_html/#post-18053311)
 * Hi, let me ask another question, I want to get the list of posts with the most
   views without using **wpp_get_mostpopular **is that possible?
 * It seems that using meta_key is not possible.
 *  Thread Starter [quangtuyen1601](https://wordpress.org/support/users/quangtuyen1601/)
 * (@quangtuyen1601)
 * [1 year, 8 months ago](https://wordpress.org/support/topic/use-do_shortcode-in-post_html/#post-18053379)
 * I did it this way: **How To: Sorting a custom query by views (All time, monthly,
   weekly, or daily)**
 * But when it comes to displaying the number of views for each post, it only shows
   0 views
 *     ```wp-block-code
       <?php echo do_shortcode('[wpp_views_count include_views_text="0" post_id=' . get_the_ID() . ']'); ?>
       ```
   
 *     ```wp-block-code
       function render_hot_stories_shortcode($atts) {    // Lấy tham số shortcode    $atts = shortcode_atts(array(        'num' => 5, // Số lượng bài viết cần lấy    ), $atts, 'hot_stories');    $args = array(        'post_type' => 'post',        'meta_key' => 'views_total',        'orderby' => 'meta_value_num',        'order' => 'DESC',        'posts_per_page' => intval($atts['num']), // Lấy số lượng bài viết từ tham số shortcode    );    $top_posts = new WP_Query($args);    ob_start(); // Bắt đầu lưu trữ output để trả về sau    if ($top_posts->have_posts()) {        ?>        <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">                    <?php while ($top_posts->have_posts()) : $top_posts->the_post(); ?>                        <li class="" 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="<?php the_permalink(); ?>" uk-scroll="">                                <picture>                                  <?php if (has_post_thumbnail()) : ?>                                    <source type="image/webp" srcset="<?php echo get_the_post_thumbnail_url(get_the_ID(), 'full'); ?> 158w" sizes="(min-width: 158px) 158px">                                    <img decoding="async" src="<?php echo get_the_post_thumbnail_url(get_the_ID(), 'full'); ?>" width="158" height="258" alt="" class="el-image uk-transition-opaque">                                  <?php else : ?>                                    <source type="image/webp" srcset="<?php echo get_stylesheet_directory_uri() . '/images/Chua-co-anh-bia.png'; ?> 158w" sizes="(min-width: 158px) 158px">                                    <img decoding="async" src="<?php echo get_stylesheet_directory_uri() . '/images/Chua-co-anh-bia.png'; ?>" width="158" height="258" alt="" class="el-image uk-transition-opaque">                                 <?php endif; ?>                                </picture>                                    <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"><?php the_title(); ?></h3>                                            <div class="el-meta uk-text-emphasis">                                                <div class="uk-text-center" uk-grid>                                                    <div class="uk-width-1-2">                                                        <?php echo do_shortcode('[count_chap post_id="' . get_the_ID() . '"]'); ?>                                                    </div>                                                    <div class="uk-width-1-2">                                                        <img src="/wp-content/themes/theme-child/images/view.svg" alt="views" title="views" width="16" height="16" style="vertical-align: middle;" uk-svg>                                                        <?php echo do_shortcode('[wpp_views_count include_views_text="0" post_id=' . get_the_ID() . ']'); ?>                                                    </div>                                                </div>                                            </div>                                        </div>                                    </div>                                </a>                            </div>                        </li>                    <?php endwhile; ?>                </ul>            </div>        </div>        <?php    }    wp_reset_postdata(); // Đặt lại dữ liệu sau khi query    return ob_get_clean(); // Trả về output đã lưu trữ}add_shortcode('hot_stories', 'render_hot_stories_shortcode');
       ```
   
    -  This reply was modified 1 year, 8 months ago by [quangtuyen1601](https://wordpress.org/support/users/quangtuyen1601/).
 *  Thread Starter [quangtuyen1601](https://wordpress.org/support/users/quangtuyen1601/)
 * (@quangtuyen1601)
 * [1 year, 8 months ago](https://wordpress.org/support/topic/use-do_shortcode-in-post_html/#post-18053584)
 * i found the reason thank you very much 😀

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘use do_shortcode() in post_html’ is closed to new replies.

 * ![](https://ps.w.org/wordpress-popular-posts/assets/icon-256x256.png?rev=1232659)
 * [WP Popular Posts](https://wordpress.org/plugins/wordpress-popular-posts/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/wordpress-popular-posts/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/wordpress-popular-posts/)
 * [Active Topics](https://wordpress.org/support/plugin/wordpress-popular-posts/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wordpress-popular-posts/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wordpress-popular-posts/reviews/)

 * 5 replies
 * 2 participants
 * Last reply from: [quangtuyen1601](https://wordpress.org/support/users/quangtuyen1601/)
 * Last activity: [1 year, 8 months ago](https://wordpress.org/support/topic/use-do_shortcode-in-post_html/#post-18053584)
 * Status: resolved