Support » Plugin: Breadcrumb NavXT » change text of specific page in the trail

  • Hey,

    I need to change a text of the post type root page.
    How can I do it by using a function in the functions.php file?

    Thanks

    The page I need help with: [log in to see the link]

Viewing 11 replies - 1 through 11 (of 11 total)
  • Plugin Author John Havlik

    (@mtekk)

    By default Breadcrumb NavXT uses the post title of the root post type root page for that breadcrumb in the breadcrumb trail. The easiest thing to do is to change the actual title of the root page. If you want the two to be different for some reason, you can achieve this with the bcn_breadcrumb_title filter. If you’re trying to change things for multi-lingual site reasons, there are premium extensions that will do this for you (may be a moot point at it looks like that route was taken on the linked site).

    Hello @regevkamber , hello @mtekk ,

    I have tried to update with filters the function.php and the class.bcn_breadcrumb.php files in order to replace the default text title (from title post) in the trail with a specific one. I didn’t get it.
    My issue is that I ended with just modifying the link title (when hovering the link).

    @regevkamber Have you been able to find a way to filter specific page and replace the default title with a different one ?

    Any help is appreciated!
    Thank you
    Rom

    Plugin Author John Havlik

    (@mtekk)

    @rom174: I strongly caution against modifying any of the files that Breadcrumb NavXT ships with as these modifications will get wiped out when you update the plugin.

    I am confused as to how you were able to write a filter for bcn_breadcrumb_title and only affect the title showing up in the link title. The only way that should be possible is if you have a static title in the breadcrumb template (e.g. did not have %htitle% in your a tag). Can you provide more information as to what you did and what markup you are seeing being generated by Breadcrumb NavXT?

    Hello @mtekk ,

    I tried to modify the core files more for testing purpose than as a final and definitive solution.

    Here is how I ended with this, you are probably going to laugh because of my lack of knowledges. In fact, I wasn’t able to write a filter, I adapted it from a snippet you gave for the home breadcrumb (in this page) :

    add_filter('bcn_breadcrumb_title', 'my_breadcrumb_title_swapper', 3, 10);
    function my_breadcrumb_title_swapper($title, $type, $id)
    {
        if(in_array('home', $type))
        {
            $title = __('Home');
        }
        return $title;
    }

    When trying to make it work, I first tried to update the code with the home breadcrumb, it worked but only the hovered title changed, not the displayed title. So from here, I tried to replace the variable $title with $htitle, as it is in the schema. (just a question, is the “h” mean HTML in order to parse html code instead of pure text ?)

    Knowing that it was written for the first breadcrumb “home”, I adapted it with my specific post ID and the title I knew was displayed with the one I wanted to replace. After testing it in the core file, I placed it in the function.php.
    The thing is that it removed all breadcrumbs after the Home breadcrumb in the trail.

    What I wanted to achieve is to shorten the displayed text in trail for not using the original title post which is long and make the trail very long.

    In order to detail a bit more, here is what was my idea :

    add_filter('bcn_breadcrumb_title', 'my_breadcrumb_title_swapper', 3, POST_ID);
    function my_breadcrumb_title_swapper($title, $type, $id)
    {
        if(in_array('ORIGNAL_TITLE_POST', $type))
        {
            $title = __('SHORTENED_TITLE');
        }
        return $title;
    }
    Plugin Author John Havlik

    (@mtekk)

    @rom174: For the specific issue on the home page, it is very likely that your home template had ‘Home’ in it rather than %htitle%, that would cause the described behavior (it is also mentioned in the article you linked to under the ‘Consider the Following section).

    As for the actual problem you’re trying to solve, if you want to want to use a shorter title, this is the filter to use. However, the check against $type probably isn’t appropriate here, you are better off checking $id against the desired post ID. Extending this concept further, you can write an interface for interacting with postmeta to store/grab the shorter title. If you don’t want to write this yourself, there is the Breadcrumb NavXT Title Trixx premium extension that provides this functionality.

    Hello @mtekk ,

    Thank you very much for your explanations, it is very very usefull.
    For the Home breadcrumb, in fact, I did a mistake describing the issue. I just realize it, looking at the doc : https://developer.wordpress.org/reference/functions/__/
    When I described the issue to you, I wrote in english the french word for home, accueil. The true question at that time was : why is the breadcrumb displaying “Accueil”, instead of Home ? (as I should have seen ‘Home’ and not ‘Accueil’ generated from the snippet while the Home schema was set up with a very different title).
    I have understood now that this function __() is looking for the translation of a word, so now it seems clear that wordpress looked after the translation, isn’t it ?

    For the shorten title issue, thank you for the extension name, I found it before but I only have just a few pages to “fix” with long titles, so I would like to first try to find a way to make it work. In these pages, all I try to achieve when matching the post id, is to replace the default post title in the trail.
    I have tried 2 solutions based on your advices, could you let me know what is better :

    if(in_array('post', $type))
    {
        if($id === 4657)
        {
            $title = __('ShortenTitle');
        }
    }

    OR

    if(in_array('post', $type))
    {
        if($title === 'LongTitle')
        {
            $title = __('ShortenTitle');
        }
    }

    I have a very last question, if I add the $htitle parameter (function my_breadcrumb_title_swapper($title, $htitle, $type, $id)), and I update the conditions with the htitle I would like to display, the page doesn’t display, just a white screen. What should I improve in order to specify a different hovered title for a breadcrumb title ?

    Again, thanks a lot for your help and your explanations,
    Rom

    • This reply was modified 2 weeks, 6 days ago by  rom174.
    • This reply was modified 2 weeks, 6 days ago by  rom174.
    • This reply was modified 2 weeks, 6 days ago by  rom174.
    • This reply was modified 2 weeks, 6 days ago by  rom174.
    Plugin Author John Havlik

    (@mtekk)

    Checking against the $id ensures you replace the title only for the page in question.

    There isn’t a $htitle parameter for the bcn_breadcrumb_title filter, so that won’t work. Changes made to the title using bcn_breadcrumb_title will affect both %title% and %htitle%, there isn’t really a way around this. %title% is literally %htitle% run through esc_attr() and strip_tags().

    For creating a special new title for hovers, I’d suggest creating your own custom breadcrumb template tag using the bcn_breadcrumb_tags filter. The documentation has a link to a simple example, you can use that as a starting point.

    rom174

    (@rom174)

    That’s pretty clear except one thing : I don’t want to create a new title for hovers but just keep the original one. In fact, when using the my_breadcrumb_title_swapper function, I don’t want it affects the title attribute but just the anchor text.
    Is there a way to “disable” the title attribute from being replaced ?

    Plugin Author John Havlik

    (@mtekk)

    Internally, there is only one title in Breadcrumb NavXT for each breadcrumb. If you just want to shorten the title shown in the breadcrumb trail so things fit nicely in your layout, consider using CSS to shorten the title (see https://mtekk.us/archives/guides/trimming-breadcrumb-title-lengths-with-css/).

    Otherwise, there is another way of doing this that may help you do what you want. You could use bcn_template_tags to create your own template tag (either for the shortened or the un-shortened title). Then update your templates to use the new custom template tag.

    rom174

    (@rom174)

    That’s a nice trick! Thank you
    Anyway I am not sure if it fits for me as I would prefer to display entire and clear title crumbs with no user action and also I am afraid that a to long title won’t fit SERPs.
    I will try to change template tags.
    Thanks

Viewing 11 replies - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.