• Resolved Rand HOPPE

    (@rand-hoppe)


    OK, after some searching I’ve yet to find a clear description of how to use a different Google Font in a Twenty Twelve Child theme. I think this’d be of some use to others, so I’m surprised to not find the process already described.

    Here’s what I’ve got so far:

    1) Take out the call to Open Sans:

    Put

    function mytheme_dequeue_fonts() {
         wp_dequeue_style( 'twentytwelve-fonts' );
     }
     add_action( 'wp_enqueue_scripts', 'mytheme_dequeue_fonts', 11 );

    in the Child’s functions.php.

    2) To call a font named “Droid Serif”:

    Do I then put something like this in the child’s functions.php?

    if ( 'off' !== _x( 'on', 'Droid Serif font: on or off', 'twentytwelve' ) ) {
    		$subsets = 'latin';
    
    		/* translators: To add an additional Open Sans character subset specific to your language, translate
    		   this to 'greek', 'cyrillic' or 'vietnamese'. Do not translate into your own language. */
    		$subset = _x( 'no-subset', 'Droid Serif font: add new subset (greek, cyrillic, vietnamese)', 'twentytwelve' );
    
    		if ( 'cyrillic' == $subset )
    			$subsets .= ',cyrillic,cyrillic-ext';
    		elseif ( 'greek' == $subset )
    			$subsets .= ',greek,greek-ext';
    		elseif ( 'vietnamese' == $subset )
    			$subsets .= ',vietnamese';
    
    		$protocol = is_ssl() ? 'https' : 'http';
    		$query_args = array(
    			'family' => 'Droid+Serif:400italic,700italic,400,700',
    			'subset' => $subsets,
    		);
    		wp_enqueue_style( 'twentytwelve-fonts', add_query_arg( $query_args, "$protocol://fonts.googleapis.com/css" ), array(), null );
    	}

    Doing only these two steps kicks back an error.

    3) I believe I came across something regarding place something in the child styles.css. Is that true?

    Any help would be welcomed.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Ayman

    (@aymanalzarrad)

    Why don’t you make a copy of the parent header.php (where I think the call to Google font is made) and add/manage the font you need…

    Thread Starter Rand HOPPE

    (@rand-hoppe)

    Hmm. I don’t see anything in header.php.

    You have to enqueue your font with another function as you have dequeued twentytwelve-fonts.

    Use a function like this:

    function child_theme_load_fonts() {
        wp_register_style('googleFontsDroid', 'https://fonts.googleapis.com/css?family=Droid+Serif:400italic,700italic,400,700');
        wp_enqueue_style( 'googleFontsDroid');
    }
    
    add_action('wp_enqueue_scripts', 'child_theme_load_fonts');
    Thread Starter Rand HOPPE

    (@rand-hoppe)

    Thanks, fredeliko.

    So now I have

    function mytheme_dequeue_fonts() {
         wp_dequeue_style( 'twentytwelve-fonts' );
     }
     add_action( 'wp_enqueue_scripts', 'mytheme_dequeue_fonts', 11 );
    
    if ( 'off' !== _x( 'on', 'Droid Serif font: on or off', 'twentytwelve' ) ) {
    		$subsets = 'latin';
    
    		/* translators: To add an additional Open Sans character subset specific to your language, translate
    		   this to 'greek', 'cyrillic' or 'vietnamese'. Do not translate into your own language. */
    		$subset = _x( 'no-subset', 'Droid Serif font: add new subset (greek, cyrillic, vietnamese)', 'twentytwelve' );
    
    		if ( 'cyrillic' == $subset )
    			$subsets .= ',cyrillic,cyrillic-ext';
    		elseif ( 'greek' == $subset )
    			$subsets .= ',greek,greek-ext';
    		elseif ( 'vietnamese' == $subset )
    			$subsets .= ',vietnamese';
    
    		$protocol = is_ssl() ? 'https' : 'http';
    		$query_args = array(
    			'family' => 'Droid+Serif:400italic,700italic,400,700',
    			'subset' => $subsets,
    		);
    		wp_enqueue_style( 'twentytwelve-fonts', add_query_arg( $query_args, "$protocol://fonts.googleapis.com/css" ), array(), null );
    	}
    
    function child_theme_load_fonts() {
        wp_register_style('googleFontsDroid', 'https://fonts.googleapis.com/css?family=Droid+Serif:400italic,700italic,400,700');
        wp_enqueue_style( 'googleFontsDroid');
    }
    
    add_action('wp_enqueue_scripts', 'child_theme_load_fonts');

    in my child theme’s functions.php

    I’ve also added

    body.custom-font-enabled {
    	font-family: "Droid Serif", Georgia, Times, serif;
    }

    to the child’s stylesheet. Unfortunately, nothing’s changed.

    site is: https://kirbymuseum.org/blogs/dynamics

    Any ideas?

    But then you are trying to load the font twice, and as you have dequeued twentytwelve-fonts function then the body class custom-font-enabled isn’t loaded anymore.

    Summing up, remove all you have done and put:

    function mytheme_dequeue_fonts() {
         wp_dequeue_style( 'twentytwelve-fonts' );
     }
     add_action( 'wp_enqueue_scripts', 'mytheme_dequeue_fonts', 11 );
    
    function child_theme_load_fonts() {
        wp_register_style('googleFontsDroid', 'https://fonts.googleapis.com/css?family=Droid+Serif:400italic,700italic,400,700');
        wp_enqueue_style( 'googleFontsDroid');
    }
    add_action('wp_enqueue_scripts', 'child_theme_load_fonts');

    And in your style.css put something like:

    body{
    	font-family: "Droid Serif", Georgia, Times, serif;
    }

    as .custom-font-enabled class is not in the body tag anymore.

    Thread Starter Rand HOPPE

    (@rand-hoppe)

    Thank you, again frendeliko, but still no success here. ??

    Here’s my child functions.php

    <?php
    // Add your own functions below this line
    
    if ( ! function_exists( 'twentytwelve_entry_meta' ) ) :
    /**
     * Prints HTML with meta information for current post: categories, tags, permalink, author, and date.
     *
     * Create your own twentytwelve_entry_meta() to override in a child theme.
     *
     * @since Twenty Twelve 1.0
     */
    function twentytwelve_entry_meta() {
    	// Translators: used between list items, there is a space after the comma.
    	$categories_list = get_the_category_list( __( ', ', 'twentytwelve' ) );
    
    	// Translators: used between list items, there is a space after the comma.
    	$tag_list = get_the_tag_list( '', __( ', ', 'twentytwelve' ) );
    
    	$date = sprintf( '<a href="%1$s" title="%2$s" rel="bookmark"><time class="entry-date" datetime="%3$s">%4$s</time></a>',
    		esc_url( get_permalink() ),
    		esc_attr( get_the_time() ),
    		esc_attr( get_the_date( 'c' ) ),
    		esc_html( get_the_date() )
    	);
    
    	$author = sprintf( '<span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s" rel="author">%3$s</a></span>',
    		esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
    		esc_attr( sprintf( __( 'View all posts by %s', 'twentytwelve' ), get_the_author() ) ),
    		get_the_author()
    	);
    
    	// Translators: 1 is category, 2 is tag, 3 is the date and 4 is the author's name.
    	if ( $tag_list ) {
    		$utility_text = __( '<div class="by-author"> by %4$s</div> <div class="posted-and">Posted %3$s in %1$s and tagged %2$s</div>', 'twentytwelve' );
    	} elseif ( $categories_list ) {
    		$utility_text = __( '<div class="by-author"> by %4$s</div> <div class="posted-and">Posted %3$s in %1$s.</div>', 'twentytwelve' );
    	} else {
    		$utility_text = __( '<div class="by-author"> by %4$s</div> <div class="posted-and">Posted %3$s.</div>', 'twentytwelve' );
    	}
    
    	printf(
    		$utility_text,
    		$categories_list,
    		$tag_list,
    		$date,
    		$author
    	);
    }
    endif;
    
    function twentytwelvechild_custom_header_setup() {
    	$header_args = array( 'height' => 125, 'width' => 770 );
    	add_theme_support( 'custom-header', $header_args );
    }
    add_action( 'after_setup_theme', 'twentytwelvechild_custom_header_setup' );
    
    function mytheme_dequeue_fonts() {
         wp_dequeue_style( 'twentytwelve-fonts' );
     }
     add_action( 'wp_enqueue_scripts', 'mytheme_dequeue_fonts', 11 );
    
    function child_theme_load_fonts() {
        wp_register_style('googleFontsDroid', 'https://fonts.googleapis.com/css?family=Droid+Serif:400italic,700italic,400,700');
        wp_enqueue_style( 'googleFontsDroid');
    }
    
    add_action('wp_enqueue_scripts', 'child_theme_load_fonts');

    and

    body line added to child stylesheet

    body {
    	font-family: "Droid Serif",Georgia,Times,serif;
    }

    Hi Rand,

    You have an error in your stylesheet that disallow following rules.

    .by-author {
    	padding-top: 8px;
    )

    The closing key must be a closing bracket, not a parenthesis. That removes next rules, so font-family is not applied.

    Thread Starter Rand HOPPE

    (@rand-hoppe)

    Thank you very much, again! Of course it had to be something like that…

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Using a different Google Web Font in a Twenty Twelve Child theme’ is closed to new replies.