Correct syntax 7 lines of php code for wordpress template

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • David Radovanovic
    New Member
    • Aug 2010
    • 4

    Correct syntax 7 lines of php code for wordpress template

    WordPress 3.0
    running on Ubuntu

    Here's the code that results errors:

    Code:
    <img src="<?php
    $url = '<?php if(function_exists('get_custom_field_data')) {
    	get_custom_field_data('web', true);
    } ?>';
    $width = 300;
    echo bm_mshot ($url, $width);
    ?>" />
    Thanks
  • TheServant
    Recognized Expert Top Contributor
    • Feb 2008
    • 1168

    #2
    There is a <?php in the PHP. Line 2-4 should be something like:
    Code:
    if(function_exists('get_custom_field_data')) { 
        $url = get_custom_field_data('web', true); 
    } else {
        $url = '';
    }
    Also, why doesn't your bm_mshot() function return a string? What does it return? What are you echo-ing?

    And, I would try and separate your logic from your display. For example:
    Code:
    <?php
    if(function_exists('get_custom_field_data')) { 
        $url = get_custom_field_data('web', true); 
    } else {
        $url = '';
    }
    $width = 300;
    
    /* bm_mshot code which returns a string */
    
    ?>
    
    <body>
    ...
    <img src="<?php bm_mshot($url, $width); ?>" />
    ...

    Comment

    • David Radovanovic
      New Member
      • Aug 2010
      • 4

      #3
      Firstly, thanks for your response.

      I added:

      Code:
      <?php if(function_exists('get_custom_field_data')) {
      	get_custom_field_data('web', true);
      } ?>
      as a way of writing a custom field (web) which is a url to that part of the bm_mshot function. BTW, I'm probably not using the correct php lingo.

      So here goes:

      I'm trying to wrap the img tag around the result of the bm_shot code. The problem is the way I'm just pasting the get_custom_fiel d_data code in what was where a hard-coded url belonged.


      Code:
      <img src=
      
      "<?php
      $url = '<?php if(function_exists('get_custom_field_data')) {
      	get_custom_field_data('web', true);
      } ?>';
      $width = 300;
      echo bm_mshot ($url, $width);
      ?>"
      
      />

      Comment

      • TheServant
        Recognized Expert Top Contributor
        • Feb 2008
        • 1168

        #4
        I understand what you're trying to do, but you cannot have:
        Code:
        "<?php 
        $url = '<?php ...
        You see you have tried to parse "<?php" in PHP (while it's reading/running PHP). This will not work. Have a read through the code I wrote. I did not change much of what you wrote.

        The other problem which I addressed is having a function within "". You cannot have that. It will try and make that a string. The correct logic is to have variables set within the if clause.

        Finally, you have "echo bm_mshot($url,$ width);". Please past your bm_mshot code so that I can see what you're trying to return? Is your function returning a string?

        Comment

        • David Radovanovic
          New Member
          • Aug 2010
          • 4

          #5
          This seems to work:

          Code:
          <?php
          
          $url = get_post_meta($post->ID, 'web', true);
          $width = 300;
          
          ?>
          
          <img src="<?php echo bm_mshot ($url, $width); ?>" />
          Thanks so much for your help

          Comment

          • TheServant
            Recognized Expert Top Contributor
            • Feb 2008
            • 1168

            #6
            No problem. See you next time.

            Comment

            Working...