1

我遇到了在 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>?

提前感谢您的帮助!

J.P

4

2 回答 2

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在适当的地方使用。

于 2013-02-06T15:51:10.380 回答
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; ?>
于 2013-02-06T15:50:51.180 回答