Problem with PHP Syntax
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开发者_如何学Python 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()
and the_title()
display the respective outcomes already they need not be echoed. If you want functions that return the values, you have to use get_permalink()
and get_the_title()
instead.
So either do:
<div id="MyBlock1">
<?php
$query = new WP_Query('posts_per_page=5');
while( $query ->have_posts() ) : $query ->the_post();
echo '<li>';
echo '<a href="'.get_permalink().'">'.get_the_title().'</a>';
echo '</li>';
endwhile;
wp_reset_postdata();
?>
</div>
or
<div id="MyBlock1">
<?php
$query = new WP_Query('posts_per_page=5');
while( $query ->have_posts() ) : $query ->the_post();
echo '<li><a href="';
the_permalink();
echo '">';
the_title();
echo '</a></li>';
endwhile;
wp_reset_postdata();
?>
</div>
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?
echo '<a href="'.the_permalink().'">'.the_title().'</a>';
In this sort of situation, you'll want to use get_permalink
instead of the_permalink
and get_the_title
instead of the_title
.
echo '<a href="'.get_permalink().'">'.get_the_title().'</a>';
WordPress the_*
functions do a direct echo
call, whereas the get_*
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:
echo '<a href="' . get_permalink() . '">' . get_the_title() . '</a>';
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, try print_f();
ing the the_title(). Hope it helped.
精彩评论