0

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.

5
  • Have you tried using is_front_page() and/or is_home() instead of in_the_loop()?
    – Jack Johansson
    Jul 20 at 23:26
  • is_home() doesn't work, but is_front_page() does, which is much better than using in_the_loop(). Thanks :) But the problem is that both the portfolio and the posts block are on the front page, and I only want to show the rating in the posts block. With is_front_page(), it's showing the rating on both the portfolio and the posts block.
    – Scarlett
    Jul 20 at 23:44
  • when you say the portfolio or posts block, do you mean widgets, or do you mean blocks, or do you mean blocks in a widget area? Widgets and blocks are not the same, you need to be very clear if you're referring to a block, or a widget. If it is indeed a block, then the answer to this question will be useless to you because you asked about widgets, not blocks. Also if you are referring to things in a page builder that's also super important to mention.
    – Tom J Nowell
    Jul 21 at 0:06
  • Sorry about the confusion. I shouldn't have used the word block. Both the portfolio and posts widgets are Elementor widgets. And yes I use the page builder Elementor Pro. I've updated the question to clarify that I'm referring to widgets.
    – Scarlett
    Jul 21 at 0:28
  • 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; }
    – Scarlett
    Jul 21 at 2:43
0

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

2
  • I tried that but it didn't work because the post type in both the portfolio widget and the posts widget are of type 'post'.
    – Scarlett
    Jul 21 at 4:01
  • Then you really need to find something better to manage portfolio items Jul 21 at 8:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.