mori
mori

Reputation: 1

Replacing background image with post's featured image in functions.php

I'm new to programming with a limited understanding of php (please be patient...). I started a Wordpress blog with fifty50 theme that I'm trying to customize. https://blog.moirarisenprints.com/

It has a vertical header set with a background image - my goal would be to replace the background image on posts with the featured image of the post. Pages would have the default background image and I don't intend to change the styling of the theme.

This is the snippet of the template-functions.php of the theme I think I shoud use somehow:

/* SIDE COLUMN BACKGROUND IMAGE
Set a sidecolumn background image and attributes with the WordPress background function
@since 1.0.0
==================================================== */
if (!function_exists('custom_background_cb')) :
  function custom_background_cb()
  {

    // background is the custom image or the default image
    $background_image_url = set_url_scheme(get_background_image());

    // When using a sidecolumn background colour without an image
    $colour = get_background_color();

    if ($colour === get_theme_support('custom-background', 'default-color')) {
      $colour = false;
    }

    // previewing in the customizer
    if (!$background_image_url && !$colour) {
      if (is_customize_preview()) {
        echo '<style type="text/css" id="custom-background-css"></style>';
      }
      return;
    }

I tried to add an action to the wp_head in my child-theme's functions.php to replace the background image url with the attachment image url on posts, this is the code I put together:

add_action('wp_head', 'featured_custom_background_cb');
  function featured_custom_background_cb()
  {
    if ( is_single() )
    {
         $background_image_url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
         }
         return $background_image_url;
  }

But it doesn't do anything (not really surprised :)). I'm not even sure I'm trying to do this the right way.

Any help would be appreciated, thanks.

Upvotes: 0

Views: 37

Answers (0)

Related Questions