I am trying to get a link together using 2 variables but the output is the link and title but no html / clickable link appearing.
I’m getting something link this:
http://www.mydomain.com/post1/post_title_here
Here is the code:
echo '<a href="'.the_permalink().'">'.the_title().'</a>';
Can anyone help please?
Thanks
UPDATE:
Here’s the whole block of code:
<div id="MyBlock1">
<?php
$query = new WP_Query('posts_per_page=5');
while( $query ->have_posts() ) : $query ->the_post();
echo '<li>';
echo '<a href="'.the_permalink().'">'.the_title().'</a>';
echo '</li>';
endwhile;
wp_reset_postdata();
?>
</div>
That’s because the wordpress functions
the_permalink()
andthe_title()
display the respective outcomes already they need not be echoed. If you want functions that return the values, you have to useget_permalink()
andget_the_title()
instead.So either do:
or
Both will work.
Here’s a checklist for debugging:
1.) Is
the_title()
returning an empty string? (You can check by looking at the html source)2.) Are you echoing this inside of the body tag?
3.) Is this being echoed in a hidden html element?
In this sort of situation, you’ll want to use
get_permalink
instead ofthe_permalink
andget_the_title
instead ofthe_title
.WordPress
the_*
functions do a directecho
call, whereas theget_*
functions return a value that you can use for further processing, like the concatenation you’re doing.(also note the inconsistent naming conventions – this can be a pain)
You could use the corresponding get_* versions:
See the codex reference for more
You need to absolutely make sure that
.the_title().
is definately bieng set a value. If it isn’t, then there will be no displayed HTML as there is no text for the anchor tag. Just a thought (i’ve done it many times, tryprint_f();
ing the the_title(). Hope it helped.