0

I have searched this problem for a while now, maybe it is simple or maybe not. I could not figure out how to get this to work.

My goal outcome would be a hyperlink related to the post meta with some styling like so.

<a href="href_link" style="color: #e67300" rel="nofollow"> Check out the r_title here!</a>

The code I have is:

<?php
$rtitle1 = get_post_meta($post->ID, 'r_title', true);
$rlink1 = get_post_meta($post->ID, 'href_link', true);
    function testfunction() {

    $output .= '<a href=\"'$rlink1'\" style=\"color: #e67300\" rel=\"nofollow\">';
    $output .= ' Check out the '$rtitle1' here!</a>';

    return $output;
    }
add_shortcode('shortcode', 'testfunction');
?>
code_burgar
  • 12,025
  • 4
  • 35
  • 53

1 Answers1

1

There are several problems with your code.

The first problem is with string concatenation. When you want to glue strings together you need to use the concatenation operator (the dot: .):

$end = 'a string';
$start = 'This is ';
$string = $start.$end;

If you just juxtapose variables and strings (or any other scalar types) then you will get errors:

$end = 'a string';
$string = "This is "$end; // Error!

The second problem is that you are using two variables ($rtitle1 and $rlink1) that are in the global scope. If you want to use global variables inside a function then you need to declare them as global inside the function:

$globalVar = 'test';
function test() {
  global $globalVar;
  echo $globalVar;
}

The third problem is that you forgot the ending closing parenthesis, ), for the get_post_meta() function:

$rtitle1 = get_post_meta($post->ID, 'r_title', true;
$rlink1 = get_post_meta($post->ID, 'href_link', true;

They should be like this:

$rtitle1 = get_post_meta($post->ID, 'r_title', true);
$rlink1 = get_post_meta($post->ID, 'href_link', true);

Before you think about asking for help you should look at the error messages that you get. If you have not seen the error message before then Google it. The best way to learn something is to find the solution on your own. Asking questions is for when you have tried finding a solution but you cannot find it.

Sverri M. Olsen
  • 13,055
  • 3
  • 36
  • 52
  • Sorry, I wills start doing that but I'm not exactly sure how. How could I find errors within my code from testing them on my wordpress site? – user2046471 Feb 06 '13 at 11:06
  • If there are errors in your code then they will appear on your site. They usually look something like: "`Parse error: syntax error, unexpected T_IF in ...`". If you do not see any errors then look at the answer to this question: http://stackoverflow.com/questions/5438060/showing-all-errors-and-warnings – Sverri M. Olsen Feb 06 '13 at 11:11