wordpress如何创建metaboxes不利用插件

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi elementum aliquam lobortis. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nunc commodo porta mauris vitae sollicitudin. Sed non malesuada lacus. Vestibulum nec nibh condimentum, rhoncus ligula eget, venenatis mauris. Morbi egestas, mauris sed volutpat luctus, ante nibh aliquet ex, at posuere urna libero sit amet diam. Sed egestas cursus tortor sed tincidunt. Sed commodo tellus sit amet bibendum fringilla. Nam metus ligula, feugiat a erat ut, feugiat egestas risus.

Suspendisse potenti. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris venenatis lectus ut dolor feugiat, at placerat orci volutpat. Cras malesuada ex et aliquet scelerisque. Vestibulum sit amet diam eget mauris cursus mollis. Sed ac elementum tortor, id porttitor tortor. Nullam tortor justo, pretium rutrum pulvinar nec, lacinia dignissim purus. Ut vulputate ex sed cursus ullamcorper. Etiam volutpat tortor odio, eget hendrerit massa mattis in. Nulla ligula mauris, laoreet nec augue eu, faucibus vulputate magna. Cras arcu nunc, venenatis ut ipsum non, suscipit ultrices tortor. Phasellus nec aliquet turpis, sed cursus sapien. Aliquam sagittis aliquet pulvinar. Fusce congue euismod ante non tempor. Proin sit amet lectus sit amet sapien accumsan dictum. Etiam dapibus maximus tempor.

WordPress comments part-1

WordPress的comment form是以comments.php文件作为基础的,没有这个文件无法调取comment函数。

首先在目录下创建comments.php文件,然后在single.php里comment的位置上,用函数调取comments form的内容:

if (comments_open(  ) || get_comments_number(  ) ) {
                comments_template();  // 调取 comments.php 
              }

comments_open( )来检查是否帖子开启了comments功能,每个帖子都可以在帖子编辑页面文档=》讨论区里设置是否单独启用comment.

接下来我们编辑comments.php的模板,首先在头部添加一段代码,来检测是否帖子设置了密码保护,如果是,就不显示comments的内容:

<?php if (post_password_required(  )) {
  return;
} ?>

通过comment_form() 函数来调用评论时候的表格,wordpress也设置好了一切检验表格的机制,comment_form用法:

comment_form( <em>array</em> $args = array(), <em>int|WP_Post</em> $post_id = null )

函数最主要有几个数组组成:‘comment_field’‘must_log_in’‘logged_in_as’

‘title_reply’‘title_reply_to’‘label_submit’‘submit_button’‘submit_field’,‘field’等组成,field又由下级数组组成:‘author’’email’‘url’‘cookies’等。comment_form里可以直接粘贴html,完整用法举例:

comment_form(
          array(
                      //comment body
                      'comment_field'  => '<div class="clear"></div>

                    <div class="col_full">
                      <label>Comment</label>
                      <textarea name="comment" cols="58" rows="7" class="sm-form-control"></textarea>
                    </div>',
                    //all the fields availbel inthe comments form
                    
                    'fields'  => array(
                      'author'  => '<div class="col_one_third">
                      <label>Name</label>
                      <input type="text" name="author" class="sm-form-control" />
                    </div>',
                      'email'  => '<div class="col_one_third">
                      <label>Email</label>
                      <input type="text" name="email" class="sm-form-control" />
                    </div>',
                      'url'  => '<div class="col_one_third col_last">
                      <label>Website</label>
                      <input type="text" name="url" class="sm-form-control" />
                    </div>',


                    ),
                    'class_submit'  => 'button button-3d nomargin',  //submit button class
                    'label_submit'  => __( 'Submit Comment', 'udemy' ),
                    'title_reply'  => __( 'Leave a <span>Comment</span>', 'udemy' ),


                    )

                  );

WordPress把所有comments存储在$comments的变量中,要把他们全部都展现出来,可以通过foreach来遍历,循环和标签的用法如下:

<?php if (have_comments()): ?>
                  <h3 id="comments-title"><span><?php comments_number(); ?></span></h3>

                <!-- Comments List
                                ============================================= -->
                <ol class="commentlist clearfix">

                  <?php foreach ($comments as $comment ): ?>
                  <li class="comment even thread-even depth-1" id="li-comment-1">

                    <div id="comment-1" class="comment-wrap clearfix">

                      <div class="comment-meta">

                        <div class="comment-author vcard">

                          <span class="comment-avatar clearfix">
                           <?php echo get_avatar( $comment, 60, '', '', array('class'  => 'avatar avatar-60 photo avatar-default') ); ?>
                          </span>

                        </div>

                      </div>

                      <div class="comment-content clearfix">

                        <div class="comment-author">
                          <?php comment_author(  ); ?>
                          <span><?php comment_date() ?></span>
                        </div>

                        <?php comment_text(); ?>

                      </div>

                      <div class="clear"></div>

                    </div>

                  </li>
                  <?php endforeach; the_comments_pagination(  ); ?>

                </ol><!-- .commentlist end -->
                <?php endif ?>

同时要注意if条件的使用,设置如果没有开启comment或者comments数量为0时不展现comment form模板。

Comment的reply功能,我们会在下一篇教程中讲到。