WordPress.org

Ready to get started?Download WordPress

Forums

Custom Post Widget
[resolved] Widget class based on content block ID (2 posts)

  1. Jesse Gardner
    Member
    Posted 1 year ago #

    It would be especially helpful if the ID of the content block that's chosen could be passed in as a class (e.g. class="content-block-1412").

    Use case: I've got several content blocks I use in several different sidebars. I'd like to be able to define the style for a specific content block wherever it appears, but every instance of that content block widget gets a different ID. The classes, on the other hand, are too broad and every content-block widget gets the same class.

    http://wordpress.org/extend/plugins/custom-post-widget/

  2. Jesse Gardner
    Member
    Posted 1 year ago #

    I added this functionality to line 79 of post-widget.php:

    // Injecting our content block id into the $before_widget class
        if( strpos($before_widget, 'class') === false ) { // It has no class, add it!
            $before_widget = str_replace('>', 'class="widget-content-block-'. $custom_post_id . '"', $before_widget);
        } else { // Append existing class
            $before_widget = str_replace('class="', 'class="widget-content-block-'. $custom_post_id . ' ', $before_widget);
        }

    It will add the ID of the content block you've chosen as class of the widget, e.g. class="widget-content-block-1412".

    Hope someone else finds this useful.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic