programing

포스트 복제 방지

minimums 2023. 3. 14. 21:31
반응형

포스트 복제 방지

저는 WordPress에 내장된 이 사이트 F9 Properties의 관리를 담당하고 있습니다.홈페이지에는 특집 속성 섹션이 있습니다."판매용" 또는 "임대용"과 같이 두 개의 다른 "상태"를 가진 부동산을 나열하면 회전목마차에 해당 부동산이 두 번 표시됩니다.다음은 특장 속성을 나열하기 위한 코드입니다.상태가 "Leased"인 속성을 필터링할 수 있습니다.다른 속성 상태에 관계없이 게시물당 하나의 자산만 나열할 수 있도록 코드를 추가할 수 있는 사람 있나요?

<?php
/* Featured Properties Query Arguments */
$featured_properties_args = array(
'post_type' => 'property',
'posts_per_page' => 100,
'meta_query' => array(
    array(
        'key' => 'REAL_HOMES_featured',
        'value' => 1,
        'compare' => '=',
        'type'  => 'NUMERIC'
    )
)
);

$featured_properties_query = new WP_Query( $featured_properties_args );

if ( $featured_properties_query->have_posts() ) :
?>
<section class="featured-properties-carousel clearfix">
    <?php
    $featured_prop_title = get_option('theme_featured_prop_title');
    $featured_prop_text = get_option('theme_featured_prop_text');

    if(!empty($featured_prop_title)){
        ?>
        <div class="narrative">
           <h3><?php echo $featured_prop_title; ?></h3>
            <?php
            if(!empty($featured_prop_text)){
                ?><p><?php echo $featured_prop_text; ?></p><?php
            }
            ?>

        </div>
        <?php
    }

    ?>

       <div class="carousel es-carousel-wrapper">
        <div class="es-carousel">
            <ul class="clearfix">
                <?php
                while ( $featured_properties_query->have_posts() ) :
                    $featured_properties_query->the_post();
                    ?>

                    <?php
                $status_terms = get_the_terms( $post->ID,"property-status" );
                if(!empty( $status_terms )){
                    foreach( $status_terms as $status_term ){

                       if($status_term->name=="Leased"){}else{

                           ?>
                           <li>
                        <figure>
                            <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                                <?php
                                the_post_thumbnail('property-thumb-image',array(
                                    'alt'   => get_the_title($post->ID),
                                    'title' => get_the_title($post->ID)
                                ));
                                ?>
                            </a>
                        </figure>
                        <h4><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h4>
                        <p><?php framework_excerpt(8); ?> <a href="<?php the_permalink() ?>"> <?php _e('Know More','framework'); ?> </a> </p>
                        <span class="price"><?php property_price(); ?></span>

                    </li>
                           <?
                       }


                    }
                }
                ?>

                    <?php
                endwhile;
                wp_reset_query();
                ?>
            </ul>
        </div>
    </div>

내가 너의 설정을 오해하고 있는 것 같은데, 왜 너는 그 조건을 반복하고 있는지 궁금하다.

대신에, 그 대신에, 그 대신,leased의 범위 내 용어WP_Query()part (공유 가능)

그러면 회전목마가 다음과 같이 단순해집니다.

<div class="carousel es-carousel-wrapper">
    <div class="es-carousel">
        <ul class="clearfix">
        <?php while ( $featured_properties_query->have_posts() ) : $featured_properties_query->the_post(); ?>
            <li><!-- YOUR POST ITEM HERE --></li>
        <?php endwhile; ?>
        </ul>
    </div>
</div>

반복이 발생할 때마다 게시 ID를 배열에 추가하고 게시물이 이미 렌더링되었는지 어레이를 확인할 수 있습니다.

$shown = array(); // new array
while ( $featured_properties_query->have_posts() ) :
    $featured_properties_query->the_post();
    $status_terms = get_the_terms( $post->ID, 'property-status' );
    if( ! empty( $status_terms ) ){
        foreach( $status_terms as $status_term ){
            if( $status_term->name == "Leased" || in_array( $post->ID, $shown ){
                continue; // post has term "Leased" or already rendered, skip
            }
            $shown[] = $post->ID; // add post ID to array
        ?>
            <!-- HTML here -->
        <?php
        }
    }
endwhile;

언급URL : https://stackoverflow.com/questions/27282897/prevent-post-duplication

반응형