Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have created a custom value called source it is retrieved inside a theme where it goes href="" place. Now when I want to add a new value to source in the Post, it just adds up to the value instead of replace that value I entered. By adds up, I mean that when I hover over the image, the href have two links that I entered. Here is how I retrieve the value:

<a href="<?php echo get_post_meta($post->ID,"source",TRUE); ?>" target="_blank">
<?php the_post_thumbnail(); ?>
</a>

I know how to delete it from the database, but this for a novice use that will barely be able to do that. Any advice ?

Thanks in advance.

PS: I initially didn't find a way to make the post's thumbnail image get a href, for that matter I used thought of this way.

share|improve this question
    
Where is the code that saves the data? –  s_ha_dum Aug 25 '13 at 14:48
    
when I go to every post, in the bottom the custom field is there, I just add new source and later when I come back I just use update. But currently for the posts that was posted before adding that meta-key, they seem to have that default value which I can not over-write –  Observer Aug 25 '13 at 14:56
    
You are using the default "Custom Fields" box? The behavior you describe is non-standard. You should only get one of the values you enter, not both "added" together. –  s_ha_dum Aug 25 '13 at 15:13
    
@s_ha_dum yes, I'm using the default "Custom Fields" box. The only problem is that I can't set default value, or let's say replace the default value. so for example I have this post submitted and it gets default value of : [link]localhost:8080/wp/wordpress/2013/08/read-blog now after I add value to the value, the href becomes the link + whatever I enter. –  Observer Aug 25 '13 at 15:49
    
Got it. Give me a second. I see the problem. –  s_ha_dum Aug 25 '13 at 15:52

1 Answer 1

up vote 0 down vote accepted

I don't think the problem is with the workings of the custom meta field (look into the XY problem). The issue is with the use of relative URLs, whether you realize that that is what you are doing or not.

Left empty, <a href="" target="_blank"> is by default the page you are on. Likewise, echo '<a href="'.get_post_meta($post->ID,"nada",TRUE).'" target="_blank">Hi</a>'; defaults to the page you are on if there is no 'nada' meta key. If there is a key, then the link ends up looking like this:

<a href="yourkeyvalue" target="_blank">

That is a relative URL and will be converted by the browser to "the page you are on" plus "yourkeyvalue". There is where you are seeing things "added" together.

I am not 100% sure what you are trying to accomplish, but I think it is something like this:

$url = get_post_meta($post->ID,"source",TRUE);
if (empty($url)) {
  $url = get_permalink();
} else {
  // make an absolute URL
  $url = site_url($url);
} ?>

<a href="<?php echo $url; ?>" target="_blank">
<?php the_post_thumbnail(); ?>
</a><?php

The only catch is the line following // make an absolute URL. I don't know exactly what kind of information you are inserting or what kind of cleaning/validation/manipulation you need there.

If you only want to print the link if there is a source value then use this:

$url = get_post_meta($post->ID,"source",TRUE);
if (!empty($url)) { ?>
  <a href="<?php echo $url; ?>" target="_blank">
  <?php the_post_thumbnail(); ?>
  </a><?php
} 

Your source meta value has to be an absolute URL-- http://example.com/whatever. (You can probably get away with a protocol relative URL.)

share|improve this answer
    
I wish I could up vote your answer. I took your snippet and started troubleshooting, but I realized that, each and every <a href=""> </a> get the get_permlink(); right away, even If I don't define it. Now I don't know how to avoid that. –  Observer Aug 25 '13 at 17:11
    
I think you missed the point. The point was about avoiding the relative URLs. Do you want to print the link only if there is a source meta_value? –  s_ha_dum Aug 25 '13 at 17:14
    
yes, that was my point, but when I retrieve the link it becomes like this: <a href="thePermLink + source"><img> </a> –  Observer Aug 25 '13 at 17:24
    
Pretty sure you did miss the point of the answer. See the edit. –  s_ha_dum Aug 25 '13 at 17:32
    
My Apologies, Thanks so much for your help! the part I missed was // make an absolute URL. This is what it looks like now and it gives me the clean url with nothing added on to it: $url = get_post_meta($post->ID,"source",TRUE); if (empty($url)) { $url = get_permalink(); } else { $url = ""; . $url; } ` –  Observer Aug 25 '13 at 17:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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