• Resolved iamapay

    (@iamapay)


    Hi,

    I’m using this function to limit titles to 10 words :

    //Ten words title
    
    add_filter( 'the_title', 'wpse_75691_trim_words' );
    
    function wpse_75691_trim_words( $title )
    {
        // limit to ten words
        return wp_trim_words( $title, 10, '...' );
    }

    Though, as soon as i put this function on my website, it’s breaking my menu where i’m using a custom link associated with a custom class and a font awesome icon.

    https://i.imgur.com/7dVYege.png

    The icon is simply not displaying and i fail to resolve the problem/see the connexion between the too.

    Any help would be really appreciated !
    Best,
    Apay

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

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    wp_trim_words() heavily pre-processes strings in order to be able be able to count words without including HTML tags, inline scripts, inline styles, etc. For whatever reason, the font awesome icon reference is being stripped out. You may not realize it, but menu items are a special post type, the menu headings are post titles, so are passed through your filter callback. Since menus wouldn’t have 10 words, you would think your code should have no affect on menus. Because of the heavy pre-processing though, the icon reference gets lost.

    What you should probably do is conditionally apply the word trimming only to where you really need it. Maybe only trim if the main query is active. Menu items also use queries, but those are not the main query. Or selectively add the filter only when you need it. For example, add your callback on the template file just before the loop. You could even remove the callback after the loop completes. Then no other titles will be affected.

    Thread Starter iamapay

    (@iamapay)

    Thanks for your help @bcworkz, I didn’t know that !

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Problem displaying font awesome after wp_trim_words’ is closed to new replies.