programing

WP_Query 개체가 비어 있는지 테스트

codeshow 2023. 10. 29. 20:03
반응형

WP_Query 개체가 비어 있는지 테스트

WP_Query 개체가 일치 항목을 반환하지 않는지 테스트하려면 어떻게 해야 합니까?템플릿과 같은 작업을 수행할 수 있으면 좋겠습니다.

<?php
    $my_query = new WP_Query(array('post_type' => 'biographies'))
    if( ***HOW DO I TEST $my_query HERE*** ) {
        //if $my_query finds anything loop through it here
    } else {
        //if $my_query does not find anything            
    }
?>

편집

더 좋은 예로, 쿼리에서 다음과 같은 것이 발견되는 경우에만 h2를 표시하고자 합니다.

<?php
    $outside_leasing_query = new WP_Query(array( 'post_type' => 'resin_biographies', 'tax_query' => array(
        'relation' => 'AND',
        array( 'taxonomy' => 'resin_buildings', 'field' => 'slug', 'terms' => $page_slug ),
        array( 'taxonomy' => 'resin_leasing_companies', 'field' => 'slug', 'terms' => 'rubenstein-partners', 'operator' => 'NOT IN' )
    ) )); // resin_buildings taxonomy term slug must match page slug
?>

<h2>Outside Leasing Contacts</h2>

<?php while ( $outside_leasing_query->have_posts() ) : $outside_leasing_query->the_post(); ?>

    <article <?php post_class('group'); ?>>
        <?php
        if( get_post_meta( $post->ID, '_biography_headshot', true ) != '' ) {
            echo '<img class="contact-thumb" src="' . get_post_meta( $post->ID, '_biography_headshot', true ) . '" alt="'. get_the_title() .'" />';
        } else {
            echo '<img class="contact-thumb-placeholder" src="' . get_bloginfo('template_url') . '/images/default_headshot.jpg" alt="'. get_the_title() . '" />';
        }
        ?>
        <div class="contact-info">
            <hgroup>
                <?php the_title( '<h3>', '</h3>' ); ?>
                <h4 class="contact-title"><?php echo get_post_meta( $post->ID, '_biography_title', true ); ?></h4>
            </hgroup>
            <div class="contact-address"><?php echo wpautop( get_post_meta( $post->ID, '_biography_address', true ) ); ?></div>
            <div class="contact-tel"><span>T</span> <?php echo get_post_meta( $post->ID, '_biography_tel', true ); ?></div>
            <?php if( get_post_meta( $post->ID, '_biography_fax', true ) != '' ) { ?>
                <div class="contact-fax"><span>F</span> <?php echo get_post_meta( $post->ID, '_biography_fax', true ); ?></div>
            <?php } ?>
            <div class="contact-email"><a href="mailto:<?php echo get_post_meta( $post->ID, '_biography_email', true ); ?>"><?php echo get_post_meta( $post->ID, '_biography_email', true ); ?></a></div>
        </div>
    </article>

<?php endwhile; ?>
<?php wp_reset_postdata(); ?>

표준 워드프레스 루프는 have_posts()를 사용하여 이 작업을 수행합니다.

<?php
$my_query = new WP_Query(array('post_type' => 'biographies'));
if($my_query->have_posts()) : while($my_query->have_posts()) : $my_query->the_post();
?>
<!-- YOUR POST(S) -->
<?php endwhile; else : ?>
<p>No posts found</p>
<?php endif; ?>

다음을 사용할 수 있을 것 같습니다.

!($my_query->have_posts())

그래서:

<?php
    $my_query = new WP_Query(array('post_type' => 'biographies'))
    if( !($my_query->have_posts())) {
        //if $my_query finds anything loop through it here
    } else {
        //if $my_query does not find anything            
    }
?>

첫 번째 예에서는 다음과 같이 설명합니다.

//if $my_query does not find anything

다음과 같은 매개 변수를 설정합니다.

$my_query_found_something = 'not';

그런 다음 필요할 때 어디서나 사용하십시오.

if ($my_query_found_something == 'not') {
// keep looking
}

언급URL : https://stackoverflow.com/questions/10952442/test-if-wp-query-object-is-empty

반응형