inserting media into post and then making it a background of some div? (4 posts)

  1. hladdha
    Posted 1 year ago #

    I am trying to create a post where i am uploading some media while creating a new post (i have created a custom post type for this purpose). Now this media doesnt need to be inserted in the post, but to be used in my category template file, wherein i have to set it as a background. Now what i am doing currently is -

    <div class="<?php echo get_post_meta($post->ID, $bgImageClass, true); ?>">

    And then defining the class with the background manually and then uploading the images seperately, Can anyone tell me how do i use wordpress power to achieve this?

  2. lettergrade
    Posted 1 year ago #

    Hi @hladdha, sounds doable but I'm a little fuzzy on details:

    When you say 'defining the class with the background manually,' do you mean in the CSS? In that case, wouldn't the images be uploaded in advance as part of your /images/ folder in the theme?

    If you preset your different classes to correspond with different images, you wouldn't have to upload new images by post, you could just have a custom field that allows you to select a class name.

    OR, is it necessary to be able to upload new images on a post-by-post basis?

  3. hladdha
    Posted 1 year ago #

    yeah, thats what I am doing currently, defining classes manually, uploading the images before and setting the custom field in the post for classname ( above the custom fields name is "bgImageClass" ) so I wanted some alternative to this where I can upload and use on post by post basis

  4. lettergrade
    Posted 1 year ago #

    You can upload the file to your media library, copy the url, and use that as the value for a custom field instead of assigning the background by selecting a class.

    However, you'd need to a) set the background image value as inline css (or in your header) in your template, or b) you'd have to rewrite your template so that the image is behind your content as an image, not a div with a background.

    By the way, if you're using custom images like this regularly, you can make the upload + assign value process even easier by using the Advanced Custom Fields plugin as well, which allows users browse-and-upload functionality right from the edit page.

Topic Closed

This topic has been closed to new replies.

About this Topic