cabrerahector / wordpress-popular-posts

WordPress Popular Posts - A highly customizable WordPress widget that displays your most popular posts.
https://wordpress.org/plugins/wordpress-popular-posts/
GNU General Public License v2.0
279 stars 83 forks source link

Function exists, but are there posts? #271

Closed evandiamond closed 4 years ago

evandiamond commented 4 years ago

Right now I have code like this ...

<div class="frame">
    <?php if (function_exists('wpp_get_mostpopular')) : ?>
    <?php $args = array(...); ?>
    <?php wpp_get_mostpopular($args); ?>
    <?php endif; ?>
</div>

Would it be possible to put that <div class="frame"> inside the function exists so if no items are found, the frame around it doesn't show?

<?php if (function_exists('wpp_get_mostpopular')) : ?>
    <?php $args = array(...); ?>
    <?php if (has_posts) : ?> // something like this
        <div class="frame">
            <?php wpp_get_mostpopular($args); ?>
        </div>
    <?php endif; ?>
<?php endif; ?>

I'd rather not show my custom widget rather than showing Sorry. No data so far.

Appreciate your help in advance.

cabrerahector commented 4 years ago

Hi @evandiamond,

That would be possible if the wpp_get_mostpopular() function returned data but it doesn't. It echoes a list of popular posts or a "Sorry, no data so far" message if no posts match the selected criteria.

Possible workarounds:

evandiamond commented 4 years ago

@cabrerahector Thanks for the reply!

Would this be acceptable?

<?php
if (is_plugin_active('wordpress-popular-posts/wordpress-popular-posts.php')) {
    $popular_posts = new WordPressPopularPosts\Query($args);
    $results = $popular_posts->get_posts();
    foreach ($results as $popular_post) {
        $post = get_post($popular_post->id);
        setup_postdata($post);

        echo get_the_title();
    }
    wp_reset_postdata();
} ?>

I'm assuming things like post_html, thumbnail_height and thumbnail_width will not work when querying this way? Is that correct?

cabrerahector commented 4 years ago

Would this be acceptable?

Yep, looks good to me.

If you're using namespaces with your theme then you may need to add a backslash before the WordPressPopularPosts namespace or else you'll get a PHP error:

$popular_posts = \WordPressPopularPosts\Query($args);

If you aren't using namespaces with your theme then it won't be necessary.

I'm assuming things like post_html, thumbnail_height and thumbnail_width will not work when querying this way? Is that correct?

Yes, that's correct. If you're familiar with the WP_Query class then this is pretty much like it: the WordPressPopularPosts\Query class also returns an array of popular posts objects and that's it. It's up to you to build the HTML output however you like it using the data provided by the class.

So, following your original question:

<?php
if (is_plugin_active('wordpress-popular-posts/wordpress-popular-posts.php')) {
    $popular_posts = new WordPressPopularPosts\Query($args);
    $results = $popular_posts->get_posts();

    // We have some posts to show, let's build the list
    if ( is_array($results) && ! empty($results) ) {
        ?>
        <div class="frame">
            <?php
            foreach ($results as $popular_post) {
                $post = get_post($popular_post->id);
                setup_postdata($post);

                echo get_the_title();
            }
            wp_reset_postdata();
            ?>
        </div>
        <?php
    }
}
?>
evandiamond commented 4 years ago

Perfecto. Thanks for the quick replies. You're a good man, Charlie Brown.