Closed evandiamond closed 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:
@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?
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
andthumbnail_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
}
}
?>
Perfecto. Thanks for the quick replies. You're a good man, Charlie Brown.
Right now I have code like this ...
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?I'd rather not show my custom widget rather than showing
Sorry. No data so far.
Appreciate your help in advance.