Hiding empty content in WordPress

Rick Wargo

Turns out that there is a lot of empty sidebar lists (<ul> elements) when there are no blog entries in a newly created WordPress blog. In creating my own theme (epic), I’ve tried to clean up those sections by checking to see if there are any posts. This turns out to be an easy task to accomplish.

The trick is to wrap the HTML in a PHP block that checks to see if any posts exist. This is done by the following PHP code:


global $wpdb;
$request = "SELECT ID FROM $wpdb->posts WHERE post_status = 'publish' AND post_password = '' LIMIT 0, 1";
$p = $wpdb->get_results($request);
if (function_exists('mdv_recent_posts') && (isset($p)) && (!is_archive() && !is_category())) {
?>

  • Sidebar List


  • In this example, the sidebar element, Sidebar List, will only appear if there is at least one post.

    Share and Enjoy:
    • Facebook
    • Twitter
    • LinkedIn
    • StumbleUpon
    • del.icio.us
    • Google Buzz
    • Yahoo! Buzz
    • Digg
    • email

    Leave a Reply

    stop spam with honeypot!