简体   繁体   English

为什么我不能在WordPress中将the_permalink()与echo一起使用?

[英]Why can't I use the_permalink() with echo in WordPress?

I have the following code: 我有以下代码:

<?php
    query_posts(array(
        'posts_per_page'   => -1,
        'post_type' => 'sample-letter',
        'order' => 'ASC'
    ));

    while(have_posts()){
        the_post();

        echo '<div class="col-md-9"><span class="title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">title</a></span><br />';
    }

    wp_reset_query();
?>

It works great but the problem is, I can't use: 它很好用,但是问题是,我不能使用:

<?php the_permalink() ?>

INSIDE the echo statement. echo语句中。 It is a simple link, and rather than render the link url, it outputs: 它是一个简单的链接,而不是呈现链接URL,而是输出:

http://sitename.com/<?php the_permalink() ?>

Instead of: 代替:

http://sitename.com/thelink

How can I make this loop work without the echo ? 如何在没有echo情况下使此循环起作用? Is that actually the problem at all? 这真的是问题所在吗?

Use the echo part like; 使用类似的echo部分;

echo '<div class="col-md-9"><span class="title"><a href="' . the_permalink() . '" rel="bookmark" title="Permanent Link to ' . the_title_attribute() . '">title</a></span><br />';

or you can use; 或者你可以使用

$current_post_id = get_the_ID(); // id of current post in the loop
$permalink = get_permalink( $current_post_id );
$title = get_the_title( $current_post_id );

echo '<div class="col-md-9"><span class="title"><a href="' . $permalink . '" rel="bookmark" title="Permanent Link to ' . $title . '">' . $title . '</a></span><br />';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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