Reputation: 95
I'm currently creating a theme for Wordpress but I've hit an roadblock so close to completion!
I'm using a custom comments field and it's wrapped in a container with styling to it. However this container is built like this:
Called to Main Template
<?php comments_template();?>
Inside comments.php which exists in my theme's directory
<div class="tweleve columns row mytheme-box mytheme-body mytheme-form">
<?php if (have_comments() ): ?>
<h4 id="comments"><?php comments_number('No Comments','One Comment','% Comments');?></h4>
<ol class="comment-list">
<?php wp_list_comments('callback=custom_comments');?>
</ol>
<?php endif;?>
<?php
$comments_args = array(
'label_submit'=>'Submit Comment',
'title_reply'=>'Leave us a comment!',
'comment_notes_after' => '',);
comment_form($comments_args); ?>
</div>
mytheme-box turns the div into a box with a small shadow, border-radius etc.
mytheme-body styles the text within
mytheme-form is unique styling to the form.
twelve columns row is the styling from the skeleton framework
Inside the functions.php file
function custom_comments ($comment, $args, $depth) {
$GLOBALS['comment'] = $comment; ?>
<li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
<div id="comment-<?php comment_ID(); ?>">
<header class="comment-header">
<?php echo get_avatar($comment,$size='48',$default='<path_to_url>'); ?>
<?php printf(__('<cite class="fn">%s</cite> <span class="says">says:</span>'), get_comment_author_link()) ?>
<div class="comment-meta commentmetadata"><a href="<?php echo htmlspecialchars( get_comment_link($comment->comment_ID)) ?>"><?php printf(__('%1$s at %2$s'), get_comment_date(), get_comment_time())?></a><?php edit_comment_link(__('(Modify)'),' ','')?></div>
</header>
<br>
<?php if ($comment->comment_approved == '0') : ?>
<em><?php _e('Your comment is pending approval.') ?></em>
<?php endif; ?>
<?php comment_text()?>
<div class="reply">
<?php comment_reply_link(array_merge($args, array('depth'=>$depth, 'max_depth'=> $args['max_depth']))) ?>
</div>
</div>
<hr>
<?php
}
This is all fine, I have no problem with any thing here. The only issue I have is I've had to create a div to wrap everything comments related in which is what wraps around the code in comments.php. I do this because I don't want to touch the /wp-includes files even though it includes comments_template.php which is where I'd like to stick mytheme-body, box and form styles as it's where it get's made.
The reason being is because when I go into wordpress and turn off comments for a page, I'm left with an empty box where the comments use to be, and this is highly annoying. Is there anything I could do to fix this?
A way to maybe assign additional classes just to one already created element? Can I totally make my own comments_template.php and not rely on the core one?
Upvotes: 0
Views: 467
Reputation: 10346
You should use another condition that checks if comments are enabled.
It can be easily achieved with the conditional tag comments_open
.
In your comments.php
file, above the opening div
add:
if(comments_open()):
//All of your code here
...
...
...
endif; //Use `else` if you want to show a notification.
Upvotes: 1