从函数中排除类别

时间:2012-03-30 作者:jimilesku

嘿,伙计们,我有这个函数,我需要排除两个类别:81和82。你能帮忙吗?:)

代码

function has_related_same_cat_posts(){
global $post;
$categories = get_the_category($post->ID);

if ($categories) {
    $category_ids = array();
    foreach($categories as $individual_category) 
        $category_ids[] = $individual_category->term_id;

    $args=array(
    \'category__in\' => $category_ids,
    \'post__not_in\' => array($post->ID),
    \'posts_per_page\'=> 4, // Number of related posts that will be shown.
    \'caller_get_posts\'=>1,
    \'orderby\' => \'rand\',
    
    );

    $my_query = new wp_query( $args );
    if( $my_query->have_posts() ) {
        $return = true;
    } else {
        $return = false;
    }
    
}

return $return;
}

1 个回复
SO网友:Michael

foreach ($categories as $individual_category) {        
    if( $individual_category->term_id == 81 || $individual_category->term_id == 82 ) 
        continue;
    $category_ids[] = $individual_category->term_id; 
}
说明:

上述内容取代:

foreach($categories as $individual_category) 
        $category_ids[] = $individual_category->term_id;
也就是说,代码跳过了要排除的类别。

结束

相关推荐

页面元框-Get_Categories下拉列表

我创建了一个包含所有现有类别的下拉列表的元框。页面管理员应该能够从下拉列表中选择一个类别,该类别将由wordpress的保存/更新页面功能保存。<select name=\"event-dropdown\" onchange=\'document.location.href=this.options[this.selectedIndex].value;\'> <option value=\"\"><?php echo esc_attr(__(\'Select Ev