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

Question

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.

in progress 0
Scarlett 2 months 2021-07-20T17:23:29-05:00 0 Answer 0 views 0

Answer ( 1 )

    0
    2021-07-23T05:42:03-05:00

    I found a workaround for the time being, just enclosing the rating in a <p> tag and adding the following css to hide the <p> tag only in the portfolio where the rating is displayed. This still shows it in the posts widget where I want it. Of course, I’d rather save loading time and not print the rating in the portfolio at all. .elementor-portfolio-item__title p { display: none !important; }

Leave an answer

Browse
Browse