• Resolved Sam

    (@forbiddenchunk)


    Hi all,

    I need some help please, I’m trying to add a featured image as a background in post that is in a page (confusing I know).

    All the tutorials I can find only allows me to add it when the post is a “page” but I’ve got some code already that pulls through an specific post into a page and want the background in the div to be the featured image but can’t find anywhere that will show me how!

    Can anyone help

Viewing 14 replies - 1 through 14 (of 14 total)
  • Hello.

    Kindly share with us a link to your blog page.

    Samuel.

    Thread Starter Sam

    (@forbiddenchunk)

    Hi Samuel Elh,

    Unfortunately I’m working locally so there is not link :-s

    That is okay, then let us know the theme name if available

    Thread Starter Sam

    (@forbiddenchunk)

    It’s a custom theme sorry…

    [ Moderator note: code fixed. Please wrap code in the backtick character or use the code button. ]

    ---
    <div class="medium-4 columns noPad" style="background-color:green">
                    <div class="sectorPanel">
                        <? php
                        $post_id = 43;
                        $queried_post = get_post($post_id);
                        ?>
                        <h4 class="text-center primaryColor"><?php echo $queried_post->post_title; ?></h4>
                        <p class="text-center whiteColor"><?php echo $queried_post->post_content; ?></p>
                    </div>
                </div>
    ---

    This is the code (if it posts)

    Hi!

    That is fine, I was just checking to know which CSS selectors to apply. I take it as you are familiar with CSS, so add the following code to the bottom of your custom thene’s functions.php file:

    add_filter('the_content', function( $content ) {
    
    	global $post;
    	$thumb = has_post_thumbnail() ? wp_get_attachment_url( get_post_thumbnail_id($post->ID) ) : false;
    
    	if( $thumb ) : ?>
    
    		<style type="text/css">
    			#post-<?php echo $post->ID; ?> {
    				background: transparent url('<?php echo $thumb; ?>');
    			}
    		</style>
    
    	<?php endif;
    
    });

    Style the backgrounds further, such as adding background size, fixed scroll, etc.

    Make sure your theme adds the post ID in the post container element, and other necessary classes, you might use theme check plugin..

    Edit: Remember to use wp_get_attachment_url( get_post_thumbnail_id($queried_post->ID) ) to get the thumbnail URL.

    Thread Starter Sam

    (@forbiddenchunk)

    Hi, thanks for your help but feeling stupid because it’s not worked and still not pulling anything through…

    I’ve made sure all my posts have a featured image and yet note :-s

    Have you looked through your code? inspect HTML through dev tools, there is a part where it says background: transparent url(' etc, it is just a matter of CSS selectors..

    Or if you want to set the background for the h4, then this might be what you are after:

    <h4 class="text-center primaryColor" style="background-image:<?php echo wp_get_attachment_url( get_post_thumbnail_id($queried_post->ID) ); ?>"><?php echo $queried_post->post_title; ?></h4>

    Thread Starter Sam

    (@forbiddenchunk)

    Yeah I had another look and still nothing sorry, what I’m hoping to achieve is;

    <div class="medium-4 columns noPad" style="background-image:url('FEATURED IMAGE HERE')">
                    <div class="sectorPanel">
                        <? php
                        $post_id = 43;
                        $queried_post = get_post($post_id);
                        ?>
                        <h4 class="text-center primaryColor"><?php echo $queried_post->post_title; ?></h4>
                        <p class="text-center whiteColor"><?php echo $queried_post->post_content; ?></p>
                    </div>
                </div>

    Try:

    <div class="medium-4 columns noPad" style="background-image:url('<?php echo wp_get_attachment_url( get_post_thumbnail_id(get_the_ID()) ); ?>')">
        <div class="sectorPanel">
            <? php
            $post_id = 43;
            $queried_post = get_post($post_id);
            ?>
            <h4 class="text-center primaryColor"><?php echo $queried_post->post_title; ?></h4>
            <p class="text-center whiteColor"><?php echo $queried_post->post_content; ?></p>
        </div>
    </div>
    Thread Starter Sam

    (@forbiddenchunk)

    Nope ??

    Just shows up in the code as

    background-image: url ('');

    How about moving the query code to the top:

    <?php
    $post_id = 43;
    $queried_post = get_post($post_id);
    ?>
    <div class="medium-4 columns noPad" style="background-image:url('<?php echo wp_get_attachment_url( get_post_thumbnail_id($queried_post->ID) ); ?>')">
        <div class="sectorPanel">
            <h4 class="text-center primaryColor"><?php echo $queried_post->post_title; ?></h4>
            <p class="text-center whiteColor"><?php echo $queried_post->post_content; ?></p>
        </div>
    </div>
    Thread Starter Sam

    (@forbiddenchunk)

    YES!! That worked perfectly!

    Thank you so much for your help and patience ??

    Cool. it is alright. Have fun coding the rest of it, and kindly mark this as resolved if you feel it is.

    Have a nice day!

    Samuel

    Thread Starter Sam

    (@forbiddenchunk)

    Resolved ??

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘How to add featured image as background image’ is closed to new replies.