How to run php code only for a specific widget on a page and not all widgets on that page?

I want to display extra information below the post title only for the posts widget on the home page and not the portfolio widget. I'm using Wordpress' the_title filter to do this and that works fine, but at the moment, it shows the extra info in both the portfolio and the posts widgets. I need a condition to restrict the filtering only to the posts widget. The post widget's class name is home-post-widget

My current code looks like this.

function add_rating_html( $title) {

    if (in_the_loop()){
        $out = Rating: 5;
        $title .= $out;
    }
    return $title;
}

add_filter('the_title', 'add_rating_html',1);

Any help would be most appreciated.

Topic widget-text php filters Wordpress

Category Web


Seeing as it's in a loop, you should be able to check against the type of post, and only add it to that type.

function add_rating_html( $title) {

    if ( is_front_page() && 'post' === get_post_type() ) {
        $out    = "Rating: 5";
        $title .= $out;
    }
    return $title;
}

add_filter( 'the_title', 'add_rating_html', 1 );

There's probably a better way to do it by hooking on to the specific widget, but I'm no expert with how Elementor works

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.