Site logo

[Resolved] Changing the background image based on month in GP

Home Forums Support [Resolved] Changing the background image based on month in GP

Home Forums Support Changing the background image based on month in GP

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #2531744
    Sean

    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);
    } 
    #2531960
    Ying
    Staff
    Customer Support

    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?

    #2532000
    Sean

    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-1000×1000-1.png

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

    –Sean

    #2532038
    Ying
    Staff
    Customer Support

    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');
    #2532049
    Sean

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

    –Sean

    #2532050
    Sean

    Closing the ticket.

    #2532267
    Ying
    Staff
    Customer Support

    No Problem, glad to help 🙂

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.