1

I am running in to an issue with making my own search loop in Wordpress. What I am trying to achieve is that certain circumstances display only certain information:

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

The issue that arises, is that is will always render one or two empty tags at the top and this ruins the style of it, as each div has a dotted border. Is there a way of telling Wordpress that if not of these conditions are met, then don't display the <div>?

Thanks in advance for any help!

JP

2 Answers 2

1

Rearrange your code so you compute the conditions before you output the content like so:

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

This allows you to detect if anything will be output and then uses a div where appropriate.

Sign up to request clarification or add additional context in comments.

Comments

0

Can you just move the <div> inside the conditional statements? Like so:

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

4 Comments

Ryan, I think I had a wood for trees moment! Thank you for your help!
Haha, trust me, I've been there. Hope this helped you out!
@user1872510 - Don't forget that you can mark a response as the answer ;)
Thanks Ryan, you are now my answer!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.