Support » Developing with WordPress » How to properly hide div when if have_post is empty.

  • Resolved kirasiris


    Hello everyone, it may be absurd to even ask because hiding a div is easy to do on loops,etc. However this time I’m not doing a while loop, instead I’m doing something more like just a “check if” on my front-page file, and it is not working there.

    Here is my code:

    <!-- Page content -->
    <?php if(have_posts()) : the_post(); ?>
    <div class="container-fluid" id="kevin_container">
    <div class="row">
    <div class="container" id="kevin_page_container">
    <!------ Child Pages and Parent Page --->
    <?php if(page_is_parent() || $post->post_parent > 0) : ?>
    <div class="pagination pull-right">
    <span class="parent-link">
    <a href="<?php echo get_the_permalink(get_top_parent()); ?>"><?php echo get_the_title(get_top_parent()); ?></a>
    $args = array(
    'child_of' => get_top_parent(),
    'title_li' => ''
    <?php wp_list_pages($args); ?>
    <div class="clearfix"></div>
    <?php endif; ?>
    <?php the_content(); ?>
    <?php endif; ?>

    I would like to say that I’m trying to show content from a page, not a post.


Viewing 5 replies - 1 through 5 (of 5 total)
  • which div exactly are you trying to hide?

    as the code is now, all the divs in your html will be hidden if there is no page.

    I want to actually hide all divs that are in the code above, you can check my website to see what I’m talking about. It is supposed to be hidden(as right now has no content), right? unfortunately it is not.
    This is my website
    As you will see, theres a big empty div :/ .


    Hello @kirasiris,

    You can simply wrap all your code in below condition.

    global $post;
    // your code block

    Let, me know if there is anything.

    It’s not working :/ . It now hides all the div when there is content on it; what I want is to have hidden just when there is not.

    EDIT: It’s working now. Thanks

    • This reply was modified 2 years, 2 months ago by kirasiris.

    good luck 🙂

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to properly hide div when if have_post is empty.’ is closed to new replies.