2013-02-06 84 views
1

我遇到了在Wordpress中創建自己的搜索循環的問題。我想實現的是,某些情況下,只顯示某些信息:Wordpress Loop問題

<?php while (have_posts()) : the_post(); echo '<div>'; ?> 

     <?php if (in_category('property')) { ?> 
      <h3><?php the_title(); ?></h3> 
      <?php the_field('main-image-description'); ?> 
      <span class="launch"> 
       <a href="<?php the_permalink(); ?>" title="<?php printf(esc_attr__('Permalink to %s', 'boilerplate'), the_title_attribute('echo=0')); ?>" rel="bookmark"> 
        <span class="link">click to launch</span> 
        <span class="launch-icon"></span> 
       </a> 
      </span> 
     <?php } ?> 

     <?php if (in_category('all-developments')) { ?> 
      <h3><?php the_title(); ?></h3> 
      <?php the_field('property-description'); ?> 
      <span class="launch-brochure"> 
       <a href="<?php the_field('pdf-download-all-developments'); ?>" target="_blank"> 
        <span class="link">Download Brochure</span> 
        <span class="launch-icon"></span> 
       </a> 
      </span> 
     <?php } ?> 

     <?php if (is_page()) { ?> 
      <h3><?php the_title(); ?></h3> 
      <?php the_content(); ?> 
      <span class="launch"> 
       <a href="<?php the_permalink(); ?>" title="<?php printf(esc_attr__('Permalink to %s', 'boilerplate'), the_title_attribute('echo=0')); ?>" rel="bookmark"> 
        <span class="link">click to launch</span> 
        <span class="launch-icon"></span> 
       </a> 
      </span> 
     <?php } ?> 

    <?php echo '</div>'; endwhile; ?> 

即出現的問題,是永遠呈現在最上面的一個或兩個空標籤,這廢墟它的風格,每個div都有一個虛線邊框。有沒有告訴Wordpress如果不符合這些條件的方法,那麼不要顯示<div>

在此先感謝您的幫助!

JP

回答

0

你能只移動條件語句裏面<div>?像這樣:

<?php while (have_posts()) : the_post(); ?> 

    <?php if (in_category('property')) { ?> 
     <div> <!-- Open div here !--> 
     <h3><?php the_title(); ?></h3> 
     <?php the_field('main-image-description'); ?> 
     <span class="launch"> 
      <a href="<?php the_permalink(); ?>" title="<?php printf(esc_attr__('Permalink to %s', 'boilerplate'), the_title_attribute('echo=0')); ?>" rel="bookmark"> 
       <span class="link">click to launch</span> 
       <span class="launch-icon"></span> 
      </a> 
     </span> 
     </div> <!-- Close div here !--> 
    <?php } ?> 

    <?php if (in_category('all-developments')) { ?> 
     <div> <!-- Open div here !--> 
     <h3><?php the_title(); ?></h3> 
     <?php the_field('property-description'); ?> 
     <span class="launch-brochure"> 
      <a href="<?php the_field('pdf-download-all-developments'); ?>" target="_blank"> 
       <span class="link">Download Brochure</span> 
       <span class="launch-icon"></span> 
      </a> 
     </span> 
     </div> <!-- Close div here !--> 
    <?php } ?> 

    <?php if (is_page()) { ?> 
     <div> <!-- Open div here !--> 
     <h3><?php the_title(); ?></h3> 
     <?php the_content(); ?> 
     <span class="launch"> 
      <a href="<?php the_permalink(); ?>" title="<?php printf(esc_attr__('Permalink to %s', 'boilerplate'), the_title_attribute('echo=0')); ?>" rel="bookmark"> 
       <span class="link">click to launch</span> 
       <span class="launch-icon"></span> 
      </a> 
     </span> 
     </div> <!-- Close div here !--> 
    <?php } ?> 

<?php endwhile; ?> 
+0

瑞恩,我想我有一棵樹木的時刻!感謝您的幫助! – JohnRobertPett

+0

哈哈,相信我,我一直在那裏。希望這能幫助你! – Ryan

+0

@ user1872510 - 不要忘記,你可以標記回答作爲答案;) – Ryan

1

重新安排你的代碼,以便您在輸出之前計算的條件,像這樣的內容:

<?php 
    while (have_posts()) : the_post(); 

    $propertyCategory  = in_category('property'); 
    $allDevelopmentsCategory = in_category('all-developments'); 
    $isPage     = is_page(); 

    $output = ($propertyCategory || $allDevelopmentsCategory || $isPage); 

    if($output){ 
     echo '<div>'; 
    } 


?> 

    <?php if ($propertyCategory) { ?> 
     <h3><?php the_title(); ?></h3> 
     <?php the_field('main-image-description'); ?> 
     <span class="launch"> 
      <a href="<?php the_permalink(); ?>" title="<?php printf(esc_attr__('Permalink to %s', 'boilerplate'), the_title_attribute('echo=0')); ?>" rel="bookmark"> 
       <span class="link">click to launch</span> 
       <span class="launch-icon"></span> 
      </a> 
     </span> 
    <?php } ?> 

    <?php if ($allDevelopmentsCategory) { ?> 
     <h3><?php the_title(); ?></h3> 
     <?php the_field('property-description'); ?> 
     <span class="launch-brochure"> 
      <a href="<?php the_field('pdf-download-all-developments'); ?>" target="_blank"> 
       <span class="link">Download Brochure</span> 
       <span class="launch-icon"></span> 
      </a> 
     </span> 
    <?php } ?> 

    <?php if ($isPage) { ?> 
     <h3><?php the_title(); ?></h3> 
     <?php the_content(); ?> 
     <span class="launch"> 
      <a href="<?php the_permalink(); ?>" title="<?php printf(esc_attr__('Permalink to %s', 'boilerplate'), the_title_attribute('echo=0')); ?>" rel="bookmark"> 
       <span class="link">click to launch</span> 
       <span class="launch-icon"></span> 
      </a> 
     </span> 
    <?php } ?> 

    <?php 
    if($output){ 
     echo '</div>'; 
    } 

    endwhile; 
?> 

這使您可以檢測是否有任何將被輸出,然後使用div酌情。