Wordpress循环中的复制问题

时间:2015-11-16 06:56:50

标签: php wordpress

我现在在网站上工作。我正在创建一个团队页面,显示从管理员后端发布的团队成员。我为此成功创建了一个循环。我还为电话,姓名等创建了自定义字段。我发布第一个团队成员留下电话字段空白,然后我发布了第二个团队成员填写电话字段。令我惊讶的是,我填写的第二个团队成员的电话号码显示在第一个。如果留空,这也适用于其他字段。我在网上搜索并尝试了很多解决方案,但它们无法正常工作。我将在下面对我的代码进行坦诚的审查表示赞赏:

<?php
  $query = new WP_Query( array('post_type' => 'specialist') );
  while ( $query->have_posts() ) : $query->the_post();
  $do_not_duplicate = $post->ID; //This is the magic line

     $meta = get_post_custom();
      if($meta['full_name'][0]) {
       $name = $meta['full_name'][0];
      }

      if($meta['designation'][0]) {
       $title = $meta['designation'][0];
      }

      if($meta['email_address'][0]) {
       $mail = $meta['email_address'][0];
      }

      if($meta['facebook'][0]) {
       $fb = $meta['facebook'][0];
      }

      if($meta['twitter'][0]) {
       $tw = $meta['twitter'][0];
      }

      if($meta['gplus'][0]) {
       $googlep = $meta['gplus'][0];
      }

      if($meta['telephone_no'][0]) {
       $tel = $meta['telephone_no'][0];
      }
  ?>
  <div class="grid_4">

    <div class="team-img-big">
      <a href="<?php echo wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>" data-imagelightbox="a"><?php the_post_thumbnail( 'full', array('class' => 'team-pix-big') ); ?></a>
    </div>

    <p class="team-name"><?php echo($name); ?></p>
    <p class="team-designation"><?php echo($title); ?></p>
    <p class="team-tel"><span class="fa fa-phone" style="padding-right:5px"></span><?php echo($tel); ?></p>


      <?php the_content() ?>

      <div class="team-social-big">
      <a href="<?php echo ($fb);?>"><span class="fa fa-facebook-square" style="padding-left:5px"></span></a>
      <a href="<?php echo($tw); ?>"><span class="fa fa-twitter-square" style="padding-left:5px"></span></a>
      <a href="<?php echo($googlep);?>"><span class="fa fa-google-plus-square" style="padding-left:5px"></span></a>
      <a href="mailto:<?php echo($mail);?>"><span class="fa fa-envelope" style="padding-left:5px"></span></a>
      </div>

  </div>
  <?php endwhile; ?>
  <?php   wp_reset_query(); ?>

以下是我的截图 enter image description here

电话号码只能在第二篇文章中显示,而不是两者都

2 个答案:

答案 0 :(得分:2)

如前所述,当字段为空或未设置时,您的代码中会出现一些未定义变量的错误(这也是一个错误)。如您所见,这将导致意外输出。

  • 调用$post全局安全

您可以在此处执行以下操作

  • 清理并验证字段。这是非常重要。永远不要在代码中留下任何后门。您必须始终清理所有用户提交的信息(,如$_GET$_POST和表单字段数据)。不这样做会导致恶意鳕鱼注入您的网站。不清理数据是被黑网站的首要原因

  • 使用默认值设置变量,如果条件失败,将使用该值。始终使用代码 失败的midset进行编码。通过这种方式,您可以消除错误

让我们来看看代码应该是什么样子

// Invoke the $post global
global $post;

// Get the post meta for the particular post
$meta = get_post_meta( $post->ID );

// Now we can get our values, sanitize them and set defaults
// $variable_name = (check if field isset) ? (sanitize field because it exist) : (set default, field does not exist);
$name  = isset( $meta['full_name'][0] )     ? filter_var( $meta['full_name'][0],     FILTER_SANITIZE_STRING ) : '';
$title = isset( $meta['designation'][0] )   ? filter_var( $meta['designation'][0],   FILTER_SANITIZE_STRING ) : '';
$mail  = isset( $meta['email_address'][0] ) ? filter_var( $meta['email_address'][0], FILTER_VALIDATE_EMAIL  ) : '';    
// etc, use the correct filter for the specific field, see http://php.net/manual/en/filter.filters.php

现在,您可以安全地使用变量,因为它们已经过清理,验证并且如果没有设置字段,则会有一个默认值(,这是一个空字符串)。

最后一个注意事项,您应该在循环后使用wp_reset_postdata(),而不是wp_reset_query(),后者与query_posts一起使用,您永远不应该使用

答案 1 :(得分:0)

我能够通过@Noman建议修复此问题,将所有变量定义为空。这是经过调整的代码:

             <?php
                    $query = new WP_Query( array('post_type' => 'specialist') );
                    while ( $query->have_posts() ) : $query->the_post();
                    $do_not_duplicate = $post->ID; //This is the magic line

                         $meta = get_post_custom();
                          if($meta['full_name'][0]) {
                           $name = $meta['full_name'][0];
                          }

                          if($meta['designation'][0]) {
                           $title = $meta['designation'][0];
                          }

                          if($meta['email_address'][0]) {
                           $mail = $meta['email_address'][0];
                          }

                          if($meta['facebook'][0]) {
                           $fb = $meta['facebook'][0];
                          }

                          $tw = ''; //(new) declare this empty to avoid dupliaction
                          if($meta['twitter'][0]) {
                           $tw = $meta['twitter'][0];
                          }

                          if($meta['gplus'][0]) {
                           $googlep = $meta['gplus'][0];
                          }

                          $tel = ''; //declare this empty to avoid duplication
                          if($meta['telephone_no'][0]) {
                           $tel = $meta['telephone_no'][0];
                          }
                    ?>
                    <div class="grid_4">

                        <div class="team-img-big">
                          <a href="<?php echo wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>" data-imagelightbox="a"><?php the_post_thumbnail( 'full', array('class' => 'team-pix-big') ); ?></a>
                        </div>

                        <p class="team-name"><?php echo($name); ?></p>
                        <p class="team-designation"><?php echo($title); ?></p>
                        <p class="team-tel"><span class="fa fa-phone" style="padding-right:5px"></span><?php echo($tel); ?></p>


                          <?php the_content() ?>

                          <div class="team-social-big">
                            <a href="<?php echo ($fb);?>"><span class="fa fa-facebook-square" style="padding-left:5px"></span></a>
                            <a href="<?php echo($tw); ?>"><span class="fa fa-twitter-square" style="padding-left:5px"></span></a>
                            <a href="<?php echo($googlep);?>"><span class="fa fa-google-plus-square" style="padding-left:5px"></span></a>
                            <a href="mailto:<?php echo($mail);?>"><span class="fa fa-envelope" style="padding-left:5px"></span></a>
                          </div>

                    </div>
                    <?php endwhile; ?>
                    <?php   wp_reset_query(); ?>

感谢@Noman的建议