我正在使用这个网站:http://f9properties.com/,在主页上,它有一个"特色属性"区域,它具有所有这些定义属性的不同属性,包括"属性类型","属性城市"和"属性状态"。在每个属性中,在WordPress仪表板中,有一种方法可以检查您是否希望该属性被推荐并显示在主页上。
问题是,如果该物业具有多种类型的状态,在这种情况下,例如"待售"和"待租",则该物业在"特色物业"区域中显示两次。据我所知,特色区域的代码是这样的:
function Featured_Properties_Widget(){
$widget_ops = array( 'classname' => 'Featured_Properties_Widget', 'description' => __('Displays Random or Recent Featured Properties','framework') );
$this->WP_Widget( 'Featured_Properties_Widget', __('RealHomes - Featured Properties','framework'), $widget_ops );
}
function widget($args, $instance) {
extract($args);
$title = apply_filters('widget_title', $instance['title']);
if ( empty($title) ) $title = false;
$count = intval( $instance['count']);
$sort_by = $instance['sort_by'];
$featured_args = array(
'post_type' => 'property',
'posts_per_page' => $count,
'meta_query' => array(
array(
'key' => 'REAL_HOMES_featured',
'value' => '1',
'compare' => 'LIKE'
)
)
);
//Order by
if($sort_by == "random"):
$featured_args['orderby']= "rand";
else:
$featured_args['orderby']= "date";
endif;
$featured_query = new WP_Query($featured_args);
echo $before_widget;
if($title):
echo $before_title;
echo $title;
echo $after_title;
endif;
if($featured_query->have_posts()):
?>
<ul class="featured-properties">
<?php
while($featured_query->have_posts()):
$featured_query->the_post();
?>
<li>
<?php
if(has_post_thumbnail()){
?>
<figure>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('grid-view-image');?>
</a>
</figure>
<?php
}
?>
<h4><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h4>
<p><?php framework_excerpt(7); ?> <a href="<?php the_permalink(); ?>"><?php _e('Read More','framework'); ?></a></p>
<span class="price"><?php property_price(); ?></span>
</li>
<?php
endwhile;
?>
</ul>
<?php
wp_reset_query();
else:
?>
<ul class="featured-properties">
<?php
echo '<li>';
_e('No Featured Property Found!', 'framework');
echo '</li>';
?>
</ul>
<?php
endif;
echo $after_widget;
}
function form($instance)
{
$instance = wp_parse_args( (array) $instance, array( 'title' => 'Featured Properties', 'count' => 1 , 'sort_by' => 'random' ) );
$title= esc_attr($instance['title']);
$count = $instance['count'];
$sort_by = $instance['sort_by'];
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Widget Title', 'framework'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('count'); ?>"><?php _e('Number of Properties', 'framework'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>" type="text" value="<?php echo $count; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('sort_by'); ?>"><?php _e('Sort By:', 'framework') ?></label>
<select name="<?php echo $this->get_field_name('sort_by'); ?>" id="<?php echo $this->get_field_id('sort_by'); ?>" class="widefat">
<option value="recent"<?php selected( $sort_by, 'recent' ); ?>><?php _e('Most Recent', 'framework'); ?></option>
<option value="random"<?php selected( $sort_by, 'random' ); ?>><?php _e('Random', 'framework'); ?></option>
</select>
</p>
<?php
}
function update($new_instance, $old_instance)
{
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['count'] = $new_instance['count'];
$instance['sort_by'] = $new_instance['sort_by'];
return $instance;
}
}?>
任何协助将不胜感激。
好的
,根据我的评论,我将解释。 可能有一种更好的方法来实现这一目标,方法是更改meta_query
参数以使其更有效率(即首先不要拉出重复项),但是我的评论中的链接正在做的是使用 while
循环设置一个新的空数组,然后在循环迭代时向其添加值。
在链接中的那个示例中,它是"国家",我已经更改了它,以便它检查帖子 ID,这对您来说是一个更好的选择,因为它是唯一标识符。 每次循环时,它都会将 post id 添加到数组$added
,在循环开始时,它使用 PHP 函数 in_array
根据已添加到$added
的所有值检查 post ID。 如果它发现 post ID 已在数组中,则调用continue
并跳过循环中的其余代码,并返回到下一次迭代。 因此,您不会将重复项输出到页面。
$added = array();
if($featured_query->have_posts()):
?>
<ul class="featured-properties">
<?php
while($featured_query->have_posts()):
$featured_query->the_post();
// bail early if this post ID has already been added
if( in_array(get_the_ID(), $added) )
{
continue;
}
// add post id to the array
$added[] = get_the_ID();
?>
<li>
<?php
if(has_post_thumbnail()){
?>
<figure>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('grid-view-image');?>
</a>
</figure>
<?php
}
?>
<h4><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h4>
<p><?php framework_excerpt(7); ?> <a href="<?php the_permalink(); ?>"><?php _e('Read More','framework'); ?></a></p>
<span class="price"><?php property_price(); ?></span>
</li>
<?php
endwhile;
?>
</ul>
<?php
wp_reset_query();
else:
?>
<ul class="featured-properties">
<?php
echo '<li>';
_e('No Featured Property Found!', 'framework');
echo '</li>';
?>
</ul>
<?php
endif;