views:

416

answers:

5

I want to escape double quotes from this string:

<li><a href="the_permalink()">the_title()</a></li>

The following works fine:

echo '<li><a href="';
echo the_permalink();
echo '">';
echo the_title();
echo '</a></li>';

... but how do I get them all in one single statement?

Thanks

+2  A: 
printf( '<li><a href="%s">%s</a></li>', the_permalink(), the_title() );
meouw
Doesn't work for some reason, links are not parsed correctly.
Nimbuz
Do your functions return strings? - what do you mean not parsed correctly?
meouw
I'm not sure, how do I test? Well, it doesn't show as a link, the link itself is shown followed by title, all text, no clickable link.
Nimbuz
post an example of the link that is generated - there must be another problem if the other solutions on this page don't work either - are you sure your original works?
meouw
Example output: http://localhost/tone/2010/02/07/review-7/Review #7--- Yes, mine works.
Nimbuz
The example you posted is working
meouw
A: 

Using concatenation (line breaks not required):

echo '<li><a href="'
 . the_permalink()
 . '">'
 . the_title()
 . '</a></li>';
Mark E
@Mark: PHP uses a '.' for string concatenation! Your statement would attempt to add the numeric values of the strings together
meouw
@meouw, caught it myself a few minutes ago, too much java lately I guess...
Mark E
... doesn't work as well! Strange!
Nimbuz
@Mark: hehe - I got into trouble the other day for declaring an array in JavaScript literal fashion
meouw
A: 
echo "<li><a href=".the_permalink().">".the_title()."</a></li>";
TeknoSeyfo
... doesn't work either.
Nimbuz
+3  A: 
echo '<li><a href="', the_permalink(), '">', the_title(), '</a></li>';
Kai Chan
WORKS! why don't the "." (dots) work?
Nimbuz
The dot version evaluates all items (e.g. calling the functions) to be concatenated before calling echo. The comma version evaluates and prints the parameters, one after another, while running the echo construct. (A comment in the PHP manual -- http://www.php.net/manual/en/function.echo.php#78898 -- explains this better.) Since, you do not want the_permalink() to be evaluated before echo is called and '<li><a href="' is printed, the dot version is not what you want.
Kai Chan
+5  A: 

The reason you are having problems is because the_permalink() and the_title() are echoing functions. Instead use get_permalink() and $post->post_title. Remember get_permalink() requires the post id ($post->ID) as a parameter.

This explains why the second example works in your initial question. If you call an echoing function from within a string, the echo will output before the end of the string.

So this:

echo 'static content '.echoing_function().' more static content';

will not work as expected, instead, it will output this:

*echoing_function output* static content more static content

In PHP you can use both single and double quotes. When I am building strings with HTML I generally begin the string with a single quote, that way, I can use HTML-compatible double quotes within the string without escaping.

So to round it up, it would look something like:

echo '<li><a href="'.get_permalink($post->ID).'">'.$post->post_title.'</a></li>';

Or as you originally requested, to simply escape them, put a backslash before the quote. Like so (the single quotes have been removed)

echo "<li><a href=\"".get_permalink($post->ID)."\">".$post->post_title."</a></li>";

This is of course assuming you are calling this from within the loop, otherwise a bit more than this would be required to get the desired output.

dskvr
That's the thing I hate most about wordpress: functions are supposed to *return* values not *print* them; I can print return values myself.
kemp
I agree. However, these functions were created for people who know little about PHP (which is 95% of the WP community). the_title() is more newbie friendly than echo $post->post_title. This is one of the many reasons WP has been so successful. For me, it comes second nature, and I use both versions, depending on placement.
dskvr
True, but `echo the_title()` isn't much harder, `echo` is familiar to anyone I think, and leaves more freedom to the developer. Functions were refactored in later version to support both echo and return, but there is hardly any consistency in that.
kemp