• pass post_class() info to widget li for css formating

    Excellent plug-in I purchased the pro-version and was hoping you could help with some functionality I’d like to add.

    I’d like to add icons or different colored BG’s to the list of posts in the sidebar widget. I’d like to assign icons with css and the category-name slug add to the unordered list ( li )the widget uses to build lists.

    I found this function online and it works to pass the post_class info
    to the widget outside the loop, but it assigns the classes to widget $params like (before_title, before_widget) and I’m not skilled enough in php the change this.

    function add_before_widget_title_extra_classes($params) {
    //* adapted from a code by @mathsmath / found in:
    //* https://www.ads-software.com/support/topic/how-to-first-and-last-css-classes-for-sidebar-widgets?replies=9

    $this_id = $params[0][‘id’]; // Get the id for the current sidebar we’re processing
    $arr_registered_widgets = wp_get_sidebars_widgets(); // Get an array of ALL registered widgets

    if(!isset($arr_registered_widgets[$this_id]) || !is_array($arr_registered_widgets[$this_id])) { // Check if the current sidebar has no widgets
    return $params; // No widgets in this sidebar… bail early.
    }

    if( is_single() ) : global $post; $class = ‘class=”‘; foreach( get_the_category($post->ID) as $pcat ) { $class .= ‘category-‘ . $pcat->slug . ‘ ‘; } // Add a widget category class for additional styling options
    elseif( is_category() ) : $cat = get_query_var(‘cat’); $class = ‘class=”category-‘ . get_category($cat)->slug . ‘ ‘; // Add a widget category class for additional styling options
    endif;
    if( is_single() || is_category() ) :
    $params[0][‘before_title’] = preg_replace(‘/class=\”/’, “$class”, $params[0][‘before_title’], 1); endif;
    return $params;

    }
    add_filter(‘dynamic_sidebar_params’,’add_before_widget_title_extra_classes’);

    https://www.ads-software.com/plugins/super-post/

  • The topic ‘pass post_class() info to widget li for css formating’ is closed to new replies.