How to use Oswald Medium font in a WordPress theme?

You can use functions.php of a a child theme and paste the following code. So, the Oswald Medium font will get imported in the theme.

Functions.php Code

function add_google_fonts() {
    // Preconnect to Google Fonts
    echo '<link rel="preconnect" href="https://fonts.googleapis.com">';
    echo '<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>';
    
    // Enqueue Google Fonts stylesheet
    wp_enqueue_style( 
        'google-fonts-oswald', 
        'https://fonts.googleapis.com/css2?family=Oswald:wght@500&display=swap', 
        array(), 
        null 
    );
}
add_action( 'wp_enqueue_scripts', 'add_google_fonts' );
Code language: PHP (php)

CSS Usage

h1, h2, h3 {
    font-family: 'Oswald', sans-serif;
    font-weight: 500;
}Code language: CSS (css)

This is how you can use the font for header elements. However, you can use it for any class or id or any elements of a theme.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *