Title: PHP problem with custom fields inside loop
Last modified: August 19, 2016

---

# PHP problem with custom fields inside loop

 *  Resolved [fotokristian](https://wordpress.org/support/users/fotokristian/)
 * (@fotokristian)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/php-problem-with-custom-fields-inside-loop/)
 * I use this code inside the loop:
 *     ```
       <?php $customField = get_post_custom_values("norwegian");
       if (isset($customField[0])) {
           echo "<a class='cssthingie' href=\"<?php the_permalink() ?>\">text in norwegian</a>";
   
       } else {
       	echo "<a class='cssthingie' href=\"<?php the_permalink() ?>\">text in english</a>";
       }
   
       ?>
       ```
   
 * Now, the problem is the `<?php the_permalink() ?>` inside the echo. Is there 
   away to solve this?. The link generated is _website.com/<?php the\_permalink()?
   >_.
 * -Kristian

Viewing 7 replies - 1 through 7 (of 7 total)

 *  [Peter Boosten](https://wordpress.org/support/users/pboosten/)
 * (@pboosten)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/php-problem-with-custom-fields-inside-loop/#post-1319124)
 *     ```
       echo "<a class='cssthingie' href='". the_permalink() . "'>text in norwegian</a>";
       ```
   
 * Since you’re already in an php session (the echo command), you don’t need to 
   start one again.
 * Peter
 *  Thread Starter [fotokristian](https://wordpress.org/support/users/fotokristian/)
 * (@fotokristian)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/php-problem-with-custom-fields-inside-loop/#post-1319128)
 * The problem now is that it actually prints the permalink insted of putting it
   in the anchor.
 *  [Peter Boosten](https://wordpress.org/support/users/pboosten/)
 * (@pboosten)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/php-problem-with-custom-fields-inside-loop/#post-1319138)
 * oh yeah, of course. the_permalink is meant to do all the xhtml work for you.
 * Use this function instead:
 *     ```
       urlencode(get_permalink($post->ID))
       ```
   
 * Peter
 *  Thread Starter [fotokristian](https://wordpress.org/support/users/fotokristian/)
 * (@fotokristian)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/php-problem-with-custom-fields-inside-loop/#post-1319156)
 * I really appreciate you helping me, but now there’s a new problem. The anchor
   now outputs this link
 * _[http://website.com/http://website.com/2009/this-is-a-post](http://website.com/http://website.com/2009/this-is-a-post)_
 * I’ve tried adding / and http:// but it didn’t help.
 *  [Peter Boosten](https://wordpress.org/support/users/pboosten/)
 * (@pboosten)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/php-problem-with-custom-fields-inside-loop/#post-1319165)
 * My mistake: lose the urlencode().
 * `echo "<a class='cssthingie' href='". get_permalink($post->ID) . "'>text in norwegian
   </a>";`
 * Peter
 *  Thread Starter [fotokristian](https://wordpress.org/support/users/fotokristian/)
 * (@fotokristian)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/php-problem-with-custom-fields-inside-loop/#post-1319177)
 * It worked! Thanx for all the help! I owe you a non-alcoholic beverage 😉
 * Kristian 😀
 *  [Peter Boosten](https://wordpress.org/support/users/pboosten/)
 * (@pboosten)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/php-problem-with-custom-fields-inside-loop/#post-1319179)
 * huh?!? Why non-alcoholic? 😉
 * Peter

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘PHP problem with custom fields inside loop’ is closed to new replies.

## Tags

 * [custom fields](https://wordpress.org/support/topic-tag/custom-fields/)
 * [php](https://wordpress.org/support/topic-tag/php/)

 * 7 replies
 * 2 participants
 * Last reply from: [Peter Boosten](https://wordpress.org/support/users/pboosten/)
 * Last activity: [16 years, 4 months ago](https://wordpress.org/support/topic/php-problem-with-custom-fields-inside-loop/#post-1319179)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
