0

I am trying to dynamically add margin-bottom property to my widget. For example, I am inputting the value "50px" from my widget and want to use this value to adjust margin-bottom property for the widget. The main purpose is to dynamically add space between my widgets.

This is what I have done so far. Its getting me a white screen of death. I think the problem is at margin-bottom: $variable.

Is it allowed to add inline css in widget function? How do I get the $space valye in the div tag?

    function widget( $args, $instance ) {
        $space= "200px";
        echo "<html>";
        echo "<body>";
        echo "<div style=margin-bottom: <?echo $space; ?>>";
        if (!empty($photos)):
             //Loop through each attachment..

             foreach ($photos as $photo_id => $photo) : 

                 $url = wp_get_attachment_url($photo_id);
                 $position = strpos($content, $url);

                 if($position){
                    echo wp_get_attachment_image($photo_id, 'thumbnail') ;
                 }
             endforeach;
         endif; 
         echo "</div>";
         echo "</body>";
         echo "</html>";
3
  • If that causes white screen please always enable WP_DEBUG and include error messages verbatim into your question.
    – Rarst
    Nov 22 '15 at 12:36
  • You are missing a semi-colon after the third echo.
    – s_ha_dum
    Nov 22 '15 at 15:23
  • Thank you for pointing it out. I corrected the third echo statement. I also noticed that I was write div style tag with wrong syntax. I have corrected it. But its still not increasing space between my widgets. Am I writing the style correctly? is there another way to do this?
    – Beans
    Nov 22 '15 at 19:17
0

Googled some more and found out that closing php tags and writing html as it is a better practice as compared to writing html inside. Made the following changes to the code shared above:

    $space=200px;
    ?>
      <div style="margin-bottom:<?php echo $space; ?>" >
    <?php
      //rest of the code
    ?>
      </div>
    <?php
      //rest of the code

Space between widgets added!

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.