dongyupen6269 2015-06-27 13:07
浏览 51
已采纳

使用PHP将类添加到元素

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?

  • 写回答

1条回答 默认 最新

  • duanchi5078 2015-06-27 13:15
    关注

    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.
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度