简体   繁体   中英

Wordpress - get link to a post

I want to display the thumbnail of a post. If the user clicks the thumbnail he gets directed to that post. But when clicking it I get a 403 ERROR. I'm new to php what is wrong?

PHP

<?php
query_posts('cat=5');
while (have_posts()) : the_post();

 the_title();

echo '<a href="<?php the_permalink();?>">';
    the_post_thumbnail();
echo '</a>';
endwhile;
?>

您的函数永远不会被解释,它被视为文本,您可以编写:

echo '<a href='.the_permalink().'>';
<?php the_permalink();?>

Displays the URL for the permalink to the post currently being processed in The Loop. This tag must be within The Loop, and is generally used to display the permalink for each post, when the posts are being displayed. Since this template tag is limited to displaying the permalink for the post that is being processed, you cannot use it to display the permalink to an arbitrary post on your weblog. Refer to get_permalink() if you want to get the permalink for a post, given its unique post id.

<?php echo get_permalink(1); ?>

http://codex.wordpress.org/Function_Reference/the_permalink

I think it may be your syntax.

Try changing: echo '<a href="<?php the_permalink();?>">';

to: echo '<a href="'; the_permalink(); echo '"';

The reason being that you are trying to reinitialize php even though you must have already started the syntax if you are using echo. So you do not need <?php ?>

Here is another forum for reference.

just replace your code

echo '<a href="<?php the_permalink();?>">';

TO :

<a href="<?= the_permalink();?>">

your code will be :

<?php
query_posts('cat=5');
while (have_posts()) : the_post();

 the_title();
?>
<a href="<?= the_permalink();?>"><?= the_post_thumbnail(); ?></a>
<?php
endwhile;
?>

You have mistake in this section:

echo '<a href="<?php the_permalink();?>">';

Replace that line on this:

echo '<a href="' . the_permalink() . '">';

And don't use short echo <?=...?> , it can be turned off by your hosting provider.

the_permalink() function already have echo , so you should not put second echo .

function the_permalink() {
    /**
     * Filter the display of the permalink for the current post.
     *
     * @since 1.5.0
     *
     * @param string $permalink The permalink for the current post.
     */
    echo esc_url( apply_filters( 'the_permalink', get_permalink() ) );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM