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?
- Missing feature image link function
- WordPress Enqueue for homepage only, functions.php, wp-framework
- How to change the markup WordPress inserts for post images
- Programmatically Set First Image as Featured
- Enqueue Script with data attributes
- WP 4.4. responsive loads normal image after loading the responsive image
- How to add classes to images based on their categories?
- How to reduce original image quality on upload?
- Set image size on media and text block with a function
- Generating Responsive Background Image Sizes in PHP
- Create thumbnail on PDF upload with Gravity Forms
- Remove images from get_the_excerpt
- Use Exact Image Size using add_image_size
- Insert width & height attributes to all images displayed on each page
- Enqueuing Script in functions.php vs on the page
- Overwrite Parent Theme add_image_size in Child Theme
- Images uploaded using media uploader are appearing upside down
- How to add css class to image attached in all the posts?
- If is multiple page templates
- Add custom image size not working
- Load JS Script only for custom post types
- how to add an image to the footer?
- wp_get_attachment_image function size argument not working if I also add a custom class
- Inserting HTML tag with ACF into shortcode
- Understanding and altering the structure of posted images
- Not sure if enqueuing js scripts properly
- Publish/Update post is changing image links from file url to post url
- Featured Image Thumbnail Creation
- Display images that are not in the content
- Can you put a condition on add_image_size() depending on the width of the uploaded image?
- show first 3 thumbnails of posts in different sizes [closed]
- how to en-queue jQuery to load before the tag
- Getting custom image size URL in functions
- Can’t generate thumbnail images
- jQuery does not work
- Control the srcset much more (not all sizes in it each time)
- add_action wp_enqueue_script priority level only works for admins
- Scripts not loading when using the wp_enqueue_scripts action
- Why does get_template_directory_uri() not include “www”, only for enqueued stylesheets?
- In jquery use php variable to execute an enqueued jquery file
- How to hide image-url if no attachment?
- Run a jquery script on on a certain template page
- Add php code to wp_print_scripts?
- What are the meta fields for an attachment?
- Load JS file only in specific template
- Programmatically add Yoast meta description after post save
- How to properly set custom logo size?
- Add attribute only to first image of every post via functions.php
- How to load jQuery in TOP of wp_footer?
- Function to insert missing image size attributes into img tags
- Image Crop Not Working
- Using two loops in one WordPress template
- video.js not enqueueing?
- Include Jquery libraries in wordpress theme?
- Use existing image sizes for WooCommerce
- Insert a field with PREG_REPLACE – strange behaviour
- wp_get_attachment_image_src returns image on main domain, false on subdomain
- Retrieve First Image Function
- Conditional function for excluding first image from content, not working
- Display ACF category image on archive and single template files
- Featured Image and Tags problem
- How to use wp_enqueue_script properly?
- Get webpack to work on child theme
- Override a Post’s URL with Advanced Custom Fields Function
- WordPress Function Assistance with Loop
- Different image using srcset function
- Advanced method to control cache of enqueued style/script
- Auto delete content in specific folder inside media library
- Add custom css file after plugin css with WordPress Child Theme functions.php
- large image size of 1440×1440 not pulling 2x of 2880×2880 image
- Test CDN link from function.php or wp_enqueue_script/style?
- Help with with my function for wordpress
- Can You Set A Minimum Image Dimension For Resizing Images?
- (Divi): How to make WordPress load images of specific size for Blurb modules on given page with a funciton?
- Images with overlay
- WordPress and Advanced Custom Fields and Flickity
- Reference multiple style sheets, clearing styles for permalink page, custom fields for css
- get_field() with ID in one signal filter not working
- WP Enqueue Script Error
- Echo a String Based On Geolocation? [Woocommerce || ACF]
- How do I dequeue a Stylesheet, stored in an ‘Assets’ folder?
- Comment function for page
- restrict uploaded image size and fixed image display size
- Disable links to images only if link is an image
- 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
- Restricted images only for logged user. (hidden url)
- Zip all (original) images from media gallery
- custom post with loading script per single post
- update_post_meta not working in function
- wp_enqueue_script only works outside of action
- Open image size links in a page instead of direct image link
- using enqueue_script in a shortcode isn’t working
- Function to get thumbnail img source
- does acf_save_post cause endless loop?
- wp_enqueue_scripts called twice?
- WordPress – Notice: Function wp_enqueue_script was called incorrectly
- dynamically import array from another js file in WordPress
- Add custom text before all image captions in WordPress posts