Archived topic
Assign font by author
3 replies · Started by David on September 6, 2021
Hello team!
I would like to do one thing on the blog page. (In my case it is also the main page).
We are 4 article and podcast authors.
I want to assign each author a different font for the headings (h1-h3) of the articles or podcast that they do individually.
Any idea how to do it?
I am using GP Premium, WP Show post Pro, and GBlocks Pro.
Thanks, David
Hi there,
hmmm... could try this - its a 2 part thing and a 2 snippet thing:
Snippet 1:
// Enqueue author font
function db_load_author_fonts() {
$author_id = get_post_field( 'post_author', get_the_ID() );
if ( $author_id == 1 ) {
wp_enqueue_style( 'Roboto', 'https://fonts.googleapis.com/css?family=Roboto', array(), '1.0' );
}
if ( $author_id == 2 ) {
wp_enqueue_style( 'AnotherFont', 'https://fonts.googleapis.com/css?family=AnotherFont', array(), '1.0' );
}
}
add_action( 'wp_enqueue_scripts', 'db_load_author_fonts' );
Here we get the $author_id.
And we check if ( $author_id == 1 ) {
here you need to change the 1 to match the authors ID. You can get their ID by editing their user profile and checking the URL in the browser.
If that condition is met then we enqueue the font CSS:
wp_enqueue_style( 'Roboto', 'https://fonts.googleapis.com/css?family=Roboto', array(), '1.0' );
The name eg. Roboto and the URL eg. https://fonts.googleapis.com/css?family=Roboto you can find in fonts.google.com
When you select a style it will provide you a Link code that contains the name and URL.
Snippet 2
uses the same conditional checks and outputs inline CSS in the head for each of the choices:
// Load Author inline font styles
function db_load_author_font_styles() {
$author_id = get_post_field( 'post_author', get_the_ID() );
if ( $author_id == 1 ) {
echo '<style>h1,h2,h3 {font-family: "Roboto" !important;</style>';
}
if ( $author_id == 2 ) {
echo '<style>h1,h2,h3 {font-family: "AnotherFont" !important;</style>';
}
}
add_action( 'wp_head', 'db_load_author_font_styles' );
Like the code above you need to change the $author_id == 1 value and the font-family name to match.
If you want more author styles then just simply copy and paste the last if condition ie.
if ( $author_id == 2 ) {
// do stuff
}
And update the values accordingly.
You don't need to include yourself in these, as if its NOT any of the authors in the condition then it will use the Customizer. So just makes you are the Author of those pages.
Thanks, David!!
It works great.
Awesome - really glad to hear that!