• Resolved woodypad

    (@woodypad)


    Hi everyone,
    I’m trying to exclude from the name of the categories some elements that the authors insert to distinguish them in the backend.
    In fact, they often insert: (a) * + “”

    This is clearly reflected in the name shown in the frontend and in the permalinks.
    For the permalink I think I have solved.
    But I can’t get it off the Frontend name.

    Do you have a suggestion?

    To be clearer, I have to leave what they write in the backend, but show it in the frontend without.

    Thank you

    • This topic was modified 2 years, 7 months ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not an Developing with WordPress topic
Viewing 7 replies - 1 through 7 (of 7 total)
  • While looking for a solution to your concern I found this article: https://wordpress.stackexchange.com/questions/259471/change-term-name-only-on-front – doesn’t that fit?

    Thread Starter woodypad

    (@woodypad)

    Hello,
    Thanks for your interest!

    But what I’m trying to do is the other way around.
    So I’m trying to remove characters instead of adding them.

    Authors often add new categories and also add fonts to distinguish them in the backend.
    These however don’t want them to end up on the front end, so I should somehow exclude them from the fronted.

    For example:
    – Lifestyle (a)
    – City +

    I should show in the frontend:
    – Lifestyle
    – City

    Remove or add characters – it doesn’t matter what is done in this hook. The way from this source code shows how you can influence the term title in the frontend in a hook. Of course, you have to adapt this for your case. E.g. by removing the corresponding characters with str_replace() instead of adding something with sprintf().

    Thread Starter woodypad

    (@woodypad)

    Hello,
    thank you i made some changes but i can’t get it to work.
    Unfortunately I have recently started working on this.

    Could you give me an example?
    Thank you

    Try this:

    add_filter('get_term', 'filter_term_name', 10, 2);
    function filter_term_name($term, $taxonomy) {
        // returns the term untouched if you are in the admin area
        if( is_admin() ) {
            return $term;
        }
        if ($taxonomy == 'category') {
            // array with texts to replace
            $replaceTexts = [
                "(a)" => "",
                " +" => ""
            ];
            // replace them
            $term->name = strtr($term->name, $replaceTexts);
        }
        return $term;
    }

    In the array you have to put all strings you want to replace. On the left what should be replaced, on the right what should come instead.

    This concerns now only categories to posts. If you mean other categories you have to adjust the taxonomy in the code here accordingly.

    Thread Starter woodypad

    (@woodypad)

    Thanks!
    It works perfectly!
    Thank you very much!

    It’s nice to read that I could help you with this. You are welcome to set the topic to solved.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Exclude from categories name’ is closed to new replies.