Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The reason you are having problems is because <strong>the_permalink()</strong> and <strong>the_title()</strong> do not return, they echo. Instead use <strong>get_permalink()</strong> and <strong>$post->post_title</strong>. Remember <strong>get_permalink()</strong> requires the post id ($post->ID) as a parameter. I know this is irritating and counter-intuitive, but it's how Wordpress works (see subjectivity in comments to this answer.)</p> <p>This explains why the second example works in your initial question. If you call function that prints from within a string, the echo will output before the end of the string.</p> <p>So this:</p> <pre><code>echo ' this should be before the link: '.the_permalink().' But it is not.'; </code></pre> <p>will not work as expected. Instead, it will output this:</p> <pre><code>http://example.com this should be before the link: But it is not. </code></pre> <p>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.</p> <p><strong>So to round it up, it would look something like:</strong></p> <pre><code>echo '&lt;li&gt;&lt;a href="'.get_permalink($post-&gt;ID).'"&gt;'.$post-&gt;post_title.'&lt;/a&gt;&lt;/li&gt;'; </code></pre> <p>Or as you originally requested, to simply escape them, put a backslash before the quote. Like so (the single quotes have been removed)</p> <pre><code>echo "&lt;li&gt;&lt;a href=\"".get_permalink($post-&gt;ID)."\"&gt;".$post-&gt;post_title."&lt;/a&gt;&lt;/li&gt;"; </code></pre> <p>This is of course assuming you are calling this from <em>within the loop</em>, otherwise a bit more than this would be required to get the desired output.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload