Site logo

Archived topic

Strange issue with dashicons

6 replies · Started by George on March 14, 2020

Viewing posts 1–7 of 7

I have a strange issue. I am including dashicons on the front-end through

add_action( 'wp_enqueue_scripts', 'load_dashicons_front_end' );
function load_dashicons_front_end() {
	wp_enqueue_style( 'dashicons' );
}

Then I enter dashicon HTML in the Navigation Label of my menu items, for example:
<span class="dashicons dashicons-admin-home"></span> Home

I also have a wp-head hook only on the blog archive that truncates the post titles to the first 7 words:

<?php

add_filter( 'the_title', 'wpse_75691_trim_words' );

function wpse_75691_trim_words( $title )
{
	// limit to 7 words
	return wp_trim_words( $title, 7, '...' );
}

?>

It works for the whole site except the blog archive where the dashicons disappear from the menu items. Removing the truncate script gets the icons back again on the blog archive. Settings the hook to wp_footer is a no-go since it doesn't work.

I have tried different priorities for both scripts but nothing. Also nothing in the logs. I have also tried putting the truncate script in functions.php instead of a hook, same problem.

Hi there,

That's strange.

Does the problem only occur on the main posts page? What about archives etc?

Any chance you can try a twenty series default theme and see if the same issue occurs?

Let me know :)

Hi Leo.

Yeah, it happens with a twenty them as well. It occurs wherever that truncate code exists. If I activate it for the entire site, it will strip icons from the entire site. It seems it's not theme-specific. I believe, I have this problem here:

https://wordpress.org/support/topic/problem-displaying-font-awesome-after-wp_trim_words/

but for dashicons.

Still, no idea how it can be solved, though...If you can suggest something, I will take it!

I will, thanks Leo.

I received the solution on this thread.

<?php

function wpse_360758_trim_words( $title, $post_id ) {
    if ( is_admin() ) {
        return $title;
    }

    if ( in_the_loop() && 'post' === get_post_type( $post_id ) ) {
        $title = wp_trim_words( $title, 7, '...' );
    }

    return $title;
}
add_filter( 'the_title', 'wpse_360758_trim_words', 10, 2 );

?>

Thanks again!

Glad you found a solution!

This archived topic is closed to new replies.