• Hi,

    I’m trying to design a component for a page, and I can’t get the php correct on it. This is the code that I have so far:

    <?php
    $b[1] = echo "<img src=\"";
    echo bloginfo('template_url') ;
    echo "/images/topimg.jpg\" alt=\"{$nav_title}\" />";
    ?>

    I want to call the template url into the line, but I’m missing something apparently. The problem lies in the $b[1] = echo "<img src=\""; line. It works fine if I remove the $b[1] part. This is no good.

    I can’t get this to work, and I’m tired of trying to figure this out. Can someone help me please?

    Thank you

Viewing 14 replies - 1 through 14 (of 14 total)
  • If you are trying to set $b[1] you should not be echoing anything. It should be simply (assuming I didn’t screw up the quotes):

    <?php
    $b[1] = "<img src=\"".get_bloginfo('template_url')."/images/topimg.jpg\" alt=\"{$nav_title}\" />";
    ?>

    And notice that you don’t want bloginfo(), you want get_bloginfo().

    Thread Starter miguel57

    (@miguel57)

    Hey apljdi,

    Thank worked fabulously! You’re too awesome! I don’t know how I was missing that. Guess I needed more sleep than I thought!

    Thread Starter miguel57

    (@miguel57)

    New question!

    How do I get the image to link to an external url? I don’t want to use an absolute url.

    Thread Starter miguel57

    (@miguel57)

    My problem is that I want to link the image with the above code

    <?php
    $b[1] = "<img src=\"".get_bloginfo('template_url')."/images/topimg.jpg\" alt=\"{$nav_title}\" />";
    ?>

    to an external page http://www.sitename.com with PHP, without having to type out the entire url. Is there even a way to do this with PHP?

    Can you give me another example? Do you want them to go to different domains or just one?

    Is there a way to tell PHP to link to a particular place without telling PHP what that place is? No. There isn’t– not if you are talking about a page on a different domain. Relative urls only work within a single domain and they don’t work reliably within WordPress at all, except for stylesheets. You can put the URL, or part of it, in a variable or a constant so that you only have to type out in full once though.

    Thread Starter miguel57

    (@miguel57)

    g3legacy, I only want it to go to one domain.

    Okay apljdi, can you give me an example of how to apply the url as a variable or a constant?

    do you mean something like storing the domain in a variable, then echo-ing that along with the rest of the URL after it? So changing that variable would change the domain you pointed too?

    define('REUSABLE_URL','http://another.domain.com');

    Now echo REUSABLE_DOMAIN will print out http://another.domain.com. All you’d have to do is add the rest of the path to the end of it. Using a constant as above means that REUSABLE_URL can’t be changed by the script during execution. If you were to store the URL in a variable, like this, $reusable_url = "http://another.domain.com"; you’d be able to echo it with echo $reusable_url; but the url could be changed by the script during execution.

    Thread Starter miguel57

    (@miguel57)

    g3legacy, that’s exactly what I want to do.

    aplijdi, I’ll try your suggestion, but can you give me an example of what the code should look like when I’m done?

    Of course, I screwed up my last post πŸ™‚

    “Now echo REUSABLE_DOMAIN will print…” should have been “Now echo REUSABLE_URL will print…”

    Put define('REUSABLE_URL','http://another.domain.com'); in your theme’s functions.php It should then be available nearly anywhere in the WordPress framework. When you want to create a link do something like <img src="<?php echo REUSABLE_URL; ?>/images/image.jpg" alt="" /> It is really fairly simple.

    Thread Starter miguel57

    (@miguel57)

    apljdi, using this code <img src="<?php echo REUSABLE_URL; ?>/images/image.jpg" alt="" /> seems like it would link to an image folder in the other domain.

    I want to link the image in this code

    <?php
    $b[1] = "<img src=\"".get_bloginfo('template_url')."/images/topimg.jpg\" alt=\"{$nav_title}\" />";
    ?>

    to another domain. Although I will tell you that I like the other solution that you gave too. Gave me an idea for something else. πŸ™‚

    miguel57, you would have to create an anchor around the image then.

    <?php
    $b[1] = "<a href=\"{REUSABLE_URL}/path/to/destination\" /><img src=\"".get_bloginfo('template_url')."/images/topimg.jpg\" alt=\"{$nav_title}\" /></a>";
    ?>
    Thread Starter miguel57

    (@miguel57)

    apljdi, THAT sir (or Madam?), is the solution that I was looking for. Thank you!

    I owe you a shirt! Shoot me an email, and let me know your size and where to send it. And if you have a Twitter account, follow me @miguelpedroza.

    Thanks again!

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Can't get PHP right for page. Please help!’ is closed to new replies.