SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Php code for WordPress

    I am doing a WordPress theme that uses a php random image file. I want to know how to adapt this code to WordPress so I can use the bloginfo for the paths to the url and image folder instead of hard coding them in.
    I am currently using
    PHP Code:
    <?php
    $img_rand 
    = array(
       
    => array(
            
    'img' => 'image5.jpg'// Add file name, type, even path if needed
            
    'wth' => '751',        // Width if not a fixed variable
            
    'hgt' => '109',        // Height if not a fixed variable
            
    'alt' => 'ISC logo'  // Alt text... IF needed (consider this) 
    ),                              
       
    => array(
            
    'img' => 'image9.jpg',
            
    'wth' => '751',        // Width if not a fixed variable
            
    'hgt' => '109',        // Height if not a fixed variable
            
    'alt' => 'ISC logo'  // Alt text... IF needed (consider this) 
    ),
       
    => array(
            
    'img' => 'image11.jpg',
            
    'wth' => '751',        // Width if not a fixed variable
            
    'hgt' => '109',        // Height if not a fixed variable
            
    'alt' => 'ISC logo'  // Alt text... IF needed (consider this) 
    ),
    );
    ?>
    <?php
    $img 
    array_rand($img_rand);
    echo(
    '   <a href="http://localhost/attorney/"><img src="http://localhost/attorney/wp-content/themes/attorney/images/headers/'.$img_rand[$img]['img'].'" width="'.$img_rand[$img]['wth'].'" height="'.$img_rand[$img]['hgt'].'" alt="'.$img_rand[$img]['alt'].'" /></a>

    '
    );
    ?>
    But I would like it to use
    PHP Code:
    <?php
    $img_rand 
    = array(
       
    => array(
            
    'img' => 'image5.jpg'// Add file name, type, even path if needed
            
    'wth' => '751',        // Width if not a fixed variable
            
    'hgt' => '109',        // Height if not a fixed variable
            
    'alt' => 'ISC logo'  // Alt text... IF needed (consider this) 
    ),                              
       
    => array(
            
    'img' => 'image9.jpg',
            
    'wth' => '751',        // Width if not a fixed variable
            
    'hgt' => '109',        // Height if not a fixed variable
            
    'alt' => 'ISC logo'  // Alt text... IF needed (consider this) 
    ),
       
    => array(
            
    'img' => 'image11.jpg',
            
    'wth' => '751',        // Width if not a fixed variable
            
    'hgt' => '109',        // Height if not a fixed variable
            
    'alt' => 'ISC logo'  // Alt text... IF needed (consider this) 
    ),
    );
    ?>
    <?php
    $img 
    array_rand($img_rand);
    echo(
    '   <a href="<?php bloginfo('url'); ?>"><img src="<?php bloginfo('template_url'); ?>/images/headers/'.$img_rand[$img]['img'].'" width="'.$img_rand[$img]['wth'].'" height="'.$img_rand[$img]['hgt'].'" alt="'.$img_rand[$img]['alt'].'" /></a>

    '
    );
    ?>
    This doesn't work. Is there another way to write it so it works with WordPress?

  2. #2
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Location
    Michigan
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try
    PHP Code:
    echo '   <a href="'.bloginfo('url').'"><img src="'.bloginfo('template_url').'/images/headers/'.$img_rand[$img]['img'].'" width="'.$img_rand[$img]['wth'].'" height="'.$img_rand[$img]['hgt'].'" alt="'.$img_rand[$img]['alt'].'" /></a>'
    The way you had it, it wasn't calling the bloginfo function.
    Last edited by darkecho; Sep 12, 2011 at 20:06. Reason: making the code box a little bigger

  3. #3
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is what your code outputs
    HTML Code:
    <p class="logo">http://localhost/attorneyhttp://localhost/attorney/wp-content/themes/attorney   <a href=""><img src="/images/headers/image5.jpg" width="751" height="109" alt="ISC logo" /></a></p>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •