简体   繁体   English

限制数组的输出结果

[英]Limit output results of an array

I wanna limit the result to 10 elements (10 words). 我想将结果限制为10个元素(10个单词)。 This is the code I got: 这是我得到的代码:

function ls_bp_hashtags_cloud() {
    $args = array () ;
    if ( bp_is_activity_component() ) {
        $toHead = __( 'Popular Hashtags across network' , 'bp-hashtags' ) ;
    }
    if ( bp_is_user_activity() ) {
        $toHead = __( 'Hashtags by user' , 'bp-hashtags' ) ;
        $args[ 'user_id' ] = bp_displayed_user_id() ;
    }
    if ( bp_is_group_activity() || bp_is_group_home() ) {
        $toHead = __( 'Hashtags in group' , 'bp-hashtags' ) ;
        $args[ 'if_activity_item_id' ] = bp_get_current_group_id() ;
    }
    echo '<div align="right"><h5>' . $toHead . '</h5>' ;
    echo ls_bp_hashtags_generate_cloud( $args ) ;
    echo '</div>' ;
}

And thisone below is the ls_bp_hashtags_generate_cloud() function: 下面是ls_bp_hashtags_generate_cloud()函数:

 function ls_bp_hashtags_generate_cloud( $args = array() ) {
    $hashtags = ls_bp_hashtags_get_hashtags( $args );
    $defaults = array(
    'smallest' => 10, 'largest' => 10, 'unit' => 'pt', 'number' => 0,
    'format' => 'flat', 'separator' => ",\n\n", 'orderby' => 'count', 'order' => 'DESC',
    'topic_count_text_callback' => 'default_topic_count_text',
    'topic_count_scale_callback' => 'default_topic_count_scale', 'filter' => 1
);
    $args = wp_parse_args( $args, $defaults );
    extract( $args );
    $tag_cloud = wp_generate_tag_cloud( $hashtags, $args );
    $tag_cloud = '<div class="hashtags">' . $tag_cloud . '</div>';

    return $tag_cloud;
}

I got this other one too, not sure if it's needed: 我也得到了另一个,不确定是否需要:

function ls_bp_hashtags_get_hashtags( $args = array() ) {
    global $wpdb;
    $bp = buddypress();
    $link = $bp->root_domain . "/" . $bp->activity->slug . "/" . BP_ACTIVITY_HASHTAGS_SLUG . "/";
    bp_hashtags_set_constants();

    $data = maybe_unserialize( get_site_option( 'ls_bp_hashtags' ) );

    if ( $data['style']['show_hashsymbol'] == '1' ) {
    $hashtag_name = ' CONCAT( "#", hashtag_name)';
    } else {
    $hashtag_name = 'hashtag_name ';
    }


    $toWhere = ls_bp_hashtags_generate_query_limitations( $args );

    $results = $wpdb->get_results( 'SELECT COUNT(hashtag_name) as count, '
    . $hashtag_name . ' as name, '
    . 'CONCAT("' . $link . '", hashtag_slug) as link
    FROM ' . BP_HASHTAGS_TABLE . ' WHERE 1=1 ' . $toWhere . ' GROUP BY hashtag_name' );

    return $results;
}

The first snippet I pasted here is in one file while the last 2 snippets are in another file. 我在此处粘贴的第一个代码段在一个文件中,而后两个代码段在另一个文件中。 I think I have to modify the function ls_bp_hashtags_generate_cloud(), the point is I don't know how to do that. 我想我必须修改函数ls_bp_hashtags_generate_cloud(),关键是我不知道该怎么做。 I tried with array_slice() and with $sql = "SELECT * FROM bp_activity LIMIT 10"; 我尝试使用array_slice()和$ sql =“ SELECT * FROM bp_activity LIMIT 10”; I checked mysql database, my ashtags are located in bp_ashtags table, but when I open this table I see each hashtag is referred to be in bp_activity table. 我检查了mysql数据库,我的ashtag位于bp_ashtags表中,但是当我打开该表时,我看到每个hashtag都指向bp_activity表中。 This function gives me the ashtags cloud of my buddypress activity stream page, so it gives me all the ahstags present in the database, instead I wanna limit the result to just 10 #ashtags (it's a wordpress website). 此功能为我的buddypress活动流页面提供了ashtags云,因此它为我提供了数据库中存在的所有ahstags,而我想将结果限制为仅10个#ashtags(这是一个wordpress网站)。 That said I'm really a novice so please if you can help me giving me the whole snippet to add and where to put it, I know almost nothing about php coding. 那就是说我真的是一个新手,所以请您帮我把整个代码片段添加到哪里以及放在哪里,我对php编码一无所知。 If you need to know other things about this code, just tell me; 如果您需要了解有关此代码的其他信息,请告诉我; thisone is an extract so I don't know if I'm missing something for the solution. 这个人是摘录,所以我不知道我是否缺少解决方案。 Thank you in advance 先感谢您

Yes, I have to modify the function ls_bp_hashtags_generate_cloud(), the point is I don't know how to do that. 是的,我必须修改函数ls_bp_hashtags_generate_cloud(),但要点是我不知道该怎么做。 I tried with array_slice() and with $sql = "SELECT * FROM bp_activity LIMIT 10"; 我尝试使用array_slice()和$ sql =“ SELECT * FROM bp_activity LIMIT 10”; I checked mysql database, my ashtags are located in bp_ashtags table, but when I open this table I see each hashtag is referred to be in bp_activity table. 我检查了mysql数据库,我的ashtag位于bp_ashtags表中,但是当我打开该表时,我看到每个hashtag都指向bp_activity表中。 This function gives me the ashtags cloud of my buddypress activity stream page, so it gives me all the ahstags present in the database, instead I wanna limit the result to just 10 #ashtags (it's a wordpress website). 此功能为我的buddypress活动流页面提供了ashtags云,因此它为我提供了数据库中存在的所有ahstags,而我想将结果限制为仅10个#ashtags(这是一个wordpress网站)。 That said I'm really a novice so please if you can help me giving me the whole snippet to add and where to put it. 就是说我真的是一个新手,所以请您帮我把整个代码片段添加到哪里以及放在哪里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM