Site logo

Archived topic

Changing the background image based on month in GP

6 replies · Started by Sean on February 13, 2023

Viewing posts 1–7 of 7

Hi GP team,

On one of our GP sites, we would like to change the logo and background based on the month (so a Christmas theme in December, etc..). We found a solution for another theme that uses an action/function in the functions.php but the hooks are for the vantage-child theme.

https://wordpress.org/support/topic/auto-change-logo-and-background-image-every-month/

Are there variables in the GeneratePress theme that would be similar to those used in the code snippet below?

Thanks for any help you can provide!

--Sean

//auto change logo and masterhead background image depending on month
add_action('init', 'update_logo');
function update_logo() {
	$month = date("n");
	$my_fields = get_option('theme_mods_vantage-child');
	if ($month == 12 || $month == 1 || $month == 2) { 
		$logo = 7078; //christmas logo post ID
		$retina_logo = 7077; //christmas retina logo post ID
		$background = 'https://images2.luga.bg/2016/12/back_1080_christmas.jpg'; //christmas masterhead background image url address
	}
	else { 
		$logo = 6979; //standard logo post ID
		$retina_logo = 6981; //standard retina logo post ID
		$background = 'https://images2.luga.bg/2016/12/back_1080.jpg'; //standard masterhead background image url address
	}
	$my_fields['theme_settings_logo_image'] = $logo;
	$my_fields['theme_settings_logo_image_retina'] = $retina_logo;
	$my_fields['vantage_page_masthead_background_image'] = $background;
	update_option('theme_mods_vantage-child', $my_fields);
} 

Hi Sean,

Try this snippet for the logo, replace your-holiday-logo-file-url with the actual logo url.

add_filter( 'generate_logo_output','holiday_logo', 10, 2 ); 
function holiday_logo( $output, $logo ) {
	$month = date("n");
	if ($month == 12 || $month == 1 || $month == 2) { 
	printf(
		'<div class="site-logo holiday-logo">
			<a href="%1$s" title="%2$s" rel="home">
				<img width="999" height="999" class="header-image" src="%3$s" alt="%2$s" title="%2$s" />
			</a>
		</div>',
		esc_url( apply_filters( 'generate_logo_href' , home_url( '/' ) ) ),
		esc_attr( apply_filters( 'generate_logo_title', get_bloginfo( 'name', 'display' ) ) ),
		esc_url( 'your-holiday-logo-file-url' )
	);
	}
		printf(
		'<div class="site-logo">
			<a href="%1$s" title="%2$s" rel="home">
				<img %3$s />
			</a>
		</div>',
		esc_url( apply_filters( 'generate_logo_href' , home_url( '/' ) ) ),
		esc_attr( apply_filters( 'generate_logo_title', get_bloginfo( 'name', 'display' ) ) ),
		esc_url( apply_filters( 'generate_logo', $logo ) )
	);
}

As for the background, can you show us the background you are referring to?

The site is at https://www.theholidayhandbook.com/

and the background is the <body> background-image.

So, for example, for February we'd like to change the logo to https://www.theholidayhandbook.com/wp-content/uploads/2022/01/THH-February-finalpng.png

For the body background-image to this https://www.theholidayhandbook.com/wp-content/uploads/2021/12/THH-Background-Feb-1000x1000-1.png

Thanks SO MUCH for your help on this and let me know if there are any additional questions.

--Sean

Here's the code for holiday background:

function holiday_background() {
  $month = date("n");

  $cssToAdd = "";

 	if ($month == 12 || $month == 1 || $month == 2) { 
    $cssToAdd = '
      <style id="holiday-background">
        body {
          background-image: url(https://www.theholidayhandbook.com/wp-content/uploads/2021/12/THH-Background-Feb-1000×1000-1.png) !important;
        }
      </style>';
    
  }
  echo $cssToAdd;
}

add_action('wp_head', 'holiday_background');

Thanks you SO MUCH! Worked like a charm. Appreciate your guidance.

--Sean

Closing the ticket.

No Problem, glad to help :)

This archived topic is closed to new replies.