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 に点線の境界線があるため、常に上部に 1 つまたは 2 つの空のタグがレンダリングされ、スタイルが台無しになることです。これらの条件が満たされていない場合は表示しないようにWordpressに伝える方法はあり<div>ますか?

助けてくれてありがとう!

JP

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 に答える