• Hi all, I’m doing pretty well with making my own theme having started with BlankSlate and now adding all my own CSS. One thing bugging me:

    When a post or page has comments turned off; it still outputs this line:
    <div id="comments"></div>

    I have styled that div#comments with a padding and border etc; so even if the comments feature is disabled I get an empty 20px high blank box based on my padding of 10px.

    Shouldn’t that div be taken away entirely when comments are disabled?

    You can see the problem on my test site: lars mullen dot co dot uk forward slash test. It’s on all the pages except the Gallery page which is a category page. See useless rounded box below the content.

    What am I doing wrong?
    Cheers…

Viewing 5 replies - 1 through 5 (of 5 total)
  • you possibly need to edit comments.php of your theme and move the conditional statement, which checks for open comments, to before the div and after the closing div.
    could be more complex than that…

    to get some suggestions, paste the code of comments.php into a https://pastebin.com/ and post the link to it here – how-to

    Shouldn’t that div be taken away entirely when comments are disabled?

    depends if existing comments are still to be shown…

    Thread Starter sonic1243

    (@sonic1243)

    Thanks Sweeper.

    I understand what you are saying but upon having a play I think this needs a new php IF statement wrapping the whole div#comments, something like:

    IF comments=enabled THEN display=true
    ELSE display=false

    Can you tell I’m not a php coder?! Anyone got a line for that?

    But, either way, surely I shouldn’t need to hack the php for something so integral to WP? Surely anybody who has put CSS styling on the div#comments will be in the same situation?

    Cheers,
    Chris

    Thread Starter sonic1243

    (@sonic1243)

    depends if existing comments are still to be shown

    That option isn’t available by default in WP is it?
    All I see is Allow Comments on/off, in Quick Edit.

    I know I could make a new template page to solve this, but that’s a work around – I’m looking to understand WP properly, and surely with this comments show/hide problem I’m missing something very simple?

    [bumping – never on a sunday]

    I found a simple work round for this. Just update your comments.php file to the following. All it does is move the div#comments into the if statement. This will stop the div#comments showing when comments are turned off ;o)

    <?php
    /**
     * The template for displaying Comments.
     *
     * The area of the page that contains both current comments
     * and the comment form.  The actual display of comments is
     * handled by a callback to twentyten_comment which is
     * located in the functions.php file.
     *
     * @package WordPress
     * @subpackage Twenty_Ten
     * @since Twenty Ten 1.0
     */
    ?>
    
    <?php if ( post_password_required() ) : ?>
    			<div id="comments">
    				<p class="nopassword"><?php _e( 'This post is password protected. Enter the password to view any comments.', 'twentyten' ); ?></p>
    
    <?php
    		/* Stop the rest of comments.php from being processed,
    		 * but don't kill the script entirely -- we still have
    		 * to fully load the template.
    		 */
    		return;
    	endif;
    ?>
    
    <?php
    	// You can start editing here -- including this comment!
    ?>
    
    <?php if ( have_comments() ) : ?>
    			<div id="comments">
    			<h3 id="comments-title"><?php
    			printf( _n( 'One Response to %2$s', '%1$s Responses to %2$s', get_comments_number(), 'twentyten' ),
    			number_format_i18n( get_comments_number() ), '<em>' . get_the_title() . '</em>' );
    			?></h3>
    
    <?php if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) : // Are there comments to navigate through? ?>
    			<div class="navigation">
    				<div class="nav-previous"><?php previous_comments_link( __( '<span class="meta-nav">&larr;</span> Older Comments', 'twentyten' ) ); ?></div>
    				<div class="nav-next"><?php next_comments_link( __( 'Newer Comments <span class="meta-nav">&rarr;</span>', 'twentyten' ) ); ?></div>
    			</div> <!-- .navigation -->
    <?php endif; // check for comment navigation ?>
    
    			<ol class="commentlist">
    				<?php
    					/* Loop through and list the comments. Tell wp_list_comments()
    					 * to use twentyten_comment() to format the comments.
    					 * If you want to overload this in a child theme then you can
    					 * define twentyten_comment() and that will be used instead.
    					 * See twentyten_comment() in twentyten/functions.php for more.
    					 */
    					wp_list_comments( array( 'callback' => 'twentyten_comment' ) );
    				?>
    			</ol>
    
    <?php if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) : // Are there comments to navigate through? ?>
    			<div class="navigation">
    				<div class="nav-previous"><?php previous_comments_link( __( '<span class="meta-nav">&larr;</span> Older Comments', 'twentyten' ) ); ?></div>
    				<div class="nav-next"><?php next_comments_link( __( 'Newer Comments <span class="meta-nav">&rarr;</span>', 'twentyten' ) ); ?></div>
    			</div><!-- .navigation -->
    <?php endif; // check for comment navigation ?>
    </div><!-- #comments -->
    <?php else : // or, if we don't have comments:
    
    	/* If there are no comments and comments are closed,
    	 * let's leave a little note, shall we?
    	 */
    	if ( ! comments_open() ) :
    ?>
    	<p class="nocomments"><?php _e( 'Comments are closed.', 'twentyten' ); ?></p>
    <?php endif; // end ! comments_open() ?>
    
    <?php endif; // end have_comments() ?>
    
    <?php comment_form(); ?>
    Thread Starter sonic1243

    (@sonic1243)

    Hi Lee, thanks for that, I can see what you have done and it worked straight away : )

    Further to that I added this line to the style.css:
    p.nocomments {display: none;}

    Ideal, cheers Lee.

    ——————
    EDIT: Oops spoke too soon, something not right:

    The div#comments isn’t showing up unless there is a comment, otherwise the ‘add comment’ box is unstyled. Then if there is a comment I’ve got this:
    “Warning: call_user_func() expects parameter 1 to be a valid callback, function ‘twentyten_comment’ not found or invalid function name in /home/username/public_html/test/wp-includes/comment-template.php on line 1336”

    This has to be do because I am not using the twentyten theme.

    I’ve run out of time today, gotta go, but will investigate further asap. Cheers…

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘CSS: div#comments shows when comments are off’ is closed to new replies.