Reputation: 3
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');
?>
Upvotes: 0
Views: 180
Reputation: 13283
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.
Upvotes: 1