• I’m building my own theme from scratch but I need help! I have comments displaying not under the post, but on an aside floated right. The actual form is under the post. Comments work and look the way I want when there are comments, when there are no comments it looks very empty.

    In a perfect world, I would like the widget area to show up when there are no comments and when there are comments then obviously the widget area goes away replaced with the comments.

    The html:

    <aside class="col span-04 sidebar comments">
    	<div class="sidebar-frame">
    		<div class="widget-area">
    			<h3>Comments</h3>
    			<?php comments_template(); ?>
    		</div>
    	</div>
    </aside>

    FYI: all of this on single.php and it’s all viewable on my site: SaturdaythruSunday.com

Viewing 12 replies - 1 through 12 (of 12 total)
  • Thread Starter harshclimate

    (@harshclimate)

    Oh, I forgot to mention, too, that the code above is OUTSIDE the loop.

    Thread Starter harshclimate

    (@harshclimate)

    Still wondering about this if anyone can help. I know it’s not as simple as:

    if(has_comments_template()){
    echo comments_template();
    } else {
    echo sidebar();
    }
    Thread Starter harshclimate

    (@harshclimate)

    … still wondering …

    https://codex.www.ads-software.com/Function_Reference/have_comments

    that the code is outside the loop possibly is no problem when used in single.php;
    try:

    <?php if( have_comments() ) : ?>
    <aside class="col span-04 sidebar comments">
    	<div class="sidebar-frame">
    		<div class="widget-area">
    			<h3>Comments</h3>
    			<?php comments_template(); ?>
    		</div>
    	</div>
    </aside>
    <?php else: ?>
    <aside class="col span-04 sidebar no-comments">
    	<div class="sidebar-frame">
    		<div class="widget-area">
    			<?php dynamic_sidebar(); ?>
    		</div>
    	</div>
    </aside>
    <?php endif; ?>

    [PS: edited to add the missing endif code]

    to use a CSS class .no-comments in the code instead of .comments depends on the existing CSS and your idea of formatting.

    the exact code for the dynamic_sidebar() depends on how the sidebar is registered in your theme.
    https://codex.www.ads-software.com/Function_Reference/dynamic_sidebar

    Thread Starter harshclimate

    (@harshclimate)

    Thanks for your response, Michael. I did add in what you had there and it did throw an error, but I just added <?php endif; ?> after the last </aside>. Nothing is showing up… not even the sidebar which is <?php dynamic_sidebar( 'widget-area-main' ); ?>

    No errors are showing at this point, but no content is either. Am I missing something?

    Thread Starter harshclimate

    (@harshclimate)

    After further review, I viewed source and I see that the widgets are showing up, but not being displayed on the page. Weird! Do I need to echo it or something?

    Here’s what I added:

    <?php if( have_comments() ) : ?>
    <aside class="col span-04 sidebar comments">
    	<div class="sidebar-frame">
    		<div class="widget-area">
    			<h3>Comments</h3>
    			<?php comments_template(); ?>
    		</div>
    	</div>
    </aside>
    <?php else: ?>
    <aside class="col span-04 sidebar no-comments">
    	<div class="sidebar-frame">
    		<div class="widget-area">
    			<?php echo dynamic_sidebar( 'widget-area-main' ); ?>
    		</div>
    	</div>
    </aside>
    <?php endif; ?>
    Thread Starter harshclimate

    (@harshclimate)

    Okay, sorry for the spam! I think it’s working. Like a dork, I didn’t scroll all the way down the page. It wasn’t where it was supposed to be so I assumed it wasn’t working cuz I couldn’t see it ??

    It’s working now and got in the right area. Thanks dood ??

    Thread Starter harshclimate

    (@harshclimate)

    I posted a comment and it’s not showing up… or it’s not replacing the widgets…

    my mistake, I didn’t read the linked Codex page https://codex.www.ads-software.com/Function_Reference/have_comments properly:

    Warning: this function will always return “false” until after comments_template has been called. If you need to check for comments before calling comments_template, use get_comments_number instead.

    i.e., try working with https://codex.www.ads-software.com/Template_Tags/get_comments_number

    <?php if( get_comments_number() ) : ?>

    Thread Starter harshclimate

    (@harshclimate)

    Sorry, I’m such a noob when it comes to php. I’m not exactly sure where I’m supposed to insert that snippet. I’ve tried numerous positions in that conditional and can’t seem to trigger the comments to show. I know the warning says before calling the comments comments_template() but nothing happened.

    Sorry about this :/

    I’m not exactly sure where I’m supposed to insert that snippet.

    apologies for not explaining this in detail.

    simply replace the first line of the previous code, i.e. instead of:

    <?php if( have_comments() ) : ?>

    use the new line:

    <?php if( get_comments_number() ) : ?>

    Thread Starter harshclimate

    (@harshclimate)

    Thanks, it’s working ?? I’m just trying to get the format to work correctly now. I guess it’s not being placed in my theme right so I’m trying to figure out what’s going on there. It’s got nothing to do with php but my own mistakes, i’m sure.

    Much appreciated, sir!

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Conditional Statement for Showing Comments’ is closed to new replies.