0

I need to a make a custom RSS template for my website and I'm having problems getting substr(the_title_rss(), 0, strpos(the_title_rss(), ' —')); to work on the RSS feed.

What the above code is suppose to do is if a title has - in the title, it removes everything after the -.

I think the best way to solve my problem is to run substr and strpos and save the title in a custom field. I'm not a programmer so has anyone been able to accomplish this or point me in the right direction?

Thanks,

1 Answer 1

2

the_title_rss() outputs its value straight away so it can't be used in a function like that, you would have to use get_the_title_rss()

Or alternatively the_title_rss() has a filter we can hook in to and return your shortened title is possible. (You would add this to your functions.php)

function gg_short_title_rss($title)
{
    // This can return false, so check there is something
    $short_title = substr($title, 0, strpos($title, ' –'));
    if ($short_title) {
        return $short_title;
    }

    // Else just return the normal title
    return $title;
}
add_filter('the_title_rss', 'gg_short_title_rss', 10, 1);
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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