为什么我不能在WordPress中使用带有echo的_permalink()?

时间:2014-02-26 19:53:37

标签: php wordpress

我有以下代码:

<?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();
?>

效果很好,但问题是,我不能使用:

<?php the_permalink() ?>

echo声明中。它是一个简单的链接,而不是呈现链接URL,它输出:

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

而不是:

http://sitename.com/thelink

如何在没有echo的情况下使这个循环工作?这实际上是问题吗?

1 个答案:

答案 0 :(得分:2)

使用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 />';

或者你可以使用;

$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 />';