• Resolved quangtuyen1601

    (@quangtuyen1601)


    <?php
    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 xay 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 to see the link]

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

    (@hcabrera)

    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 xay 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 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

    (@quangtuyen1601)

    I tried and succeeded, you are very enthusiastic, thank you very much

    Thread Starter quangtuyen1601

    (@quangtuyen1601)

    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

    (@quangtuyen1601)

    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

    <?php echo do_shortcode('[wpp_views_count include_views_text="0" post_id=' . get_the_ID() . ']'); ?>
    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');
    Thread Starter quangtuyen1601

    (@quangtuyen1601)

    i found the reason thank you very much ??

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.