0

I'm not sure if this can be done as I cannot find anything to help me anywhere no matter how hard i try. I'm using Ajax to load to in posts on the press of the button. the posts that are loaded in are formatted by my function inside my functions.php. That all works perfetly,the issue i'm having is the background-image: property needs to be inline css so that it can pull the image from my custom field. This all works perfectly in the template file, however. no matter what I try the functions.php rejects my inline style for background image no matter what I try.

Here's my If statement in it's entirety:

if ($loop -> have_posts()) :  while ($loop -> have_posts()) : $loop -> the_post();


        if ($_SESSION["load_type"] == 'home')
        {
                $out .= '<div class="small-6 large-3 columns end thumb">
                            <div class="grid">
                                <figure class="effect-zoe">
                                    '.get_the_post_thumbnail( get_the_ID(), $size, $attr ).'
                                    <figcaption>    
                                        <h2>'.get_the_title().'</h2>
                                        <hr class="light">
                                        <p class="description">'.get_the_content().'</p>
                                    </figcaption>           
                                </figure>
                            </div>
                        </div>';        
        }

        else

        if ($_SESSION["load_type"] == 'category')

        {



                $out .= '<div class="small-6 large-6 columns end thumb under">
                            <div id="case">

                                <div class="th" id="element" >
                                    <a class="fill-div" href="'. get_permalink($post->ID) .'"></a>
                                </div>
                            </div>


                            <div class="brief">
                                <a href="'. get_permalink($post->ID) .'">'.get_the_title().'</a>    
                                <p class="suppy">Supplier</p>
                                <p>'.get_the_excerpt().'</p>
                                <a class="more-btn" href="'. get_permalink($post->ID) .'">More</a>
                            </div>                  
                        </div>';
        }

        else

        if ($_SESSION["load_type"] == 'product')

        {               
                $out .= '<div class="small-6 large-3 columns">
                            <div class="small-6 large-12 columns end no-pad morepro" style="background-image: url(''.the_field(product_image).'')">
                                <a class="fill-div" href="'. get_permalink($post->ID) .'"></a>
                            </div>
                            <h2 class="product-load">'.get_the_title().'</h2>
                        </div>';

        }


    endwhile;
    endif;
    wp_reset_postdata();
    die($out);

and heres the part of it that I'm having trouble with and crashes the whole site every time I try to load in a background-image:

if ($_SESSION["load_type"] == 'product')

    {               
            $out .= '<div class="small-6 large-3 columns">
                        <div class="small-6 large-12 columns end no-pad morepro" style="background-image: url(' '.the_field(product-image).' ')">
                            <a class="fill-div" href="'. get_permalink($post->ID) .'"></a>
                        </div>
                        <h2 class="product-load">'.get_the_title().'</h2>
                    </div>';

    }


enter code here

Can anyone see where I'm going wrong?

  • 1
    Should you be escaping those single quotes? style="background-image: url(' '.the_field(product-image).' ')" e.g. style="background-image: url(\' '.the_field(product-image).'\')" – deadlyhifi Aug 13 '15 at 14:19
  • 2
    aside from your quote issues, the_field echoes content, you need to use get_field, which returns. – Milo Aug 13 '15 at 14:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.