<div>自动循环而无法控制它</div>

时间:2014-06-06 06:01:19

标签: php wordpress

上次我在我的代码中遇到了一些问题,到目前为止我已经修复了问题,但是还有一个问题就是出现问题。 在我的代码中,主<div>自动循环5次,显示5个timee的相同元素列表。我无法找出导致循环的原因。在检查元素时,我开始知道主循环,即<div class="row-fluid">循环5次。问题在哪里,我该如何解决? 真的需要帮助。

提前感谢你。

<?php if ($cat == 2900) { ?>
  <div class="row-fluid">
   <div class="span12 featured-block"> 
    <?php $block1Featured= new WP_Query( 'cat=2900' );
     if ( $block1Featured->have_posts() ) : ?>
      <?php while($block1Featured->have_posts()): $block1Featured->the_post(); ?>
        <div class="span6 ">
        <a class="featured_image" href="<?php the_permalink();?> "target="_blank">
         <?php if ((function_exists('has_post_thumbnail')) && (has_post_thumbnail())  ) { 
           $image_id = get_post_thumbnail_id(); 
           $image_url = wp_get_attachment_image_src($image_id,'large');
           $image_url = $image_url[0];
           if(!@getimagesize($image_url)){
           $image_url = "/wp-content/uploads/2014/02/logo2.jpg";
           } 
           $blogurl = get_bloginfo('url'); 
           $image_url = str_replace($blogurl, '', $image_url);
          ?>

           <img src="<?php echo bloginfo('template_url'); ?>/thumb.php?src=<?php echo ($image_url); ?>&amp;w=390&amp;h=216&amp;zc=1&amp;q=100" alt="<?php echo the_title(); ?>" />

         <?php }else{ ?> 


           <img src="<?php echo bloginfo('template_url'); ?>/thumb.php?src=<?php echo "http://www.nepaljapan.com/wp-content/uploads/2014/02/logo2.jpg"; ?>&amp;w=390&amp;h=216&amp;zc=1&amp;q=100" alt="<?php echo the_title(); ?>" />


         <?php }?>
        </a>

    <a href="<?php the_permalink();?>" target="_blank">
    <h2><?php the_title(); ?></h2></a>
    <p><?php echo excerpt(37); ?> </p>
    </div>
    <?php endwhile; ?>

    <?php wp_reset_postdata(); ?>
<?php else:  ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

  </div>
  </div>

<?php } ?>

<?php }else{ ?>
//else contents
<?php } ?>

1 个答案:

答案 0 :(得分:1)

试试这个

<?php

if ($cat == 2900)
    { ?>
  <div class="row-fluid">
   <div class="span12 featured-block"> 
    <?php
    $block1Featured = new WP_Query('cat=2900');
    if ($block1Featured->have_posts()): ?>
      <?php
        while ($block1Featured->have_posts()):
            $block1Featured->the_post(); ?>
        <div class="span6 ">
        <a class="featured_image" href="<?php
            the_permalink(); ?> "target="_blank">
         <?php
            if ((function_exists('has_post_thumbnail')) && (has_post_thumbnail()))
            {
            $image_id = get_post_thumbnail_id();
            $image_url = wp_get_attachment_image_src($image_id, 'large');
            $image_url = $image_url[0];
            if (!@getimagesize($image_url))
            {
            $image_url = "/wp-content/uploads/2014/02/logo2.jpg";
            }

            $blogurl = get_bloginfo('url');
            $image_url = str_replace($blogurl, '', $image_url);
?>

       <img src="<?php
            echo bloginfo('template_url'); ?>/thumb.php?src=<?php
            echo ($image_url); ?>&amp;w=390&amp;h=216&amp;zc=1&amp;q=100" alt="<?php
            echo the_title(); ?>" />

     <?php
            }
            else
            { ?> 


           <img src="<?php
                echo bloginfo('template_url'); ?>/thumb.php?src=<?php
                echo "http://www.nepaljapan.com/wp-content/uploads/2014/02/logo2.jpg"; ?>&amp;w=390&amp;h=216&amp;zc=1&amp;q=100" alt="<?php
                echo the_title(); ?>" />


         <?php
             } ?>
        </a>

    <a href="<?php
            the_permalink(); ?>" target="_blank">
    <h2><?php
            the_title(); ?></h2></a>
    <p><?php
            echo excerpt(37); ?> </p>
    </div>
    <?php
        endwhile; ?>

    <?php
        wp_reset_postdata(); ?>
<?php
    else: ?>
<p><?php
        _e('Sorry, no posts matched your criteria.'); ?></p>
<?php
    endif; ?>

  </div>
  </div>

<?php
    } ?>