You need to add the classes for the corresponding cases.
For pages/posts:
$css = "body.page-id-**post_id** { background-image: url('$bg_image'); }";
For categories:
$css = "body.category-**category_id** { background-image: url('$bg_image'); }";
You can retrieve post_id with $post->ID
. For the category you need to check whether the page is_archive()
and to append the corresponding ID or category name.
Related Posts:
- How to update an image attachment’s alt text from a custom field when saving a post?
- remove links from images using functions.php
- functions.php inject inline css
- How to use get_template_directory_uri() to load an image that is in a sub-folder of my theme?
- How to add classes to images based on their categories?
- How to enqueue every script in a folder automatically?
- Is it possible to maintain image aspect ratio when scaling images?
- Generating Responsive Background Image Sizes in PHP
- Why are image thumbnails cropped proportionally (not per dimensions)?
- Most elegant way to enqueue scripts in function.php with foreach loop
- Problem using is_single() to enqueue script from functions.php
- How to add a rel attribute to images that contains their categories?
- Enqueuing External Javascript functions.php
- Why are image thumbnails cropped proportionally (not per dimensions)?
- How to enqueue JavaScript for specific WordPress pages only?
- Enqueue script o style only if a template part is loaded
- Display height and width properties with the_post_thumbnail() or related function
- Dequeue script in template isn’t working
- Custom image size in Media Dropdown
- Removing the first image in content
- Function to auto-set a featured image that is already in use
- Actions according to image type and size
- Woocommerce Storefront WordPress Ignore Media Gallery Images and use External 3rd Party Host
- How do I get a child theme to load scripts from the parent theme?
- How can you limit srcset on a single type of page?
- Enqueue scripts not working with if is page conditional tag in functions.php
- Inserting HTML tag with ACF into shortcode
- Run function when WordPress new multisite is created or ACF field is updated
- Having issues with a foreach inside of a shortcode with ACF gallery
- Twentyten Child Theme: header images display messed up in admin panel
- Disable auto-resizing of uploaded images, but only for certain filename
- Get Current Post ID in functions php, meta query filter
- Can you put a condition on add_image_size() depending on the width of the uploaded image?
- Remove after images
- Add Custom Background Properties to Customizer
- enqueue styles for only mobile wp
- Can’t generate thumbnail images
- Redirect to another page using contact form 7? [closed]
- enqueue_script with filemtime javascript not working
- How to retrieve the values of a sub-field in the first and last row of an (ACF) repeater inside function?
- Limit dimensions of “Full” size images
- How to hide image-url if no attachment?
- Can set_post_thumbnail be used to remove a thumbnail?
- wp_enqueue_script | Help me figure out what is causing jQuery is not defined
- Programmatically add Yoast meta description after post save
- Theme JS is available but theme CSS isn’t
- Need To Change WordPress Meta Tags… Includes wordpress shortcodes
- How to Add Shortcode to html img code?
- How to add a new image size and apply it to posts only?
- Using two loops in one WordPress template
- Problem in using Customizer
- Problem enqueuing scripts on not single posts
- wp_get_attachment_image_src returns image on main domain, false on subdomain
- Call two different function.js depending on page
- Why I can’t load my JavaScripts using an hook inside my functions.php file?
- Custom Image Size in Insert Media
- Perform function on publish AND save (not just save)
- Enqueueing scripts selectively & activation where needed
- Featured Image keeps cropping
- Prevent creating multiple image resizes in twentytwelve template
- Conditional function for excluding first image from content, not working
- get_image_tag() html output : empty src attribute
- Need To Resize Images Exactly Without Losing Image Details
- Different image using srcset function
- How to show ACF value under post/page title in wp-admin
- How to display an image before title text in menu items
- Is it possible to limit number of files a non-admin user can upload via the WordPress media library?
- WordPress wp_dequeue_script remove all other scripts
- Filter works on last selection but no others
- Is it possible to load recaptcha script only in url with fragment identifier (#)?
- Can You Set A Minimum Image Dimension For Resizing Images?
- blank page with wp_get_attachment
- Why does my wp_enqueue_script() only insert the script for logged in users
- (Divi): How to make WordPress load images of specific size for Blurb modules on given page with a funciton?
- ACF Date Form in Custom Admin Field
- Assign IDs to headings in ACF using functions.php
- Echo a String Based On Geolocation? [Woocommerce || ACF]
- How do I dequeue a Stylesheet, stored in an ‘Assets’ folder?
- Featured image on archive page based on post type
- I need to ‘wp_dequeue_script’ and ‘styles’ and ADD a bunch of other css and js
- I need to completely ‘wp_dequeue_script’; what’s the best way?
- Using Switch Statement to Change Image According to Last Digit of Topic ID
- True parameter but jquery register in header and not in the footer with wp_register_script
- Why is style.css not loading through the functions.php
- Return ACF Field value function
- Providing fallback function and allow override by plugin
- How to wp_enqueue_script with html?
- Update add_image_size
- wp_enqueue_script only works outside of action
- Function the_content
- Exclude javascript in certain page url and all following url’s after forward slash
- java script error Uncaught SyntaxError: Unexpected token ILLEGAL
- Full size image option removing problem
- Why my wp_enqueue_script doesnt work on some page?
- Call ACF data from functions.php [closed]
- How to resize image from import image url?
- How to crop image in WordPress with aspect ratio for any size of image
- How to add height and width dimensions automatically to archive descriptions
- How to overwrite image if it already exists – WordPress, Gravity form
- Loading newest dependency javascript module file in functions.php