Support » Plugin: WordPress Popular Posts » Placing code for reading time, and link on sorrounding div

  • Resolved anyway

    (@sfdsfwer)


    I have 3 cards in a row in Bootstrap and I wrote a custom template for it. However, after the headline I need to place the reading time of the article. For this I have this plugin:

    https://de.wordpress.org/plugins/wordpress-popular-posts/

    which is integrated with a shortcode:

    <?php echo do_shortcode('[rt_reading_time label="Reading Time:" postfix="Minutes" postfix_singular="Minute"]') ?>;

    Is it possible to get this in there somehow?

    This is my code:

    <?php
    if ( function_exists('wpp_get_mostpopular') ) {
    $args = array(
    'range' => 'all',
    'post_type' => 'post, page',
    'header_start' => '<h2 class="h5">',
    'header_end' => '</h2>',
    'limit' => 3,
    'range' => 'last30days',
    'thumbnail_width' => 516,
    'thumbnail_height' => 151,
    'excerpt_by_words' => 1,
    'excerpt_length' => 25,
    'stats_category' => 1,
    
    'wpp_start' => '<div class="row"><div class="col-md-12"><div class="row">',
    'post_html' => '<div class="col-md-4"><div class="post-thumbnail">{thumb}</div><div>{taxonomy}</div><h3 class="h6">{title}</h3><div>{excerpt}</div></div>',
    'wpp_end' => '</div></div></div>',
    
    );
    wpp_get_mostpopular( $args );
    }
    ?>

    ..

    And one more question:
    Is it possible to place a link to the article on the individual surrounding DIVs of the cards?

    • This topic was modified 1 month, 4 weeks ago by anyway.
    • This topic was modified 1 month, 4 weeks ago by anyway.
Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author Héctor Cabrera

    (@hcabrera)

    Hi @sfdsfwer,

    Is it possible to get this in there somehow?

    The short answer is yes, it’s possible. However, to tell you exactly what you need to do I need to know how that shortcode works. Is it from another plugin? Or is it some custom code you/your developer wrote for your website?

    Is it possible to place a link to the article on the individual surrounding DIVs of the cards?

    Yep, you could just do something like this:

    'post_html' => '<div class="col-md-4" onclick="window.location.href={url}"> ... </div>'

    However, I’d avoid placing inline JavaScript code like that for various reasons. Personally I’d do it like this instead.

    Thread Starter anyway

    (@sfdsfwer)

    Hi Héctor,

    thanks for your quick reply and of course for the awesome plugin!

    I’m sorry, I posted the wrong plugin link… this is the right one:
    https://wordpress.org/plugins/reading-time-wp/

    About the surrounding link: I actually wanted to avoid using jQuery (or JS), but with this it works of course… thank you!

    One more question: is there a list with all parameters? I haven’t found one in the wiki.

    • This reply was modified 1 month, 4 weeks ago by anyway.
    Plugin Author Héctor Cabrera

    (@hcabrera)

    Thanks, that helped.

    Alright, so one way we can use that shortcode with the wpp_get_mostpopular() template tag is by creating a custom Content Tag (see Display Tags and Author image for a more detailed explanation + real life examples).

    So, in your case, that would look like this:

    /**
     * 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 {reading_time} with
        // the output of the [rt_reading_time] shortcode
        if ( false !== strpos($html, '{reading_time}') ) {
            $reading_time = do_shortcode('[rt_reading_time post_id=' . $post_id . ' label="Reading Time:" postfix="Minutes" postfix_singular="Minute"]');
    
            // Replace {reading_time} with the value of $reading_time
            $html = str_replace( '{reading_time}', $reading_time, $html );
        }
    
        return $html;
    
    }
    add_filter( "wpp_parse_custom_content_tags", "wpp_parse_tags_in_popular_posts", 10, 2 );

    Add this code to your theme’s functions.php file and then update your post_html parameter to include our new {reading_time} Content Tag so it looks something like this:

    'post_html' => '<div class="col-md-4"><div class="post-thumbnail">{thumb}</div><div>{taxonomy}</div><h3 class="h6">{title}</h3><p class="reading-time">{reading_time}</p><div>{excerpt}</div></div>',

    One more question: is there a list with all parameters?

    Yep, there is. Go to your WordPress Dashboard > Settings > WordPress Popular Posts > Parameters. You’ll find everything there. (Although I should add this to the Wiki so it’s easier to find, maybe I’ll look into it.)

    Plugin Author Héctor Cabrera

    (@hcabrera)

    Yep, there is. Go to your WordPress Dashboard > Settings > WordPress Popular Posts > Parameters. You’ll find everything there. (Although I should add this to the Wiki so it’s easier to find, maybe I’ll look into it.)

    … and done! Parameters (this took forever to do).

    Thread Starter anyway

    (@sfdsfwer)

    Thanks for your effort, that worked perfectly!

    I will soon install “Advanced Custom Fields” in the project and would like to display another field in the row of three cards. Will this work the same way with ACF fields?

    And sorry about the parameters: I must have missed the obvious yesterday. There it is, big and clear and really well documented.

    Plugin Author Héctor Cabrera

    (@hcabrera)

    Thanks for your effort, that worked perfectly!

    Don’t mention it, glad I could help!

    And sorry about the parameters: I must have missed the obvious yesterday. There it is, big and clear and really well documented.

    It’s OK, don’t worry about it 🙂

    I will soon install “Advanced Custom Fields” in the project and would like to display another field in the row of three cards. Will this work the same way with ACF fields?

    Yes you can create a custom Content Tag the same way to display your custom field, like this for example: ACF field integration?

    Thread Starter anyway

    (@sfdsfwer)

    This looks promising. I will try it out in the next few days. Thanks again and have a great weekend!

    Thread Starter anyway

    (@sfdsfwer)

    “Advanced Custom Field” Integration worked great! Once you understand the concept of Custom Content Tags, it’s really easy to work with. Very flexible. Thanks again for your support!

    Plugin Author Héctor Cabrera

    (@hcabrera)

    Once you understand the concept of Custom Content Tags, it’s really easy to work with. Very flexible.

    That’s the idea 😀 Glad you found it useful!

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