WordPress.org

Ready to get started?Download WordPress

Forums

WordPress Popular Posts
[resolved] All post content instead of 'excerpt'... (13 posts)

  1. jpanizzoli
    Member
    Posted 1 year ago #

    Great plugin! I'm having no trouble figuring out how to use the parameters to customize the output of my most popular posts.

    But I'd like to display the complete post (rather than just an excerpt) of my #1 post. Then, underneath it I'll have another list of the Top 10 with just the post titles.

    It appears that using 'excerpt_length' will only provide a limited amount of content. I'd like to display all of the content from my #1 post (ie: the_content() ) -- is this possible?

    http://wordpress.org/extend/plugins/wordpress-popular-posts/

  2. Héctor Cabrera
    Member
    Plugin Author

    Posted 1 year ago #

    Hi there,

    You don't need to use the plugin for that. You can use WordPress' WP_Query class to fetch your most recent post, and then use the wpp_get_mostpopular() template tag to display the top 10 popular posts.

    Here's a quick, non-tested example:

    <?php
    $my_latest_post = new WP_Query('showposts=1');
    while ($my_latest_post->have_posts()) : $my_latest_post->the_post(); ?>
        <h2><?php the_title(); ?></h2>
        <p><?php the_content(); ?></p>
        <?php
    endwhile;
    
    if ( function_exists('wpp_get_mostpopular') ) {
      wpp_get_mostpopular('order_by=views');
    }
    ?>

    Of course, you need to adapt the code to your needs.

  3. jpanizzoli
    Member
    Posted 1 year ago #

    Thanks for your reply...

    I'm not looking for the "most recent" post. I want to get the "most popular" -- but display the entire post. Then, I want to display the TOP 10 below it (only with the title).

    I have the TOP 10 displayed already, but I can't figure out how to display the #1 separately with all the content.

    IE:

    #1 Most Popular Post (full content)

    - #1 Most Popular Title
    - #2 Most Popular Title
    - #3 Most Popular Title
    - Etc.
    -------------------------------

    Thanks again for your help!

  4. Héctor Cabrera
    Member
    Plugin Author

    Posted 1 year ago #

    Sorry, I misread your comment, I know what you mean now.

    Well, as of now you can't display the entire content of the post - only the excerpt. You need to alter the plugin for that.

    In wordpress-popular-posts.php find:

    // EXCERPT
    if ( $instance['post-excerpt']['active'] ) {
        if ($instance['markup']['pattern']['active']) {
            $excerpt = $this->get_summary($p->id, $instance);
        } else {
            $excerpt = ": <span class=\"wpp-excerpt\">" . $this->get_summary($p->id, $instance) . "...</span>";
        }
    }

    Change to:

    // EXCERPT
    if ( $instance['post-excerpt']['active'] ) {
        if ($instance['markup']['pattern']['active']) {
            $excerpt = get_the_content($p->id);
        } else {
            $excerpt = ": <span class=\"wpp-excerpt\">" . get_the_content($p->id) . "...</span>";
        }
    }

    ... then, you should call the wpp_get_mostpopular() twice: the first one for displaying only one post and its excerpt, and the other to show the top 10 (which, by the way, will also include the top post).

    Keep in mind that you'd need to make this modification each time the plugin gets updated.

  5. jpanizzoli
    Member
    Posted 1 year ago #

    Hector,

    Thanks again for your help!

    I tried adding the code you provided, but it's still only showing the post title for the #1 post. I updated wordpress-popular-posts.php (as suggested) and used this code in my template:

    <?php if (function_exists('wpp_get_mostpopular')) wpp_get_mostpopular("cat='4'&order_by=views&limit=1&range=all&stats_comments=0&excerpt_format=1"); ?>

    I tried to add the "excerpt_length", but it didn't change the output...

  6. Héctor Cabrera
    Member
    Plugin Author

    Posted 1 year ago #

    In your shortcode you have excerpt_format instead of excerpt_length. Set excerpt_length to any random numeric value higher than 0 and try again.

  7. jpanizzoli
    Member
    Posted 1 year ago #

    Same thing... I had excerpt_format because that's included in the FAQ instructions for adding an excerpt. I removed it and tried excerpt_length but it's still the same thing -- just the TITLE of the post. I also tried both excerpt_format and length together...

    I'm not a programmer by any means, but in your updated code you used "get_the_content". In another plugin I've been playing with, I used "echo $p->post_content" and "echo the_content()" to successfully include the post content. Can this be included in your code somehow??? I tried to mix it in myself, but I'm definitely not a programmer / developer... thanks again!

  8. Héctor Cabrera
    Member
    Plugin Author

    Posted 1 year ago #

    The excerpt_format parameter tells WordPress Popular Posts to keep (or not) all HTML tags in the excerpt (content, in this case), so it's not really necessary to use the excerpt. Where did I say that it was required? If I did, please let me know so I can correct that.

    In the code I posted, the post content is being assigned to a variable which will be used later to display it. As I said, I didn't test what I posted before but it should work. I'll give it a closer look later, maybe I missed something.

  9. jpanizzoli
    Member
    Posted 1 year ago #

    You're right... it doesn't say excerpt_format is required. But since I want to include the entire post, it would be necessary if I wanted all the original formatting.

    I tried using your code and double (triple!) checked everything, but I couldn't get it to work (using excerpt_length now). Tried making some of my own modifications, but I don't really know what I'm doing and nothing worked. Hopefully it's something simple and you can spot the error...

  10. Héctor Cabrera
    Member
    Plugin Author

    Posted 1 year ago #

    It must be something simple to fix, just haven't had the time to check it out. Will let you know once I get some spare time, right now my day job is keeping me quite busy. I'll give it another look in a few hours.

  11. Héctor Cabrera
    Member
    Plugin Author

    Posted 1 year ago #

    Found out what the problem was and gotta say two things:

    1. I should never reply to forum posts when I'm too tired / distracted. The reason why it doesn't work is that there is no way -natively- to retrieve post's content outside The Loop. The functions the_content and get_the_content only work inside The Loop, so passing an ID to get_the_content was never going to work. You need a custom function for this.

    2. Found how to solve that. Add this function to the bottom of your theme's functions.php file:

    /**
     * Get Filtered Content By ID - source: http://techstudio.co/wordpress/the_content-outside-the-wordpress-loop
    */
    function the_content_by_id($post_id) {
         $page_data = get_page($post_id);
         if ( $page_data )
              return apply_filters('the_content',$page_data->post_content);
         return false;
    }

    Then, update the code I posted before to:

    // EXCERPT
    if ( $instance['post-excerpt']['active'] ) {
        if ($instance['markup']['pattern']['active']) {
            $excerpt = the_content_by_id($p->id);
        } else {
            $excerpt = ": <span class=\"wpp-excerpt\">" . the_content_by_id($p->id) . "</span>";
        }
    }

    Just tested and it works fine. Credits to Ryan Burnette at TechStudio.co for his useful snippet to retrieve post's content by its ID.

    Enjoy!

  12. jpanizzoli
    Member
    Posted 1 year ago #

    YOU'RE THE MAN! Nice work! Will be "buying you a beer" shortly... got a few other questions for another post...

    Thanks again for figuring this out!

  13. Héctor Cabrera
    Member
    Plugin Author

    Posted 1 year ago #

    No problem! Glad I could help :)

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.