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
280 stars 82 forks source link

How to hide wpp block when no-data? #295

Closed NgKhanh closed 2 years ago

NgKhanh commented 3 years ago

Describe the solution you'd like When no data, it show block with class wpp-no-data, I can use css to hide this block. But I can't hide header of block

Describe alternatives you've considered I hope we can add custom class to block to header and block body, and add header title ínside container

<div class="custom-d-none">
<h3 class="wpp-header">Related posts</h3>
<ul class="wp-list">
</ul>
</div>
cabrerahector commented 3 years ago

Hi @NgKhanh,

This is a bit of a "hack" but it should work: you can use the wpp_no_data filter hook like this to hide the entire container from view (assuming you're using the widget of course):

/**
 * Hide WPP's widget if there's nothing to show.
 *
 * @param  string $no_data_html
 * @return string
 */
function my_custom_no_posts_found( $no_data_html ){ 
    $output = '<style>.popular-posts { display:none; }</style>';
    return $output;
}
add_filter('wpp_no_data', 'my_custom_no_posts_found', 10, 1);

You need to add this code snippet to your theme's functions.php file for it to work.

And regarding your question about how to customize the HTML code please have a look at this FAQ: How can I use my own HTML markup with WordPress Popular Posts?