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?
- warning: trying to access array offset on value of type bool ACF field image group
- 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?
- Enqueue script on every page except one
- Run a jquery script on on a certain template page
- Add php code to wp_print_scripts?
- Access category within rss2_head hook?
- What are the meta fields for an 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
- Load JS file only in specific template
- Programmatically add Yoast meta description after post save
- Compare meta key to current date in pre get post
- Woocommerce image sizes missing from Appearance › Customize but not declared by theme
- How to properly set custom logo size?
- Add attribute only to first image of every post via functions.php
- Theme JS is available but theme CSS isn’t
- Need To Change WordPress Meta Tags… Includes wordpress shortcodes
- Need to convert image url to a Base_64 data url with wordpress function..
- How to Add Shortcode to html img code?
- 404 when enqueue_script using plugin_url
- How to add a new image size and apply it to posts only?
- 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
- Problem in using Customizer
- Problem enqueuing scripts on not single posts
- Insert a field with PREG_REPLACE – strange behaviour
- 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?
- Retrieve First Image Function
- 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
- Why is this function crashing my wordpress installation? [closed]
- colorbox not loading in
- Conditional function for excluding first image from content, not working
- Fatal error: allowed memory size when after using enqueue_script
- Using add_image_size in functions.php to get original uploaded file
- get_image_tag() html output : empty src attribute
- wp_enqueue_script() not working
- Need To Resize Images Exactly Without Losing Image Details
- Display ACF category image on archive and single template files
- Featured Image and Tags problem
- Custom image sizes showing in Classic Editor only when upladed directly to post
- How to use wp_enqueue_script properly?
- Get webpack to work on child theme
- Override a Post’s URL with Advanced Custom Fields Function
- how can I change all wordpress media file url to custom cdn url?
- WordPress Function Assistance with Loop
- 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
- Advanced method to control cache of enqueued style/script
- How to load a css file depending on the current role
- Is it possible to limit number of files a non-admin user can upload via the WordPress media library?
- localize_script but data changes dependent on product ID
- Auto delete content in specific folder inside media library
- How to change images url in function.php?
- WordPress wp_dequeue_script remove all other scripts
- Filter works on last selection but no others
- Add custom css file after plugin css with WordPress Child Theme functions.php
- Is it possible to load recaptcha script only in url with fragment identifier (#)?
- Class parameter in get_avatar args doesn’t get added to output
- wp enqueue style on about us page
- 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?
- 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?
- Images with overlay
- ACF Date Form in Custom Admin Field
- WordPress and Advanced Custom Fields and Flickity
- Assign IDs to headings in ACF using functions.php
- Unable to declare AOS library in functions
- 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?
- Featured image on archive page based on post type
- Comment function for page
- ACF Filter return value
- Printing medium sized image
- 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?
- restrict uploaded image size and fixed image display size
- Why am I losing image quality on my site?
- Replace image attributes for lazyload plugin on image elements with specific class