【问题标题】:Prevent Post Duplication防止后期重复
【发布时间】:2015-02-01 16:13:03
【问题描述】:

我负责管理这个网站F9 Properties ,它是用 WordPress 构建的。在主页上有一个特色属性部分。我注意到,如果您列出了具有两种不同“状态”(例如“待售”或“待租”)的属性,则该属性在轮播中出现了两次。下面是列出特色属性的代码。我可以看到它过滤掉了属性状态为“已出租”。任何人都可以帮我添加一些代码以在每个帖子中仅列出一个属性,而不管它有多少不同的属性状态?

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

【问题讨论】:

  • 您能否分享$featured_properties_queryWP_Query 部分?

标签: wordpress post filter


【解决方案1】:

我可能误解了您的设置,但我想知道您为什么要循环使用这些术语。

我认为您应该考虑排除 WP_Query() 部分中的 leased 术语(希望您可以分享)。

然后您的轮播将被简化为:

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

【讨论】:

    【解决方案2】:

    您可以在每次迭代发生时将帖子 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;
    

    【讨论】: