the_title
output contents itself. You need to use get_the_title()
which returns the content
Try this:
echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
Question
I'm having trouble displaying a title as a link in a WordPress function.
If I code it like this:
function my_popular_posts($count) {
$query = new WP_Query( array(
'orderby' => 'comment_count',
'order' => 'DESC',
'posts_per_page' => $count));
if($query->have_posts()) {
echo '<ul>';
while($query->have_posts()) : $query->the_post();
**THIS LINE --> echo '<li><a href='.get_permalink().'> .the_title(). </a></li>';
endwhile;
echo '</ul>';
} else {
echo "<p>No popular posts found<p>";
}
}
At run-time, the link displays as ".the_title()"
If I code it this way:
echo '<li><a href='.get_permalink().'>'.the_title().'</a></li>';
It will display the title, but not as a link.
Any Ideas? Your help will be appreciated.
Thnx!
Solution
the_title
output contents itself. You need to use get_the_title()
which returns the content
Try this:
echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
OTHER TIPS
I think you just need to use double quotes for href=""
to let the link work
echo '<li><a href="'.get_permalink().'">'.the_title().'</a></li>';
You are missing closing quotation marks on that line.
Note the addition of the closing quotes:
while($query->have_posts()) : $query->the_post();
echo '<li><a href=' . get_permalink() . '>' . get_the_title() . '</a></li>';
endwhile;
Additionally, you markup should include quotations around the link url, as edited below:
echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';