PDA

View Full Version : Link with existing PHP code



OnlineD
Oct 14th, 2011, 03:54 PM
I am trying to insert a link within some existing PHP code but can not get the syntax correct.

This is the existing code

$image_html = "<p style='float:left'><img src='". $resized ."' alt='' width='$width' height='$height' class='shadow alignleft' /></p>";

and I am trying to do something like this so the image becomes clickable

$image_html = "<p style='float:left'><a href="<?php the_permalink(); ?>"><img src='". $resized ."' alt='' width='$width' height='$height' class='shadow alignleft' /></a></p>";

Can anyone show me how to correctly code this?

Fou-Lu
Oct 14th, 2011, 03:57 PM
This is already within PHP code, so you don't want to try opening and closing PHP code within it.

$image_html = "<p style='float:left'><a href='" . the_permalink(); . "'><img src='". $resized ."' alt='' width='$width' height='$height' class='shadow alignleft' /></a></p>";

OnlineD
Oct 16th, 2011, 01:58 PM
Thanks for this but it is not working. If I use this code then the page just displays as a blank page. Is there a small error in the syntax perhaps?

_Aerospace_Eng_
Oct 16th, 2011, 02:06 PM
Just a small one but it is one that is very basic. Remove the semi-colon here

<a href='" . the_permalink(); . "'>

OnlineD
Oct 16th, 2011, 02:12 PM
Almost there I think but I am now seeing the full URL as text above the image, and although the image itself is now 'clickable' every image on the page has the text url above it and only links to the root URL.

Fou-Lu
Oct 16th, 2011, 05:09 PM
Just a small one but it is one that is very basic. Remove the semi-colon here

<a href='" . the_permalink(); . "'>

Oops my bad.


Almost there I think but I am now seeing the full URL as text above the image, and although the image itself is now 'clickable' every image on the page has the text url above it and only links to the root URL.

That sorta smells that the_permalink() returns more than just the address itself. What does the HTML source show?

OnlineD
Oct 16th, 2011, 05:44 PM
OK, good idea - look at the source!

Here is the source - I now see the problem but not sure how to fix it...

<div class="post-entry">http://davidshukman.com/blog/just-add-dna-a-new-technique-for-making-stem-cells/<p style='float:left'><a href=''><img src='http://davidshukman.com/wp-content/uploads/2011/10/NYSCF.jpg' alt='' width='290' height='190' class='shadow alignleft' /></a></p><div style="float:left;width:330px">

Perhaps I should alos post the full bit of code on the page that contains the snippet I am trying to manipulate...

<div class="post-entry<?php echo $class; ?>">
<?php
$html_content = get_the_content_with_formatting();
$only_text = strip_single_tags($tags, $html_content);
$images = get_all_img_urls($html_content);
$image = $images[0];

// now lets check widht of image. If it is bigger than 540 - resize it
if ($image != "")
{
$width = $air_img_w;
$height = $air_img_h;
$resized = generate_thumb($images[0], $width, $height, true);
$image_html = "<p style='float:left'><a href='" . the_permalink() . "'><img src='". $resized ."' alt='' width='$width' height='$height' class='shadow alignleft' /></a></p>"; }
else
{
$image_html = "";
$width = 0;
}

Fou-Lu
Oct 16th, 2011, 06:53 PM
Does the function the_permalink return a result?
From what this source shows, it prints the result directly from the function. So that means you'll need to either rewrite the the_permalink function to return a result, or call it directly by itself.


print '<p style="float:left"><a href="';
the_permalink();
print '"><img... /></a></p>';

Which is cumbersome. The only other alternative is to use output_buffering to capture the results of the_permalink call into a variable.

OnlineD
Oct 16th, 2011, 07:30 PM
Seems to me the source should look something like this

<div class="post-entry"><p style='float:left'><a href=''http://davidshukman.com/blog/just-add-dna-a-new-technique-for-making-stem-cells/"><img src='http://davidshukman.com/wp-content/uploads/2011/10/NYSCF.jpg' alt='' width='290' height='190' class='shadow alignleft' /></a></p><div style="float:left;width:330px">

but something in the code is causing the URL to get put in the wrong place. This is way above my pay grade so can not see why it might be happening.

Fou-Lu
Oct 16th, 2011, 07:39 PM
Yeah, it happens since the result is printed from the function.
Prints work in stack order. So a print is placed on the stack, and if within that print is another print its stacked on top of the current stack, and so forth. Then when complete, it bubbles down the stack and prints from the inner most to the outer most. That is why you see the URL before where it is intended, which indicates that the_permalink doesn't actually return a result, it prints it.
I recognize this function from other posts though, so it would make it a part of a system. Look for other methods or functions instead to retrieve the url as a return. Otherwise, the only way to capture it in a variable would be with:


ob_start();
the_permalink();
$link = ob_get_contents();
ob_end_clean();