• Hi.

    I will have about 100 widgets in one widgets area (RSS widgets).

    I figure out how to display random widgets with this code:

    add_filter( 'sidebars_widgets', function( $sidebars_widgets ) {
    
        $sidebar_index = 'sidebar-1';
    
        if( isset( $sidebars_widgets[$sidebar_index] ) )
        {
              shuffle( $sidebars_widgets[$sidebar_index] );
        }
        return $sidebars_widgets;
    }, PHP_INT_MAX );

    But I will not show them all 100 at once, just 8 of them. How can I hide other 92?

    Thanks.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi, @akachan. Try adding this line immediately after your shuffle statement:

    $sidebars_widgets[$sidebar_index] = array_slice( $sidebars_widgets[$sidebar_index], 0, 8 );
    Thread Starter akachan

    (@akachan)

    O-ho! It is alive! ??

    Solved. Thank you.

    add_filter( 'sidebars_widgets', function( $sidebars_widgets ) {
    
        $sidebar_index = 'sidebar-1';
    
        if( isset( $sidebars_widgets[$sidebar_index] ) )
        {
              shuffle( $sidebars_widgets[$sidebar_index] );
    	  $sidebars_widgets[$sidebar_index] = array_slice( $sidebars_widgets[$sidebar_index], 0, 8 );
    
        }
        return $sidebars_widgets;
    }, PHP_INT_MAX );
    Thread Starter akachan

    (@akachan)

    I have another problem with it. All is working in frontpage, but in adminitration too. I see 8 widgets randomly there, like it was a frontpage. It is possible to make in this code an exception for admininistration side of my web?

    Thanks.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Limit widgets in widget area’ is closed to new replies.