Wordpress循环问题

时间:2013-02-06 15:40:56

标签: php wordpress

我遇到了在Wordpress中进行自己的搜索循环的问题。我想要实现的是某些情况只显示某些信息:

<?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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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; ?>