Php 为什么我不能在WordPress中使用带有echo的_permalink()?
我有以下代码:Php 为什么我不能在WordPress中使用带有echo的_permalink()?,php,wordpress,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"><s
<?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();
?>
而不是:
http://sitename.com/thelink
如果没有回声,我如何使这个循环工作?这真的是问题所在吗?像这样使用回声部分
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 />';
太棒了!但有一个问题,现在链接到我所在的同一页。知道如何让它链接到查询中每个实例的单个帖子页面吗?@lowercase您可以在get\u permalink中使用\u ID作为参数。查看我更新的答案,它工作得更好,但在做它应该做的事情之前,它会在一个大字符串中列出每个ID和标题。为什么/我如何摆脱它?@小写是我的错。我用了我的ID而不是获取ID。我修复了它,再次看到我更新的答案更好!但是最后一个问题。如何获取文章标题作为链接显示?我用的是'。“标题”取代了上面的“标题”,但它破坏了链接。
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 />';