amarinediary / WordPress-simple-URL-based-breadcrumb

🍞 A non-invasive WordPress unofficial plugin, minimalist and SEO friendly. both lightweight and lightning fast, adding URL based breadcrumb support. Plug-and-play, with no required configuration.
https://stackoverflow.com/a/67453887/3645650
Creative Commons Zero v1.0 Universal
27 stars 5 forks source link

Get the title better #1

Closed mohamadnr closed 3 years ago

mohamadnr commented 3 years ago

Sometimes url_to_postid cannot retrieve the title of the page despite the page's existence, it happened for me in the archive page. It can also try to get the title using the slug if couldn't find it via the URL. So I added this code:

if ($id = url_to_postid($crumb->url))
    $title = get_the_title($id);
elseif ($id = get_page_by_path($crumb->slug)->ID)
    $title = get_the_title($id);

and changed the value inside the span to this:

 ((isset($title)) ? $title : ucfirst(str_replace('-', ' ', $crumb->slug)))

Seems better to me.

amarinediary commented 3 years ago

Thank you for your input @mohamadnr. Indeed it seems logic to have a fallback. I wasn’t aware that url_to_postid was failing from time to time. I will update the original file as soon as I can.